Gradle is something we Androids work with all the time, and it comes in very handy for doing some automation during build time. To understand how to use it, it is also convenient for us to do some compilation optimization during development.

Here are four common Gradle tips

Gradle global variable definitions are used

  1. gradle.extGlobal variables (can be found insetting.gradleIn gradle scripts, you can configure some common properties. In gradle scripts, you can configure a method with one click.
Def hello = {it -> println "hello"} gradle.ext.hello = helloCopy the code
  1. Global extension parameters at the project level can be set to Ext in rootProject. As follows, set it in the outermost build.gradle
Ext kotlinVersion = "1.0" / / / using rootProject. Ext kotlinVersionCopy the code

Gradle Tasks are executed in order

  1. Use a dependsOn (the command will be followed by a dependsOn order)
// task a1() {doFirst {println "do a1"; DependsOn task a2(dependsOn:) DependsOn task () {dependsOn 'a2' doLast {println "do a2" exec {commandLine "ls"}} task () {dependsOn 'a2' doLast { println "do a3"; DependsOn task A4 () {doLast {println "do a4"; } } a4.dependsOn(a3)Copy the code
  1. Using finalizedBy
Task a1() {doFirst {println "a1"}} task a2() {doFirst {println "a2"}} a1. FinalizedBy (a2) {doFirst {println "a2"}} a1.Copy the code

Plugin command to view the dependency tree and find the source of duplicate dependencies and version number mismatch

./gradlew app:dependencies
Copy the code

The output log is very intuitive, tree list, there is a problem directly search the corresponding dependent library to view the version number

\ - androidx databinding: databinding - compiler: 4.1.3. + - androidx databinding: databinding compiler - common: 4.1.3 | + -- -- -- Androidx. Databinding: databinding - common: 4.1.3 | + -- -- -- com. Android. Databinding: baseLibrary: 4.1.3 | + -- -- -- Org. Antlr: antlr4:4.5.3 | + Commons - IO: Commons - IO: 2.4 | + -- -- -- com. Googlecode. Juniversalchardet: juniversalchardet: 1.0.3 | + -- -- -- com. Google. : guava guava: 28.1 jre | | + -- -- -- com. Google. Guava: failureaccess: 1.0.1 | | + -- -- -- Com. Google. Guava: listenablefuture: 9999.0 - empty - to - get - conflict - with - guava | | + -- -- -- Com. Google. Code. Findbugs: jsr305:3.0.2 | | + -- -- - org. Checkerframework: the checker - qual: 2.8.1 | | + -- -- -- Com. Google. Errorprone: error_prone_annotations: 2.3.2 | | + -- -- -- com. Google. J2objc: j2objc - annotations: 1.3 | | \ -- -- -- Org. Codehaus. Mojo: animal - sniffers - annotations: 1.18 | + -- -- -- com. Squareup: javapoet: 1.10.0 | + -- -- -- Org. Jetbrains. Kotlin: kotlin - stdlib - jdk8:1.3.72 | | + -- -- - org. Jetbrains. Kotlin: kotlin - stdlib: 1.3.72 | | | + -- -- -- Org. Jetbrains. Kotlin: kotlin stdlib - common: 1.3.72 | | | \ - org jetbrains: annotations: 13.0 | | \ -- -- -- Org. Jetbrains. Kotlin: kotlin - stdlib - jdk7:1.3.72 | | \ - org jetbrains. Kotlin: kotlin - stdlib: 1.3.72 | + -- -- -- (*) Com. Google. Code. Gson: gson: 2.8.5 | + -- -- - org. Anyone. Jaxb: jaxb - runtime: 2.3.1 | | + -- -- -- javax.mail. XML. Bind: jaxb - API: 2.3.1 | | | \ - javax.mail activation: javax.mail. Activation - API: 1.2.0 | | + -- -- - org. Anyone. Jaxb: txw2:2.3.1 | | + -- -- -- Com. Sun. Istack: istack - Commons - the runtime: 3.0.7 | | + -- -- - org. Jvnet. Staxex: stax - ex: 1.8 | | + -- -- -- Com. Sun. XML. Fastinfoset: fastinfoset: 1.2.15 | | \ - javax.mail activation: javax.mail. Activation - API: 1.2.0 | + -- -- -- Com. Android. The tools: annotations: 27.1.3 | \ - com. Android tools. Build. Jetifier: jetifier - core: 1.0.0 - beta09 | + -- -- -- Com. Google. Code. Gson: gson: 2.8.0 - > 2.8.5 | \ - org jetbrains. Kotlin: kotlin - stdlib: 1.3.60 - > 1.3.72 + - (*) Androidx. Databinding: databinding - common: 4.1.3 + - org. Jetbrains. Kotlin: kotlin stdlib - jdk8:1.3.72 + - (*) Com. Google. Auto: auto - common: 0.10 | \ - com Google. : guava guava: 23.5 jre - > 28.1 - jre (*) + Commons - IO: Commons - IO: 2.4 + Commons - codec: Commons - codec: 1.10 + - org. Antlr: antlr4:4.5.3 \ -- -- -- Com. Googlecode. Juniversalchardet: juniversalchardet: 1.0.3Copy the code

4. Rely on gradle files

apply from:".. /libconfig.gradle"Copy the code

Extract the generic Grdle script and place it in a separate Gradle file. Apply the file to each child module. Projects with multiple submodules can reduce the use of template code and reduce the amount of work needed to modify it. Libconfig. Gradle as follows

apply plugin: 'com.android.library' android { compileSdkVersion rootProject.ext.android.compileSdkVersion buildToolsVersion rootProject.ext.android.buildToolsVersion defaultConfig { minSdkVersion rootProject.ext.android.minSdkVersion targetSdkVersion rootProject.ext.android.targetSdkVersion versionCode rootProject.ext.android.versionCode versionName rootProject.ext.android.versionName javaCompileOptions { annotationProcessorOptions { arguments = [AROUTER_MODULE_NAME: project.getName()] } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'Proguard-rules.pro'}} buildFeatures {dataBinding = true} compileOptions {sourceCompatibility = 1.8 TargetCompatibility = 1.8}}Copy the code

The build.gradle subproject can reduce the configuration

apply from:".. /libConfig.gradle" dependencies { api project(':lib_utils') api rootProject.ext.dependencies["immersionbar"] annotationProcessor rootProject.ext.dependencies["arouter-compiler"] implementation RootProject. Ext. Dependencies [" arouter - API "] API 'androidx. Appcompat: appcompat: 1.3.0' API 'com. Google. Android. Material: material: 1.3.0' API 'androidx. Constraintlayout: constraintlayout: 2.0.4'}Copy the code