The original address
The introduction of Gradle
1.1 gralde
Gradle is a project building tool that automatically builds a project by associating a bunch of directories and files according to certain rules. AndroidStudio builds are built using Gradle.
A brief history of build tools: We used Ant to automate android packaging a long time ago, but jar dependencies were cumbersome to deal with. So maven, in addition to automatic packaging, can also automatically download the corresponding JAR package dependencies, but Maven logic is rigid, customization is difficult. Gradle has the best of both worlds, with flexible scripts, and is now the official Android build automation tool.
Gradle version Example: gradle-6.1.1-all.zip. It is normally located in the gradle folder -> Wrapper ->gradle-wrapper.properties of the project home directory.
Automated builds of Android Studio are built on top of Gradle, and the Android Gradle plugin adds several features specifically for building Android applications.
Gradle versions can be specified from the File > Project Structure > Project menu in Android Studio. It can also be specified in the gradle/wrapper/gradle-wrapper.properties file. The following example sets gradle version to 6.1.1 in the gradle-wrapper.properties file.
. DistributionUrl = https\://services.gradle.org/distributions/gradle-6.1.1-all.zip...Copy the code
1.2 Gradle plug-in
The Gradle plugin is a tool packaged with Gradle distributions and Android SDK Build Tools. It has two main functions:
- Build the project by calling Gradle’s own code and batch tools
- Call the compile and package functions of the Android SDK
Gradle plugin versions can be specified from the File > Project Structure > Project menu in Android Studio or from the top-level build. Gradle File. The specified plug-in version will be used for all modules built in the corresponding Android Studio project. The following example sets the plug-in version to 4.0.1 from the project’s build.gradle file:
// Gradle 4.1 and higher include support for Google's Maven repo using // The Google () Method. And you need to include this repo to download // Android Gradle Plugin 3.0.0 or higher.google ()... } dependencies {classpath 'com. Android. View the build: gradle: 4.0.1'}}Copy the code
Gradle plug-ins have version numbers, and each version number should have one or more Gradle distributions (usually limited to a minimum version). The following table lists the Gradle versions required for each Android Gradle plugin version. For best performance, you should use the latest versions of Gradle and plug-ins:
Gradle common commands
2.1 Gradle Query Commands
Viewing Main Tasks
./gradlew tasks
Copy the code
Look at all tasks, including cache tasks, and so on
./gradlew tasks --all
Copy the code
2.2 Gradle Runs the command
Perform a task for a module [moduleName] [TaskName]
./gradlew :moduleName:taskName
Copy the code
2.3 Gradle rapid build commands
Gradle’s quick build commands replace the IDE’s visual build operations, such as clean, Build, and so on, which we use most often.
Viewing the build
./gradlew -v
Copy the code
Clear the Build folder
./gradlew clean
Copy the code
Check dependencies and compile packages. Note that the build command builds packages for both the Debug and release environments.
./gradlew build
Copy the code
Compile and install the Debug package
./gradlew installDebug
Copy the code
Compile and print logs
./gradlew build --info
Copy the code
Compile and output performance reports in the build/reports/profile root directory
./gradlew build --profile
Copy the code
Debug mode builds and prints the stack log
./gradlew build --info --debug --stacktrace
Copy the code
Force the latest dependencies to be updated and clean the build before building it
./gradlew clean build --refresh-dependencies
Copy the code
Compile and Debug package
./gradlew assembleDebug # simplified version command, take the first letter of each word./gradlew aDCopy the code
Compile and type the package Release
Gradlew assembleRelease./gradlew assembleReleaseCopy the code
2.4 Gradle Installation Commands
Release mode is packaged and installed
./gradlew installRelease
Copy the code
Uninstall the Release mode package
./gradlew uninstallRelease
Copy the code
Debug Release mode All channels are packaged
./gradlew assemble
Copy the code
2.5 Gradle Displays package dependency commands
Look at dependencies in the project root directory
./gradlew dependencies
Copy the code
View dependencies under the APP module
./gradlew app:dependencies
Copy the code
Look at dependencies under the APP module that contain the implementation keyword
./gradlew app:dependencies --configuration implementation
Copy the code
Gradle life cycle
Click on the elephant icon (Snyc Project with Gradle Files) in the upper right corner of Android Studio and Gralde will build our Android Project for us. The whole construction process is divided into three parts:
3.1 Initial Build
The figure above shows a common multi-component project Gradle Scripts, using a Gradle build for that project as an example.
After clicking the elephant icon (Snyc Project with Gradle Files) in the upper right corner of Android Studio, Gradle creates a Settings object for the settings.gadle file, Read and execute all configuration code in settings.gadle file:
include ':app',
':client',
':mars-core-release',
':emojilibrary',
':imagepicker',
':avenginekit',
':push',
':lvb',
':beauty',
':AliyunVideoCommon',
':AliyunPlayerBase',
':AliyunLiveShiftPlayer'
include ':lib_base_code'
Copy the code
Settings. gadle finds the build.gradle file for all the projects contained in settings.gadle and creates a hierarchical Project object based on this file.
The official document address of Settings
The official document address of the Project
If you want to know more about these two interfaces, you can go to the official website for further study.
Process summary:
Click Build (Snyc Project with Gradle Files). Gradle will create a Settings object based on the settings.gadle file. This object automatically creates a Porject object for the root Project according to include:Project in settings.gradle. The Project object corresponds to the build.gradle file.
3.2 Configuration and Construction
Execute all the configuration code in the build.gradle file of the project, download the three parties, dependencies, and build the dependency diagram of the Task. Building dependency diagrams for tasks is at the heart of Gradle.
3.3 Executing a Build
Determine the subset of tasks to be executed based on the task commands and parameters and execute the tasks. For multiple projects, each execution of the task requires the previous initialization and configuration of all the included projects.
In build.gradle, write the following code to get the corresponding log
3.4 hook
Gradle provides hooks for a Project or Task that needs to be executed at a certain stage
The Gradle execution process executes the above methods in the following order:
Code examples:
BeforeEvaluate {println 'before configuration'} / this. AfterEvaluate {println 'before configuration'} / this. AfterEvaluate {println 'configuration phase after the execution'} / * * * gradle execution after the callback * / this. Gradle. BuildFinished {println 'execution phase after the completion of the'} / / beforeEvaluate equivalent This. Gradle. BeforeProject {} / / equivalent afterEvaluate enclosing gradle. AfterProject {println ' 'configuration phase has been completed}Copy the code
References:
gradle
gradle github
Gradle official documentation
Android developers
Gradle Summary – Lifecycle
Gradle Automatic Build technology