Detailed three gradle files in Android project
Foreword
When using Android studio to develop Android projects, it is necessary to contact gradle, such as introducing dependencies, configuring signature information, and so on. When we create a new project, Android studio will help us create three Gradle files by default, one settings.gradle file, two build.gradle files, and the build.gradle file should not be placed in the root directory and module directory of the project project. Let's uncover the mystery of Gradle.
First look at the file directory structure of the new studio project:
settings.gradle
The role of settings.gradle is that after some modules are included, they need to be declared here:
include ': app'
include ': app', ': demo'
1
2
Multiple modules are separated by ",". The settings.gradle file will be executed during the initialization period.
build.gradle file in the project root directory
The gradle file is a common property of all modules defined in this project, and it contains two methods by default:
buildscript {
repositories {
google ()
jcenter ()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
}
}
allprojects {
repositories {
google ()
jcenter ()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
The buildscript method defines global related properties, and the repositories define jcenter as a repository. A repository represents the source of your dependencies, such as a maven repository. dependencies are used to define the build process. This means that you should not define the dependencies of submodules in this method body. You only need to define the default Android plugin, because this plugin allows you to execute related Android tasks.
The allprojects method can be used to define the default properties of each module. You can not only limit the default configuration. In the future, you can create tasks in the allprojects method body. These tasks will be visible in all modules.
build.gradle file in module directory
The gradle file in the module module only works for this module, and it can override any parameters from the gradle file in the root directory. When writing programs, sometimes we need mobile phones compatible with different system versions for real machine testing, so sometimes we need to modify the version number of the SDK or other things. At this time we need to understand the build.gradle file. The module file should look like this:
apply plugin: 'com.android.application' // Describes the type of module, com.android.application is the program, and the package is .apk. com.android.library is a library, and the package is .aar.
android
{
compileSdkVersion 25 // Compiled SDK version
buildToolsVersion "25.0.0" // Compiled Tools version
defaultConfig
{//default allocation
applicationId "com.test" // Application package name
minSdkVersion 14 // Minimum supported version
targetSdkVersion 22 // Supported target versions
versionCode 1 // version number
versionName "1.0.0" // version name
}
sourceSets {// directory points to configuration
main
{
manifest.srcFile 'AndroidManifest.xml' // Specify AndroidManifest file
java.srcDirs = ['src'] // Specify the source directory
resources.srcDirs = ['src'] // Specify the source directory
aidl.srcDirs = ['src'] // Specify the source directory
renderscript.srcDirs = ['src'] // Specify the source directory
res.srcDirs = ['res'] // Specify the resource directory
assets.srcDirs = ['assets'] // Specify the assets directory
jniLibs.srcDirs = ['libs'] // Specify the lib library directory
}
debug.setRoot ('build-types / debug') // Specify the path of debug mode
release.setRoot ('build-types / release') // Specify the path of the release mode
}
signingConfigs
{// Signing configuration
release
{// Release signature configuration
storeFile file ("E: /test.jks") // Key file path
storePassword "123" // Key file password
keyAlias "test" // key alias
keyPassword "123" // key password
}
debug
{// debug version signature configuration
storeFile file ("test.jks")
storePassword "123"
keyAlias "test"
keyPassword "123"
}
}
buildTypes
{// build type
release {// release
minifyEnabled true // Confusion is on
proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-project.txt' // Specify the obfuscation rules file
signingConfig signingConfigs.release // Set the signing information
}
debug {// debug
signingConfig signingConfigs.release
}
}
packagingOptions
{
exclude 'META-INF / ASL2.0'
exclude 'META-INF / LICENSE'
exclude 'META-INF / NOTICE'
exclude 'META-INF / MANIFEST.MF'
}
lintOptions
{
abortOnError false /// lint terminates error reporting to prevent inexplicable failure during compilation
}
// android studio 3.0 supports java8 features
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {// Set skip translation
disable 'MissingTranslation'
}
flavorDimensions "color"
productFlavors {
baidu {}
xiaomi {}
yyb {}
huawei {}
}
productFlavors.all {
flavor-> flavor.manifestPlaceholders = [ChannelName: name]
}
// Package apk rename
applicationVariants.all {variant->
SimpleDateFormat dateFormat = new SimpleDateFormat ("MM-dd")
def dateFlag = dateFormat.format (new Date ())
variant.outputs.all {
def flavorFlag = variant.flavorName
def newName = "yzb_" + dateFlag + "_v" + variant.versionName + "_" + variant.versionCode + "_" + flavorFlag + ".apk"
outputFileName = newName
}
}
}
dependencies
{
compile fileTree (dir: 'libs', exclude: ['android-support * .jar'], include: ['* .jar']) // Compile .jar files in the lib directory
compile project (': demo') // Compile additional project
// glide image loading frame
compile 'com.github.bumptech.glide: glide: 3.7.0'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
This is my understanding of Gradle in the Android project. If there is any deficiency, please leave a message to correct me, thank you ~~
————————————————
Copyright statement: This article is the original article of the CSDN blogger "fyjc_wlj", which follows the CC 4.0 BY-SA copyright agreement.