As you can see, this is an English book. Since the domestic Gradle translation materials are incomplete, I have specially opened a column to translate the book Gradle for Android, and at the same time add my own experience and actual work experience. I hope you will like it. If you are a new Android developer, or a newbie switching from eclipse to Android studio, then I strongly recommend you to follow my article, as shown on the cover, to use the gradle build tool to automatically build your Android project. Without further ado, let's get started. Today I will mainly introduce the use of Android studio tools, as well as the basics of cradle, the use of cradle wrapper and how to migrate from eclipse to Android studio. This article was written on December 30, 2015. The official version of Android studio has been developed to 1.5, and the preview version has reached 2.0, so let's switch to Android studio. When you first open Android studio, there is a view that shows you the environment you are about to create and makes sure you are using the latest Android SDK and the necessary Google dependencies. It also lets you choose whether to create an AVD so that you can use the emulator. A few more words here:
Understanding basic Gradle If you want to create an Android project based on gradle, you must write a build script, which is usually called build.grade. You may have noticed that when we look at this script, gradle will provide us with a lot of default configurations and common default values, which greatly simplifies our work. For example, when using ant and maven, we need to write a lot of configuration files, which is disgusting. And gradle has default configurations. If you need to use your own configuration, you can simply rewrite them. The Gradle script is not like a traditional XML file, but a dynamic DSL based on Groovy, which is a dynamic language based on JVM. You don't have to worry at all. When using gradle, you still need to learn the Groovy language. The language is easy to read, and if you have already learned java, learning Groovy will not be difficult. If you want to start creating your own tasks and plug-ins, then you'd better have a deeper understanding of Groovy. However, since it is based on JVM, it is entirely possible for you to develop your own plug-ins through pure Java code or any other JVM-based language. As for plug-in development, we will have relevant introduction later. Projects and tasks There are two important concepts in grade, namely project and tasks. Each build is completed by at least one project, so the project in Android studio is not the same concept as the project in Gradle. Each project has at least one task. Each build.grade file represents a project. Tasks are defined in build.gradle. When initializing the build process, gradle will gather all projects and tasks based on the build file. A task contains a series of actions, which will then be executed in sequence. An action is a piece of code that is executed, much like a method in Java. Build lifecycle Once a task is executed, it will not be executed again. Tasks that do not contain dependencies are always executed first. A build will go through the following three stages:
Configuration file for build.gradle Projects built based on grade usually have at least one build.gradle, so let's take a look at Android's build.gradle:
This is where the actual build begins. In the repository address, we have used JCenter. JCenter is similar to Maven repository and does not require any additional configuration. Grade also supports several other repositories, both remote and local. The build script also defines an Android build tool, which is where the Android plugin comes in. The Android plugin provides everything you need to build and test your application. Every Android application needs a plugin like this:
Plugins are used to extend the capabilities of gradle scripts. Plugins are used in a project so that the build script of the project can define the properties defined by the plugin and the tasks that use it. Note: When you are developing a dependent library, then you should use 'com.android.library', and you cannot use both of them at the same time, it will cause build failure, a module either uses Android application or Android library plugin, not both. When using the Android plugin, the Android tag will be available, as shown below:
We will discuss more properties in Chapter 2. Project Structure Compared with Eclipse, the structure built by Android studio is very different:
The grade project usually contains a build.gradle in the root folder, and the code used is in the app folder. This folder can also be named other than app. For example, when you use Android studio to create a project for a mobile application and an Android wear application, the modules will be called application and wearable by default. Gradle uses a concept called source set. The official explanation is: a source set is a set of resource files that will be compiled and executed. For Android projects, main is a source set, which contains all the resource code. When you start writing test cases, you usually put the code in a separate source set, called androidTest, which only contains tests. Getting started with the Gradle Wrapper Grade is just a build tool, and new versions are always updated, so using Gradle Wrapper will be a good choice to avoid problems caused by Gradle version updates. Gradle Wrapper provides a windows batch file and other system shell files. When you use these scripts, the current gradle version will be downloaded and automatically used in the project build, so each developer only needs to use Wrapper when building their own app. So developers do not need to install any gradle version for your computer. On Mac, you only need to run gradlew, and on Windows, you only need to run gradlew.bat. You can also use the command line ./gradlew -v to view the current gradle version. The following is the folder of wrapper:
You can see a bat file for Windows, a shell script for Mac, a jar file, and a configuration file. The configuration file contains the following information:
You can change the url to change your gradle version. Using basic build commands Using your command line, navigate to your project and type:
This command will list all the runnable tasks. You can also add the --all parameter to see all tasks. When you are developing, to build the project, you need to run the assemble task through the debug configuration:
This task will create a debug version of the app, and the Android plugin will save it in the MyApp/app/build/outputs/apk directory. In addition to assemble, there are three basic commands:
Keep old eclipse file structure This article will not describe how to import eclipse projects into Android studio.
Configuring this in the grade file will preserve the eclipse directory structure. Of course, if you have any dependent jars, you need to tell gradle where they are. Assuming the jars are in a folder called libs, you should configure it like this:
This line means: treat all jar files in the libs folder as dependent packages. Summarize Through this article, we can learn the advantages of gradle and why we should use gradle. We took a brief look at Android studio and how it helps us generate build files. At the same time, we learned about Gradle Wrapper, which makes it easier for us to maintain and share projects. We know how to create a new project in Android studio, and how to migrate from eclispe to Android studio and keep the directory structure. At the same time, we learned the most basic gradle tasks and command line commands. In the next article, we will customize our own build files. |
<<: Android login page imitation hook effect, you will always need it!
>>: Gradle for Android Part 2 (Getting Started with Build.gradle)
"Hotspot Review | Major Technology Events of...
Not long ago, China Mobile, China Unicom and China...
Self-driving cars are already a hot topic that ev...
recent Produced by CCTV The TV series "My Al...
This article mainly talks about how to develop a ...
First, I would like to talk about the two dimensi...
Winter vacation is coming to an end. Are your chi...
In October, the article "Watching Douyin and...
NASA 's Perseverance rover has discovered pos...
For some species, when external conditions become...
In the earth's atmosphere, there are weather ...
I believe everyone has heard of the fairy tale &q...
If we were to divide the world's people into ...
During the 3.15 period this year, CCTV's 3.15 ...
Today I want to talk to you about a question that...