background

When installing Android Studio for the first time, many people will encounter a problem: How to open a project with Android Sutdio the first time? Gradle Project Sync Filded error

Ever wonder how androidstudio found our gardle?

The principle of

Gradle configuration is divided into several parts

1. Gardle download and configuration and principles 2. Gradle plug-in download and configuration and principles 3

We’ll focus on the first two this time

Gradle plugin and gradle version

The Android Studio build system is based on Gradle, and the Android plugin for Gradle adds several features that are specific to building Android apps. Although the Android plugin is typically updated in lock-step with Android Studio, the plugin (and the rest of the Gradle system) can run independent of Android Studio and be updated separately.

Gradle plugin version number follows androidStudio version number and gradle version number has certain corresponding relationship, hope everyone uses the latest version.

Gradle installation and configuration principles

How does androidstudio load our gradle?

1. When Android Studio opens a project, it first reads the gradle-wrapper.properties file, This will tell you which version of Gradle is required for the project, and then go to the GRADLE_USER_HOME folder where you save gradle to see if this version does not exist. 2. If it does not exist, go to distributionUrl to download it

The gradle-wrapper.properties file is involved, so let’s go over it first (you can skip it for details).

Gradle-wrapper. properties:

distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME ZipStorePath = wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zipCopy the code

distributionUrl

The distributionUrl is the address of the gradle you want to download. Change the gradle version you want to use here.

Gradle has three versions:

Gradle-xx-all. zip is the complete version, containing various binaries, source files, and offline documentation. For example, https://services.gradle.org/distributions/gradle-3.1-all.zip

Gradle-xx-bin. zip is a binary version and contains only binary files (executable files), no documentation or source code. For example, https://services.gradle.org/distributions/gradle-3.1-bin.zip

Gradle-xx-src. zip is the source version. It contains only gradle source code and cannot be used to compile your project. For example, https://services.gradle.org/distributions/gradle-3.1-src.zip

For compilation purposes only, you can use the binary version instead of the full version, for example, Gradle-3.1-bin.zip.

The other four properties

ZipStoreBase and zipStorePath, together, are the location where gradle-3.1-bin.zip is downloaded. ZipStorePath is a subdirectory of the directory specified by zipStoreBase.

Together, the distributionBase and distributionPath are the location of the file after decompressed Gradle-3.1-bin.zip. DistributionPath is a subdirectory of the directory specified by distributionBase.

The download location can be different from the decompression location.

ZipStoreBase and distributionBase can be GRADLE_USER_HOME or PROJECT.

GRADLE_USER_HOME indicates the user directory. On Windows it is %USERPROFILE%/.gradle, for example C:\Users<user_name>.gradle\. On Linux it is $HOME/.gradle, for example ~/.gradle.

PROJECT represents the current directory of the PROJECT, that is, the directory where GradLew resides.

Example: the overall meaning of each attribute

Example 1: Gradle-wrapper. properties

distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME ZipStorePath = wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zipCopy the code

The overall meaning of each attribute is as follows:

(1) go to https://services.gradle.org/distributions/gradle-3.1-bin.zip to download gradle version 3.1, contains only the binary version.

Gradle-3.1-bin. zip in C:\Users<user_name>. Gradle \wrapper\dists. (note: C:\Users<user_name>. Gradle \wrapper\dists\gradle-3.1-bin<url-hash>\ The directory name is computed from the MD5 value of the distribution URL path string. For details, see rootDirName() and getHash() in PathAssembler.java. For pathAssembler.java, see the reference path at the end of this article.

(3) Decompress gradle-3.1-bin.zip and store the decompressed file in C:\Users<user_name>.gradle\wrapper\dists. (Note: there is also a 2-level directory, as above)

The solution

Now that we know how it works, and that the reason why it’s slow to open is because we’re stuck downloading Gradle, let’s download Gralde ourselves. (1) Modify gradle-wrapper.properties. Change the distributionUrl in gradle-wrapper.properties to the version we want. For details, see the link below:

Gradle download address: https://services.gradle.org/distributions/

At this time, AS will automatically download Gradle and create a directory. At this point, simply kill Android Studio and exit, AS this step is to get to the download directory automatically created by AS. Go to the following directory: Linux:

~/.gradle/wrapper/dists

windows:

C:\users{user name}.gradle\wrapper\dists

(2) Download Gradle from the above gradle download address, download the corresponding version of Gradle, gradle In this example, it is gradle-2.4-all.zip, and then delete gradle-2.4-all.zip. Part file in the preceding figure, and copy the downloaded gradle-2.4-all.zip to the folder with a string of garble in the preceding figure, be careful not to decompress. So we skip the step of downloading Gradle.

(3) Restart Android Studio, open the project, OK, suddenly open.

Androidstudio gradle Settings will also affect gardle loading

Option by option

  • If Use default Gradle Wrapper (recommended) is selected, the gradle location is set to the path in Service Directory path.

  • If Use local Gradle Distribution is selected, the gradle location is the path in the Gradle Home.

Note: Service Directory path is global, Use default Gradle Wrapper (recommended) and Use local Gradle distribution are project level and have a higher priority than global Settings.

GRADLE_USER_HOME is the default value of the Service directory path in gradle-wrapper.properties file. GRADLE_USER_HOME can be set. On Windows it is %USERPROFILE%/.gradle, for example C:\Users<user_name>.gradle\. On Linux it is $HOME/.gradle, for example ~/.gradle

Offline work is used after a successful compilation to speed up compilation (provided that there are no local dependencies that need to be loaded).

Loading sequence (debatable, welcome)

1. Determine gradle version 2 according to gradle-wrapper.properties. If Use default Gradle Wrapper (recommended) is selected, the gradle-wrapper. Properties file is loaded by default: The gradle-wrapper.properties file is read first to know which version of Gradle is required for the project, and then it is checked in the GRADLE_USER_HOME folder to see if this version of Gradle does not exist. 3. Select Use local Gradle Distribution and load it directly from this directory

Gardle plug-in

Gradle plugin and Gradle are two separate things. The gradle plugin version is determined by the build.gradle file in the outermost layer of the project

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 2.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
Copy the code

After modifying the gradle plugin version, sync the code

There is a mapping between the Android Studio Gradle plugin version and the Gradle version.

Gradle plugin versions are typically inherited from Android Studio versions

Go to the official website

Suggestion: Do not write version number +

To use

conclusion

Gardle installation and configuration is every Android programmer will encounter, but more or less there will be some confusion, to clarify the logic behind them, for our development has a great help.

Reference: https://blog.jetbrains.com/idea/2013/04/gradle-improvements-at-121/ https://blog.csdn.net/u013553529/article/details/55011602 https://www.jianshu.com/p/33089ca7c98e https://blog.csdn.net/fuchaosz/article/details/51567808

Reprint please indicate the original address!