Friends, do you often hear about Gradle? We often see Gradle in projects. We are not so familiar with Gradle, and we seem to have never seen Gradle before. The following will be led by little De to understand it, know it.
What is Gradle
2, not related to AS, Android.
Gradle uses a language called Groovy, which is more straightforward.
4. Create custom build logic using plug-ins.
Kid’s question 1: How does the building system represent it?
Kid’s question 2: How does the plug-in approach work?
The plug-in approach is shown below
[1] For example, in build.gradle, there is a phrase like “apply plugin ‘android'”, which means that the project has added an Android plugin, and its function is to provide Androd related functions
[2], for example in the build. Gradle, classpath ‘com. Android. View the build: gradle: 0.11.1’ such a word, said the introduction of gradle 0.11.1 version, the introduction of this plug-in.
[3] Also in Dependence, a plug-in for okHttp is introduced.
Kid’s Question 3: How is the custom build logic represented?
Custom build logic provides flexibility.
[1] For example, you can define gradle version 1.0 or 2.0 for your build system in your project. (In real life: do you want a 1.0 person or a 2.0 person)
[2] For example, if you want to parse Json in a project, do you want to use Gson? Think FastJson. You can customize it to your liking.
As Android developers, we often hear Gradle used to build projects, so what is the relationship between AndroidStudio and Gradle?
The relationship between AS and Gradle
After having a general understanding of Gradle above, let’s learn the details of Gradle in detail. First, let’s understand the syntax of Gradle.
Gradle language syntax
Gradle language syntax, design the following two points:
1. Script to object
Gradle is the script file that converts the script into an object when it is executed.
Such as
When build.gralde is executed, it is converted to a Project object.
When settings.gradle is executed, it will be converted to a Settings object.
The table below shows the different objects for the different scripts.
Type of script | Transformed object |
The Build script | Project |
Settings script | Settings |
2. Structure of the script
A build script consists of multiple method blocks or methods.
For example, in build.gradle
(1) Apply plugin: ‘com.android.application’
Apply **([plugin: ‘com.android.application’])**, you can see that it is a method.
We can see that project calls the apply method, which passes a map parameter with the key plugin and the value ‘com.android.application’.
Let’s look at another example
(2)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Copy the code
Write dependencies like this:
Project dependencies ({add (' the compile ', 'com. Android. View the build: gradle: 2.0', {})})Copy the code
Conclusion:
After learning Gradle syntax, we have a slightly deeper understanding of Gradle. Now let’s learn the three concepts involved in Gradle, project, task and activity
projects , tasks and action
Project
When you look at the diagram below, you will have a completely new concept of projects and tasks.
Task
3. There is a task under the build.gradle file of the main project
task clean(type: Delete) {
delete rootProject.buildDir
}
Copy the code
Indicates that the name of the task is clean, the responsibility of the task is Delete, the file is deleted, and the content is deleted in the buildDir directory of rootProject.
How to Perform tasks
There are two ways to perform a task
One way to do this is by clicking on Studio shortcuts like Build and Clean,
One way is the way commands are executed.
Mode of command
The following is a list of ways to enter a command on the command line to perform a task.
gradlew task -all | List all tasks |
gradlew assembleDebug | Export all channel test packages |
gradlew assembleRelease | Export all channel packages |
gradlew assembleBaiduDebug –stacktrace | Export the specified channel test package with exception information |
gradlew stop | Stop compiling immediately |
gradlew check | Check the task |
gradlew build | Check and assemble are implemented |
gradlew clean | Clear all intermediate compilation results |
For example, enter GradLew assembleDebug in Terminate to generate the debug package.
After studying projects, tasks and activities, you will learn a concept that is very closely related to this concept, which is the process of building Gradle
Build lifecycle (the process of building a project -_-)
This is the phase of creating a project, which is divided into three phases
Gradle related files
local.properties
[1] The function of documents
Specifies the directory for the SDK.
[2] Contents of the document
sdk.dir=D:\ProgramWork\ASSDK
Gradle – Wrapper.properties (Gradle version)
1. What is it
Gradle-wrapper. properties determines the version of Gradle in As.
Gradle-wrapper. properties:
#Fri Nov 10 19:53:11 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
Copy the code
DistributionUrl: is the address of the gradle to download. Change the gradle version here
3. Content explanation
Gradle has three versions:
[1] Gradle-xx-all.zip: The complete version, including various binaries, source files, and offline documentation. For example: services.gradle.org/distributio…
【 2 】 gradle – xx – bin. Zip: it contains only the binary file (do not include the source code and documentation), such as: services.gradle.org/distributio…
【 3 】 gradle – xx – SRC. Zip: contains the gradle source, cannot be used to build your project For example: services.gradle.org/distributio…
Download location
Extract the location
Together, the distributionBase and distributionPath are used to store the location of the file after decompression of Gradle-3.3-all. zip. The distributionPath is a subdirectory of the specified directory
Note: The download location can be different from the decompression location
ZipStoreBase and distributionBase have two values: GRADLE_USER_HOME and PROJECT GRADLE_USER_HOME: indicates the directory of a user.
Under window %USERPROFILE%/.gradle for example: C:\User<user_name>.gradle\
PROJECT: indicates the directory of the current PROJECT
4, As first load Gradle process
【2】 In the zipStorePath directory under zipStoreBase, which is C:\Users<user_name>. Gradle \wrapper\dists\gradle-3.3-all<url-hash>,
The strength of the whole railway network to C: \ Users \ Administrator gradle \ wrapper \ dists \ gradle – 3.3 – all \ 55 gk2rcmfc6p2dg9u9ohc3hw9
The gradle-3.3-all directory is named according to the file name of the downloaded Gradle. The directory is md5 value calculated based on the distribution URL directory string.
【3】 Decompress gradle-3.3-all.zip and store the file in C:\Users<user_name>. Gradle \wrapper\dists
By now, do you know why opening a project is so slow? Because because AS went back to download Gradle.
How to load Gradle process when AS opens the project again
Settings. gradle (Module Management Minister)
**1, the role of files **
[1] Decide which modules will be built.
[2] will be executed during the initialization phase.
2. How can I increase the compilation speed of my project?
Build. Gradle in Project (generic bulletin)
1. What is it
The configuration information is common to all modules.
Module under build.gradle
1. What is it?
[1] The build.gradle file in the module can only be used for the current module. You can overwrite the contents in the build.gradle file in the home directory.
[2] are divided into three modules: apply the plugin, android, dependencies.
Gradle.properties (configuring public dependencies)
1. What is it?
[1] Some common dependencies are configured.
[2] This dependency can be shared by multiple libraries in order to cause library conflicts.
2. Contents of the document
# Project-wide Gradle settings. # IDE (e.g. Android Studio) users: # Gradle settings configured through the IDE *will override* # any settings specified in this file. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. org.gradle.jvmargs=-Xmx4096m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # Org.gradle. parallel=true COMPILE_SDK_VERSION=26 BUILDTOOLS_VERSION=26.0.2 MIN_SDK_VERSION=19 TARGET_SDK_VERSION=24 Android.enablebuildcache =true org.gradle.daemon=false Android.enableaapT2 =false # public dependency DPDCJunit = junit:junit:4.12 DPDCAndroidAppCompatV7 = com. Android. Support: appcompat - v7:26.0.2 DPDCAndroidDesign = com. Android. Support: design: 26.0.2 DPDCAndroidRecyclerView = com. Android. Support: recyclerview - v7:26.0.2 DPDCAndroidBaseRecyclerViewAdapterHelper = Com. Making. CymChad: BaseRecyclerViewAdapterHelper: 2.9.30 DPDCAndroidButterKnife = com. Jakewharton: butterknife: 8.8.1 DPDCOkhttp3 = com. Squareup. Okhttp3: okhttp: 3.10.0 DPDCGlide = com. Making. Bumptech. Glide: glide: 3.8.0 #?? DPDCGlideOkHttp = Com. Making. Bumptech. Glide: okhttp3 - integration: 1.4.0 @ aar DPDCGson = com. Its code. Gson: gson: 2.8.0 DPDCFASTJSON = Com. Alibaba: fastjson: 1.1.68. Android DPDCEasyPermissions = pub. Devrel: easypermissions: 0.2.1 DPDCEventBus = Org. Greenrobot: eventbus: # 3.1.1 Google DPDCFindBug = com. Its code. Findbugs: jsr305:3.0.0Copy the code
Note 1, we can see some of the public, DPDCAndroidButterKnife = com. Jakewharton: butterknife: 8.8.1 butterknife, for example, in the repository can use these rely on.
3. Refer to them in Build. gradle for Modual
apply plugin: 'com.taobao.android.emas'
apply from: 'common.gradle'
android {
compileSdkVersion Integer.parseInt(project.COMPILE_SDK_VERSION)
buildToolsVersion project.BUILDTOOLS_VERSION
Copy the code
Code parsing:
1. Project.COM PILE_SDK_VERSION is a String value
2. CompileSdkVersion needs a value of type int, so we need to convert this value to an int.
Config.gradle (global configuration)
1. What is it?
Projects often use the same configuration, such as compileSdkVersion, buildToolsVersion, and so on, in different Modules. These common configuration items are called shared variables.
To solve this problem, we define a file named common_config.gradle and place it in the project root directory.
2. How do I configure this parameter?
Step 1: Create a config.gradle file in the project root directory
Ext {CFGS = [compileSdkVersion: 26, buildToolsVersion: "26.0.2", minSdkVersion: 14, targetSdkVersion: 26, versionCode: 10, versionName: "2.0.0", ucropVersionCode: 22, ucropVersionName: "2.2.0-native", // Open version Control androidSupportVersion: "26.0.2", Glide: "3.8.0", RxJava: "2.0.5", RxAndroid: "2.0.1]}"Copy the code
Step 2: Introduce the config. Gradle file into your project’s build.gradle file
apply from: “config.gradle”
Step 3: Use rootproject.ext.xxxxx in the Module build.gradle
Gradle package and publish function library to JCenter and JCenter
Add……
Gradle installation
Gradle Wrapper (equivalent to Gradle version administrator)
(1) Gradle project can be flexible configuration Gradle version.
(2) No manual download and installation is required. The configured version can be downloaded automatically.
The Gradle Wrapper in AS is AS follows
According to the path above, the Gradle Wrapper will be automatically downloaded for us
Gradle details
Gradlew and gradler. Bat
Gradlew: Used on Linux, is the startup script for GradLew.
Gradlew. Bat: used on Windows, is the startup script for GradLew.
In layman’s terms, this is how you start Gradle.
Gradle and Settings relationships
Gradle will import projects in the specified directory according to Settings. Gradle configuration.
build.gradle
Tell the project how to configure it, such as which dependencies to download, which remote repositories to use, which plug-ins to configure, etc.
gradle.properties
Gradle properties configuration file, values are added to the Gradle Project object.
For example, you set COMPILE_SDK_VERSION=29 in gradle.properties
Then use it as project.COMPILE_SDK_VERSION in your project’s build.gradle
Gradle role
1, where to find build plug-ins, to which repositories maven, JCenter to find plug-ins.
For example, in the project build.gradle, the buildScript has the following, indicating that the plug-in is found from Google or from jCenter
repositories {
google()
jcenter()
}
Copy the code
2. Look for built plug-ins, like Kotlin’s.
For example, in the project build.gradle, the buildScript has the following instructions to load gradle and Kotlin plug-ins
Dependencies {classpath "com. Android. Tools. Build: gradle: 4.0.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }Copy the code
3. Where to find project dependencies, such as Maven repository, JCenter repository, etc.
For example, in the build.gradle-allprojects global configuration of the project, the dependencies of the project can be found in Google () and jCenter ().
allprojects {
repositories {
google()
jcenter()
}
}
Copy the code
4, find the project dependencies, such as the configuration of some dependency packages, okHTTP, etc.
5. How to build a project, such as the following command, which is every time gralde builds a project, it deletes the build directory and creates it again.
task clean(type: Delete) {
delete rootProject.buildDir
}
Copy the code
Gradle custom tasks
For example, write the following code at the bottom of your build.grale project, where println(‘Hello’) is the preparation task and doLast is the execution body of the task.
task hellWorld(group: 'hello'){
println('Hello')
doLast {
println('World')
}
}
Copy the code
Then click the triangle arrow on the left to perform the task. The result is as follows
At the same time, the group Hello and the task helloWorld appear in the right Gradle.