Android Studio Gradle file parsing, androidgradle
This article is reposted from stormzhang's blog
What is Gradle? Gradle is a dependency management tool based on the Groovy language and mainly for Java applications. It discards various complicated configurations based on XML, instead, it is a Groovy-based internal domain-specific (DSL) language.
The basic concepts of Gradle are described in detail in my open-source project 9GAG. The Gradle-related knowledge includes the following files:
The red tag is analyzed step by step from top to bottom:
1. 9GAG/app/build. gradle
This file is the gradle configuration file of the Module in the app folder. It can also be regarded as the main gradle configuration file of the entire project. Let's take a look at the content of this file:
// Declare the Android program to apply plugin: 'com. android. application 'android {// compiled SDK version compileSdkVersion 21 // build tools version buildToolsVersion "21.1.1" defaultConfig {// application package name applicationId "me. storm. ninegag "minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName" 1.0.0 "} // java compileOptions {sourceCompatibility JavaVersion. version_00007 targetCompatibility JavaVersion. version_00007} buildTypes {debug {// debug mode} release {// whether to confuse minifyEnabled false // proguardFiles getdefaproproguardfile('proguard-android.txt '), 'proguard-rules.txt '} // remove the error lintOptions {abortOnError false} dependencies {// compile all jar packages in the libs directory compile fileTree (dir: 'libs', include: ['*. jar ']) compile 'com. android. support: support-v4: 21.0.2 'compile 'com. etsy. android. grid: library: 1.0.5 'compile 'com. alexvasilkov: foldable-layout: 1.0.1 '// compile the compile project (': extras: ShimmerAndroid ') of the ShimmerAndroid module under the extras directory ')}
Here, we need to describe the following points:
- Apply plugin at the beginning of the file is written in the latest gradle version. In the past, it was written in the apply plugin: 'android' format. If it is still written in the past, correct it.
- BuildToolsVersion requires you to install it locally. Many people import new third-party libraries. One of the reasons for the failure is that the build version is incorrect, you can manually change the version to your local version or open the SDK Manager to download the corresponding version.
- ApplicationId indicates the package name of the application, which is also the latest method.
- Jdk1.7 can be compiled only after android 5.0 is installed by default. However, since jdk 1.6 is installed in the mac system, you need to manually download jdk1.7 and configure it, for more information, see install and manage Java on Mac in my blog.
- MinifyEnabled is also the latest syntax. It was a long time ago runProguard, which also needs to be updated.
- ProguardFiles contains two sections. The first part represents the default android program obfuscation file. This file already contains the basic obfuscation statement, which saves us a lot of trouble, the Directory of this file is in **/tools/proguard/proguard-android.txt **, the latter part is the custom obfuscation file in our project, the directory is in ** app/proguard-rules.txt **, if you use Studio 1.0 to create a new project with the default generated file name is ** proguard-rules.pro **, this name does not matter, in this file you can declare some third-party dependency obfuscation rules, because it is an open-source project, 9GAG is not obfuscated. The specific obfuscation syntax is not covered in this blog. The final obfuscation result is that these two files work together.
- The compile project (': extras: ShimmerAndroid') line exists because other modules exist in 9GAG. If you do not know the concept of a Module, you can refer to tutorial 2-Basic settings and running of this blog Android Studio series, in short, you can understand it as Android Library. Due to the popularity of Gradle and the improvement of remote warehouses, this dependency will gradually become very uncommon, but you need to know the dependency.
The content in the above files is only basic configuration. In fact, there are many custom parts, such as automatic packaging of debug, release, beta, and other environments, signatures, and multi-channel packaging. They will be explained separately in the future.
2. 9GAG/extras/ShimmerAndroid/build. each Module of gradle requires a gradle configuration file with the same syntax. The only difference is that the application plugin: 'com. android. library'
3. 9GAG/gradle this directory has a wrapper folder, which can see two files, we mainly look at the content of the file gradle-wrapper.properties:
#Thu Dec 18 16:02:24 CST 2014distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
We can see that the gradle directory and download path and the gradle version used in the current project are declared. These default paths will not be changed, the incorrect gradle version specified in this file is also one of the reasons why many export packages fail.
4. 9GAG/build. gradle is the basic configuration file of the entire project. Let's take a look at the content here.
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.0.0' }}allprojects { repositories { jcenter() }}
The content mainly includes two aspects: one is to declare the source of the Repository. here we can see that it is the specified jcenter (), and the previous version is mavenCentral (), jcenter can be understood as a new central remote warehouse, compatible with maven central warehouse, and with better performance. The other is to declare the version of android gradle plugin. The official version of android studio 1.0 must support the version of gradle plugin 1.0.
5. The 9GAG/settings. gradle file is a global project configuration file, which mainly declares the modules that need to be added to gradle. Let's take a look at the content of 9GAG:
include ':app', ':extras:ShimmerAndroid'
The app, extras: ShimmerAndroid in the file are all modules. If there are other modules, they must be added in the above format.