laitimes

Thoroughly understand the differences and connections between Gradle, Gradle Wrapper, and Android Plugin for Gradle

Welcome to exchange technical related questions:

Email: [email protected]

Blog Park Address: http://www.cnblogs.com/jiangxinnju

GitHub Address: https://github.com/jiangxincode

Zhihu Address: https://www.zhihu.com/people/jiangxinnju

First, in an easy-to-understand but not very professional passage, describe the concepts, differences, and connections between the three.

  • Gradle is a build system that simplifies your compilation, packaging, and testing processes. Students who are familiar with Java can compare Gradle to Maven.
  • The role of Gradle Wrapper is to simplify the installation and deployment of Gradle itself. Different versions of the project may require different versions of Gradle, manual deployment is more cumbersome, and may cause conflicts, so you need Gradle Wrapper to help you with these things. Gradle Wrapper is part of the Gradle project.
  • Android Plugin for Gradle is a collection of Gradle plugins for Android development, mainly developed by Google's Android team, Gradle is not an exclusive build system for Android, but with Android Plugin for Gradle, you will find it especially simple to build Android projects with Gradle.

Another point to note is that Gradle, Gradle Wrapper and Android Plugin for Gradle don't have to be used with Android Studio, you can completely break away from Android Studio and use all three to build Android projects independently. Here are the official guidance documents for the three (as can be seen from the address, Gradle Wrapper is part of the Gradle project):

  • Gradle: https://docs.gradle.org/current/userguide/userguide_single.html
  • Gradle Wrapper: https://docs.gradle.org/current/userguide/gradle_wrapper.html
  • Android Plugin for Gradle: https://developer.android.com/studio/build/index.html

In order to deepen everyone's understanding of the three, let's talk about the relationship between the three in the actual project construction, as mentioned before, the three can be used independently of Android Studio, but this situation is rare in the actual development scenario, so this article still uses Android Studio as a development tool to introduce. When we create a new Android project, a directory structure similar to the one shown in the following figure appears:

Thoroughly understand the differences and connections between Gradle, Gradle Wrapper, and Android Plugin for Gradle

You can see a gradle/wrapper directory with two files: gradle-wrapper.jar/gradle-wrapper.properties, and the gradle-wrapper .jar is the main feature package of the gradle wrapper. Gradle-wrapper is generated during Android Studio installation.jar (if installed by default).

C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle\wrapper\gradle\wrapper\gradle-wrapper.jar

)。 Then, each time you create a new project, you'll copy the gradle-wrapper .jar to your project's gradle/wrapper directory. The gradle-wrapper.properties file mainly specifies what version of Gradle is needed for the project, where to download the version of Gradle, and where to download it and put it, as shown in the following figure:

Thoroughly understand the differences and connections between Gradle, Gradle Wrapper, and Android Plugin for Gradle

Where GRADLE_USER_HOME generally refers to

~/.gradle

From the illustrated project, you can know that I want to use the gradle-4.1 version, from

https://services.gradle.org/distributions/gradle-4.1-all.zip

Download, download to local

~/.gradle/wrapper/dists

directory.

Sometimes due to network problems, the corresponding gradle version may not be downloaded properly, resulting in sync Project With Gradle Files failing, which can be reported as an error after the AS

~/.gradle/wrapper/dists

Find the directory for the gradle version, for example

~/.gradle/wrapper/dists/gradle-4.1-all/cdund22i8guosqylfo49op4dv

, then download the gradle-4.1-all .zip from the Internet and put it in this directory, and re-synchroniz project With Gradle Files.

Is it possible that the Gradle of each project must be downloaded through Gradle Wrapper, and can all projects share a Gradle? This is theoretically possible, but since Gradle itself doesn't necessarily maintain full compatibility, sharing a Gradle for old and new projects can sometimes run into unexpected problems. Specify the corresponding version of Gradle, and the setting method that is not downloaded through Gradle Wrapper is checked in the image below

Use local gradle distribution

, and specify Gradle home:

Thoroughly understand the differences and connections between Gradle, Gradle Wrapper, and Android Plugin for Gradle

After the Gradle version is downloaded, the mission of Gradle Wrapper is basically completed, and Gradle reads the build.gradle file, which specifies what version of Android Plugin for Gradle the project needs and where to download that version of Android Plugin for Gradle. As shown in the following figure:

Thoroughly understand the differences and connections between Gradle, Gradle Wrapper, and Android Plugin for Gradle

From the illustrated project, we can know that we are going to use version 3.0.1, download from Google and jcenter, so where to download to our local? It will be downloaded to

~\.gradle\caches\modules-2\files-2.1\com.android.tools.build

middle. Sometimes everyone network loading is not good, choose the following figure

Offline work

When the "No cached version of com.android.tools.build:gradle:xxx available for offline mode" issue may occur, you just need to download the corresponding version of Android Plugin for Gradle to your local

C:\Program Files\Android\Android Studio\gradle\m2repository\com\android\tools\build

Medium.

Thoroughly understand the differences and connections between Gradle, Gradle Wrapper, and Android Plugin for Gradle

Well, the relationship between the three is clear from the sample project. If you have any questions, you can leave me a message.

My blog is about to be moved and synchronized to the Tencent Cloud + community, inviting everyone to join in: https://cloud.tencent.com/developer/support-plan

Read on