Android工程中有三个gradle格式的文件:
1. build.gradle(Project:appName)
这个文件是最顶层的,针对整个项目(project)的gradle构建文件.
// 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:2.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
```
>**buildscript**闭包里是构建过程Gradle自身所需要的依赖的仓库,它告诉Gradle到哪里下载**gradle**插件.默认是从**jcenter**[^1]下载,你也可以改成从**Maven**下载,只要把*dependencies*里的*classpath*修改成对应的**Maven**路径就可以了.
>**allprojects**闭包里表明项目*(project)*及其模块*(module)*都默认使用**jcenter()**来解析任何*Java*依赖库.
>**task clean**闭包是添加到整个项目顶层构建*(top-level build)*的一个任务[^2],后面的*type: Delete*指明这个任务是Gradle内嵌的Delete任务的一个自定义对象,它的作用是将该build路径从根项目默认的build文件夹中移除.
[^1]:Bintray JCenter Artifactory repository.
[^2]:gradle采用有向循环图 (DGA) 来解析不同任务(task)之间的关系,允许用户向DGA添加自定义任务.DGA:directed acyclic graph.
## 2. build.gradle(Module:app)
每个模块*(module)*都可以有自己的**build.gradle**文件,比如Studio为我们自动创建的**app**模块就有一个如下的Gradle构建文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion '24.0.3'
defaultConfig {
applicationId "com.yiheng.***"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
storeFile file(RELEASE_SRORE_FILE)
storePassword RELEASE_STORE_PASSWORD
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'org.xutils:xutils:3.3.36'
compile 'com.google.code.gson:gson:2.+'
}
> **apply plugin**这句是向Gradle构建系统添加Android插件,激活Android的**DSL**设置.这里的``com.android.application``说明这是一个应用程序模块,如果是``com.android.application``则表明是一个库模块.
> **android**闭包里是Android程序构建时的一些设置,比如配置sdk版本等,这一块下篇文章详细说.
>**signingConfigs**闭包里是对具体签名信息进行配置[^3].
[^3]:一般是将具体信息写到gradle.properties文件里,这里只是调用了对应的引用而已.
> ** buildTypes**闭包里是对release或者debug版本进行一些设置.
>>比如[^4]:
是否启用代码压缩: minifyEnabled.
定义 ProGuard 规则:proguardFiles.
签名设置: signingConfig.
[^4]:这里将debug版本的签名替换成了release版本,不做任何配置的话会采用默认的debug签名.
> **dependencies**闭包中的**compile filetree**这句意思是*libs*文件夹中所有以**.jar**结尾的文件都被添加到了编译类路径*(compile classpath)*.
>**testCompile**和**androidTestCompile**都是用于测试,区别是前者没有引用上下文,主要用于局部的单元测试,后者可以引用上下文,可以用于对整个apk进行全局测试.
**compile**后跟的都是添加的依赖库,Gradle会根据地址自动下载并添加.
>> *'com.android.support:appcompat-v7:24.2.1'这里的-v7是说最低支持的版本到Android 7,而不是指这个支持包本身的版本*
## 3. setting.gradle(Project Settings)
Android工程支持多个模块*(module)*的Gradle构建,这个文件显示模块包含在哪些子路径中,默认内容如下:
include ':app'
这个**include**声明说明只有一个模块,位于在**app**子路径下.
网友评论