1. Gradle Important concepts

  • The official documentation

  • Gradle is a build tool, or programming framework. Gradle is based on the Groovy language, which is based on Java. In contrast to Maven, Gradle is programmable.

  • Set the option GRADLE_HOME: environment variable that tells the operating system where Gradle is installed. GRADLE_USER_HOME: Environment variable used to store common Gradle runtime configurations, caches, etc. The default value is $HOME/. Gradle. Gradle-wrapper. gradle: gradle-wrapper.gradle: gradle-wrapper.gradle: gradle-wrapper.gradle: gradle-wrapper.gradle:

  • Project: In Gradle, each Project to be compiled is called a Project, so there can be multiple projects, and each Project can be packaged as a JAR, war, etc., that is, as a separate module. Every Project has a build file (each build.gradle is converted to a Project object)

  • Additional Attributes Use Ext to add additional attributes to Project and Task. Both Project and Gradle objects can set ext properties. Once an Ext is defined, it is possible to invoke properties across scripts.

    ext.pwd="abc"
    ext {
        url = "www.baidu.com"
        username = "zhangsan"
        flag = true
    }
    Copy the code
  • Project synchronization After modifying gradle files, you need to perform a synchronization operation. With Android Studio, this can be triggered via Sync Project with Gradle Files. It essentially performs the Generation debugsouces task.

2. Enter options

  • -PTo add gradle parameters when compiling an Android project, you can enter the build command in Terminal or in the Setting of AndroidStudioCommand-line OptionsOption, write the parameter. When multiple parameters are carried, append them one by one:-Pparam1=123 -Pparam2=456 ...Then read the parameters:
    sdk_active = project.hasProperty('sdkactive')? project.property('sdkactive') : DEFAULT_SDK_ACTIVE
    Copy the code

3. Build options

  • Whether to use a daemon process for the build. Default is true.

    Set the parameters in gradle.propertiesorg.gradle.daemon=false

4. Gradle Workflow

Too simple to say, need to understand

  1. Initialization: Gradle has an initialization process, which settings. Gradle executes.
  2. Configuration: In this phase, each Project is parsed. Instead of executing the tasks, it evaluates the dependencies of the task and creates a directed acyclic graph of the task based on their dependencies
  3. Execute: If you specify a task in Gradle XXX, Gradle will execute all the tasks in the XXX task chain in dependent order.

Objects in Gradle

5.1 Gradle Objects: When running the Gradle command, a Gradle object is constructed by default. And only one object is generated during the entire execution

/ / code
println( gradle.getGradleHomeDir() )
println( gradle.getGradleUserHomeDir() )

//-------------------------
// Execution result
C:\Users\xxx\.gradle\wrapper\dists\gradle-5.11.-all\97z1ksx6lirer3kbvdnh7jtjg\gradle-5.11.
C:\Users\xxx\.gradle
Copy the code

5.2 Project Objects: Each build.gradle is converted to a Project object

println(project.rootDir)
println(project.projectDir)

//-------------------------
// Execution result
D:\Projects\MyApp
D:\Projects\MyApp\utillib  
Copy the code

Setting. Gradle is converted to a Setting object

  • Taking the Android project as an example, the assumed project structure is abstracted as follows:
MyApp (Root Project) | | - app (module 1) | | - build. Gradle | | - utillib (module 2) | | - build. Gradle | | - build. Gradle | -- Settings. Gradle | -- gradle. Properties | - gradle - wrapper. GradleCopy the code

As you can see from the above structure, Root Project contains two child projects, app and Utillib.

  • Build. gradle: Each Project requires a build.gradle file
  • Settings. gradle: Tells Root Project that a Project contains several sub-projects
include ':app'.':utillib'
Copy the code

Use the gradle command to view

# command
gradlew projects

# the results
> Task :projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'MyApp'
+--- Project ':app'
\--- Project ':utillib'

Copy the code

6. Supplementary:

6.1 subprojects and allprojects

A single childProject can be uniformly configured in rootProject: SubProjects and AllProjects

  • Allprojects is a configuration for allprojects, including Root projects.
  • Subprojects is a configuration for all Child Projects
  • buildscript
buildscript {

  repositories {
      google()
      jcenter()

    }
    dependencies {
      classpath 'com. Android. Tools. Build: gradle: 3.4.2'}}Copy the code

6.2 Difference between Release and Snapshot

  • Snapshot version represents an unstable version under development, Snapshot version. (Early edition)

  • A Release is a stable version, a Release.

  • To generate a release aar, enter gradlew assembleRelease in Terminal AS

6.3 Garbled Characters Occur when Building a Project using IDEA + Gradle

Method one:

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

Method 2: In the idea, help-edit Custom VM Option add configuration

-Dfile.encoding=UTF-8
Copy the code

The file is stored in C:\Users\ XXXXX \AppData\ JetBrains\IntelliJIdeaxxxxx

In idea, File–Settings– File Encodings sets the encoding format