Gradle is a Configuration language developed by Google to replace Ant’s build based on Groovy

Gradle is a compilation system for Android based on the Groovy language. Google has developed a DSL language for Android compilation using Groovy. Or Java code

Gradle Wrapper Every project built on Gradle has a gradle local agent called gradle Wrapper which declares the pointing directory and version in the /gradle/wrapper/gralde-wrapper.properties directory

Every version of the official agent services.gradle.org/distributio download address… If gradle builds slowly at first, you can manually download the agent to ${USER}/.gradle/wrapper/dists

Create gradle.properties file locally or create gradle.properties file in user’s.gradle directory as global Settings

# open parallel compilation org. Gradle. The parallel = true # open daemon org. Gradle. # daemon = true on-demand org. Gradle. Configureondemand = true # set compile the JVM parameter Org. Gradle. Jvmargs = - Xmx2048m - XX: XX: MaxPermSize = 512 m - + HeapDumpOnOutOfMemoryError - Dfile. Encoding = utf-8 # set agent SystemProp, HTTP proxyHost = 127.0.0.1 systemProp. HTTP. ProxyPort = 10384 systemProp. HTTPS. ProxyHost = 127.0.0.1 SystemProp. HTTPS. ProxyPort = 10384 # open compile JNI support outdated API for android. UseDeprecatedNdk = trueCopy the code

Common commands

Note: In Windows you can run gradlew directly. For Linux or MAC you can run gradle. Gradlew is abbreviated to./gradlew

Gradle task query command

  • All subsequent commands must be visible in tasks –all, otherwise the report will not find the task
/gradlew tasks --all run./gradlew on a task [TaskName] on a module [moduleName] :moduleName:taskNameCopy the code

instructions

Submodule tasks do not mean that project roots have the same tasks, so you need to independently query Moduel best naming practices for all lowercase English to prevent compile compatibility issuesCopy the code

1. Quick build commands

/gradlew clean # check dependencies and compile package./gradlew build # compile and installDebug package./gradlew installDebug # Compile and print the log./gradlew build --info Build /reports/profile./gradlew build --profile # Build and print the stack log./gradlew build --info --debug /gradlew clean build --refresh-dependenciesCopy the code

If you need to specify build, use the following command

Gradle specifies the build target command

Gradlew AssemblbleDebug is an assembleDebug package. Gradlew assembleDebug is an assembleDebug package. Gradlew assembleDebug is an assembleDebug package This is the abbreviation assembleRelease./ Gradlew aRCopy the code

Gradle updates the latest dependency issues

This is a problem that confuses many developers. In fact, if you study it, you will know it

  • ${user}/. Gradle /caches/modules-2 (default cache update is 24 hours)
  • ${project.root}/. Gradle also makes a layer of cache in the current project
  • With IDE integrated development environments, you also add a layer of caching (in the IDE cache directory)
  • Project development profile (current project under.idea.vsc etc.), this will affect the code prompt

You need to update the gradle command to refresh the gradle dependencies section in the following order: But it doesn’t affect the IDE. If you want the IDE to know when you’re writing code, you need to refresh or modify the IDE cache. How do you do that depends on the situation

Script tool Current directory IDEA project cleaning tool

Script tool Gradle local cache SNAPSHOT cleaning tool

Script tool is compiled by PYTHon2, how to use it globally, please configure it in environment variables, need additional functions, please modify the script by yourself

Gradle builds and installs debug commands

/gradlew install app:assembleDebug /gradlew install app:assembleDebug Gradlew assembleRelease # install gradlew installRelease # uninstall Gradlew installReleaseCopy the code

Gradle multichannel packaging

Assemble can also combine productFlavors with errors like Task 'Install' is ambiguous in root project, Look at the multiple channels configured and modify the command to./gradlew install[productFlavorsName] app:assembleDebug to build debugging with the commandCopy the code
Gradlew installRelease./gradlew uninstallReleaseCopy the code
Gradlew assembleRelease # Debug Test Gradlew assembleTestRelease # debug Release ./gradlew assembleCopy the code

Gradle looks at package dependencies

#. / gradlew dependencies or dependent module. / gradlew app: dependencies # retrieval dependent libraries. / gradlew app: dependencies | grep CompileClasspath # Windows without the grep command. / gradlew app: dependencies | findstr searches "CompileClasspath" # will retrieve to rely on group found Such as multiDebugCompileClasspath Is a multi channel distribution development relying on. / gradlew app: dependencies - configuration multiDebugCompileClasspath # general compile time dependent libraries, not fixed configuration mode, /gradlew app:dependencies --configuration compile # /gradlew app:dependencies -- Configuration RuntimeCopy the code

Gradle dependency management

  • Transitive dependency
Dependencies {transitive true // Manually configure the transitive property to false to prevent dependencies}Copy the code
  • Force the compilation version to be specified
Configurations. All {// transitive false // Force to specify the version resolutionStrategy{force 'org.hamcrest: hamcrea-core :1.3' // Force not to compile all*.excludegroup: 'org.hamcrest', module:'hamcrest-core' } }Copy the code
  • Dynamic dependence
< span style = "max-width: 100%; clear: both; min-height: 1em group:'a',name:'a',version:'latest.integration' }Copy the code
  • View detailed dependency information

8. Use offline mode

./gradlew aDR --offline
Copy the code

daemons

./gradle build --daemon
Copy the code

10. Parallel compilation

./gradle build --parallel --parallel-threads=N
Copy the code

11. Compile mode on demand

./gradle build --configure-on-demand
Copy the code

12. Do not rely on the repository with snapshot

If it’s available offline

./gradlew clean aDR
Copy the code

13. Set the code

allprojects {
...
    tasks.withType(JavaCompile){
        options.encoding = "UTF-8"
    }
...
}
Copy the code

The warehouse is set

Set up central warehouse

The default is jCenter, which can be mavenCentral

After 4.0 repositories {/ / gralde, access to the warehouse for https://dl.google.com/dl/android/maven2/ Google () / / private, Or domestic mirror warehouse configuration method maven {url "http://maven.oschina.net/content/groups/public"} / / maven centeral by Sonatype services provided by the company, It is the default repository for ApacheMaven, SBT, and other build systems mavenCentral() // jcenter Java repository in Bintray provided by JFrog, is the default repository within GoovyGrape, Gradle built-in support jcenter () / / mavenCentral and jcenter search http://mvnrepository.com/}Copy the code

The writing order of the Repository affects the speed of the pull because the Groovy script executes by array subscripts