1.3.6 detailed explanation build.gradle File – Android first line of code (2nd Edition) notes

Time:2020-11-15

Unlike eclipse, Android studio uses gradle to build a project. Gradle is a very advanced project building tool that uses a groovy based domain specific language (DSL) to declare project settings.
First look at the outermost directory of the project build.gradle File, code as follows:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

The code is generated automatically,
In the first two places, jcenter () is declared in the closure of repositories. Jcenter is a code hosting repository.
Next, declare a gradle plug-in using classpath in the dependencies closure. Since gradle was not developed specifically for building Android projects, we need to state that if we want to use it to build Android projectscom.android.tools.build:gradle:3.1.3This plug-in. The last part is the version number.

Let’s take a look at the app directory build.gradle File, code as follows:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.0'
    defaultConfig {
        applicationId "com.lowthink.helloworld"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

In the first line, a plug-in is applied. Generally, there are two values to choose from:

  • com.android.application Indicates that this is an application module,
  • com.android.library Indicates that this is a library module.

The biggest difference between application module and library module is that one can be run directly and the other can only be run as code library attached to other application modules.


Android closure: in this closure, we can configure various properties of the project build.

  • compileSdkVersion: used to specify the build version of the project. 28 is specified here to indicate that the SDK of Android 9.0 system is used for compilation.
  • buildToolsVersion: used to specify the version of the project building tool. The latest version is 28.0.0. If there is an updated version, Android studio will prompt you. There are two ways to modify buildtoolsversion,

The first one is directly in the app build.gradle Add

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.0'
    defaultConfig {}
    buildTypes {}
}

The second method is to select the project, right-click, open module settings, and directly change the version of buildtools, as shown in the figure

1.3.6 detailed explanation build.gradle File - Android first line of code (2nd Edition) notes

1.3.6 detailed explanation build.gradle File - Android first line of code (2nd Edition) notes

You can also click the picture in the upper right corner, as shown in the figure

1.3.6 detailed explanation build.gradle File - Android first line of code (2nd Edition) notes

  • Defiltconfig closure: more details of the project can be configured. Among them:
    applicationId: the package name used to specify the project;
    minSdkVersion: used to specify the minimum compatible Android system version of the project. If it is set to 15, it indicates the minimum compatible to Android 4.0 system;
    targetSdkVersion: the specified value indicates that you have been fully tested on the project version, and the system will enable some of the latest features and features for your application;
    versionCode: used to specify the version number of the project;
    versionName: used to specify the version name of the project;

Correspondence between API level and Android version

API Level Android version
28 9
27 8.1
26 8.0
25 7.1.1
24 7.0
23 6.0
22 5.1.1
21 5.0.1
20 4.4w.2
19 4.4.2
18 4.3.1
17 4.2.2
16 4.1.2
15 4.0.3
14 4.0
10 2.3
8 2.2

Buildtypes closure: used to specify the configuration for generating installation files. Usually, there are only two self closing packages, one is debug and the other is release.

  • Debug closure: used to specify the configuration to generate the beta installation file;
  • Release closure: used to specify the configuration to generate the official installation file;
    Minifyenabled: used to specify whether to obfuscate the code of the project. True means obfuscation and false means no obfuscation;
    Proguardfiles: used to specify the rule files to be used in case of confusion. Here, two files are specified, the first Proguard- android.txt It is under the Android SDK directory, which contains the common confusion rules for all projects, the second Proguard- rules.pro It is in the root directory of the current project, in which you can write the confusion rules specific to the current project.

Note: the test version installation files are generated by running the project directly through andorid studio;
Note: debug closure can be ignored;


  • Dependencies closure: it can specify all the dependencies of the forward project.

Generally, Android studio projects have three dependency modes: local dependency, library dependency and remote dependency;

*Local dependency: dependency can be added to the local jar package or directory;
*Library dependency: you can add dependencies to library modules in the project;
*Remote dependency: you can add dependencies to open source projects on jcenter library;

Note: implementation will be used instead of compile in 2018

  • The first line of implementation filetree is a local dependency declaration, which means that all. Jar suffix files in the LIBS directory are added to the project’s build path.
  • The second line, implementation, is the remote dependency declaration, com.android.support : appcompat-v7:28.0.0-alpha 3 is a standard remote dependency library format, where com.android.support Is the domain name part, which is used to distinguish from other company libraries: appcompat-v7 is the group name, which is used to distinguish different libraries in the same company; 28.0.0-alpha 3 is the version number, which is used to distinguish from the same library Different versions should be distinguished. After adding this statement, gradle will first check whether there is a local cache of this library when building the project. If not, it will download it automatically online, and then add it to the construction path of the project.
  • The third line of codeimplementation 'com.android.support.constraint:constraint-layout:1.1.2'For the use of constraintlayout (for more information, please move here)
  • The fourth line of code testimplementation is used to declare the test case library;

This article is published by openwrite, a blog multi post platform!