Android–Gradle plug-in gradle-wrapper.properties

1. Android Studio version, Android Gradle plug-in version, Gradle version

Android Studio uses Gradle to build code through the Android Gradle plug-in;

After each upgrade of Android Studio, the Android Gradle plug-in is automatically updated, and the corresponding Gradle version will also change;

Therefore, the following correspondence will be generated:

(1) Correspondence between Android Studio version and Android Gradle plug-in version

For details, see the official website description: Android Gradle plug-in version description | Android Developers | Android Developers

as the picture shows

(2) Correspondence between Android Gradle plug-in and Gradle version

See the official website description for details: Android Gradle plug-in version description | Android Developers | Android Developers

As shown below

(3) android studio Gradle plug-in configuration location

  • Method 1: Configure in the build.gradle file in the project root directory
  • Method 2: Set in project in the project structure panel under settings in android studio

2. Implement Gradle plug-in and Gradle version adaptation through gradle-wrapper

Gradle Wrapper is a wrapper for Gradle that allows us to run and build our projects without installing Gradle;

It is often necessary to modify the gradle-wrapper.properties configuration file to match the correspondence between the Android Gradle plug-in version and the Gradle version;

(1) The directory where Gradle Wrapper is located

The gradle-wrapper.properties file is usually located in the /gradle/wrapper/ directory under the project root directory and is a hidden file. In order to avoid misoperation, it is recommended not to modify this file directly, but to modify the Gradle Wrapper settings through the Gradle configuration interface provided by Android Studio.

(2) gradle-wrapper.properties configuration property file description

The role of the gradle-wrapper.properties file is to provide necessary configuration information for Gradle Wrapper so that Gradle Wrapper can automatically download, install and configure the Gradle running environment. By modifying the settings in the gradle-wrapper.properties file, we can control the Gradle version, download source, storage path and other parameters to customize the Gradle build process and environment.

  • distributionUrl: specifies the download address of the Gradle release version. Gradle Wrapper will automatically download the specified version of Gradle from this address.
  • distributionSha256Sum: Specifies the SHA-256 checksum of the Gradle version corresponding to distributionUrl, which is used to verify whether the downloaded Gradle is complete and has not been tampered with.
  • zipStoreBase and zipStorePath: Specify the storage path of the Gradle Zip package. When Gradle Wrapper is run for the first time, Gradle will download the Gradle Zip package from distributionUrl and decompress it to this path.
  • distributionType: Specifies the distribution type of Gradle. Optional values are “bin” and “all” (default value).

GRADLE_USER_HOME If not configured, the default is ~/.gradle

(3) Solve the problem of gradle domestic download failure and offline installation package configuration

If: distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

Since it cannot be downloaded in China, you can download gradle-7.4-all.zip through Thunder (which is awesome, but the browser cannot download it), and you can put it in a folder on your computer. Then modify the following radle-wrapper.properties configuration file to achieve this

#Sat Nov 12 21:00:23 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=file:///D:/GradleOffLinePackage/gradle-7.4-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
distributionType=all

(3) gradle build directory

The directory of gradle under the android studio project

├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── settings.gradle

(1)build.gradle

build.gradle is the configuration of a certain project. Configure jar dependencies, define or introduce tasks to complete project construction.

(2)gradle-wrapper-properties

GRADLE_USER_HOME If not configured, the default is ~/.gradle

zipStoreBase and zipStorePath define the local path where the downloaded gradle (gradle-7.6-bin.zip) is stored.
distributionBase and distributionPath define the local directory where the downloaded gradle is decompressed.

The wrapper defines which version of gradle the project depends on. If the local distributionPath does not have the corresponding version of gradle, the corresponding version of gradle will be automatically downloaded.

gradle-wrapper.jar will determine whether to download the wrapper-configured gradle, pass parameters to the downloaded gradle, and run the downloaded gralde to build the project.

(3)gradle.properties

Mainly used to configure variable values used in the build process. You can also configure the values of some gradle built-in variables to modify the default build behavior.

org.gradle.logging.level=quiet
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xms512m -Xmx2g -XX:MaxMetaspaceSize=512m -XX: + HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

org.gradle.jvmargs is used to configure Daemon’s JVM parameters. The default value is -Xmx512m "-XX:MaxMetaspaceSize=384m".
When our project is relatively large, the build may fail due to insufficient JVM heap memory, so we need to modify this configuration.
org.gradle.logging.level Adjust gradle’s logging level. Refer to gradle logging to select the desired logging level.

(4)gradlew and gradlew.bat

gradlew is available under macos and linux systems.
gradlew.bat is used under windows system

(5)settings.gradle

settings.gradle is mainly used to configure the project name and which sub-projects it contains.
It can also be used to configure the dependency version of the plug-in (which will not be applied to the project unless the project applies this plug-in) and the download of the plug-in.