android studio gradle plugin 用户指南 选择性翻译
原文
http://tools.android.com/tech-docs/new-build-system/user-guide
构建文件 build.gradle
Basic Project
1.最简单的配置:
apply plugin: 'java'
使用自带的java插件,用gradle编译java
2.最常见的android配置:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.11.1' } } apply plugin: 'android' android { compileSdkVersion 19 buildToolsVersion "19.0.0" }
repositories 使用maven central repository
dependencies 依赖android plugin
apply plugin 使用android plugin
android {} 配置android plugin 参数,这个事dsl的入口
提示:需要 local.properties 设置代表android sdk 路径的sdk.dir属性,或者使用 ANDROID_HOME 环境变量
项目结构
1.默认目录结构:
- src/main/ 放源码
- src/androidTest/ 放测试代码
2.java和android plugin 共有源码目录:
- java/
- resources/
3.android plugin 源码目录
- AndroidManifest.xml
- res/
- assets/
- aidl/
- rs/
- jni/
配置路径
修改java plugin默认路径的两种方式
sourceSets {
main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
sourceSets { main.java.srcDirs = ['src/java'] main.resources.srcDirs = ['src/resources'] }
将android plugin 的路径修改为旧的形式
android { sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } androidTest.setRoot('tests') } }
构建任务
常见任务
- assemble 合并输出
- check 运行所有检查
- build 运行 assemble 和 check
- clean 清空输出
这几个任务本身不做什么事,具体的事情要往里面插,类似策略模式
java任务
- assemble
- jar
This task creates the output.
- jar
- check
- test
This task runs the tests.
- test
jar 直接或者间接依赖于其他任务,比如classes 编译java代码
具体看http://gradle.org/docs/current/userguide/java_plugin.html
android任务
- assemble
The task to assemble the output(s) of the project - check
The task to run all the checks. - connectedCheck
Runs checks that requires a connected device or emulator. they will run on all connected devices in parallel. - deviceCheck
Runs checks using APIs to connect to remote devices. This is used on CI servers. - build
This task does both assemble and check - clean
This task cleans the output of the project
多了connectedCheck和deviceCheck检查设备连接
tip:gradle支持任务名简写:比如用gradle aR 代替gradle assembleRelease
自定义构建过程
Manifest entries
android { compileSdkVersion 19 buildToolsVersion "19.0.0" defaultConfig { versionCode 12 versionName "2.0" minSdkVersion 16 targetSdkVersion 16 } }
默认值
Property Name | Default value in DSL object | Default value |
versionCode | -1 | value from manifest if present |
versionName | null | value from manifest if present |
minSdkVersion | -1 | value from manifest if present |
targetSdkVersion | -1 | value from manifest if present |
applicationId | null | value from manifest if present |
testApplicationId | null | applicationId + “.test” |
testInstrumentationRunner | null | android.test.InstrumentationTestRunner |
signingConfig | null | null |
proguardFile | N/A (set only) | N/A (set only) |
proguardFiles | N/A (set only) | N/A (set only) |
依赖管理
依赖于本地包
dependencies {
compile files('libs/foo.jar')
}
android {
...
}
compile的文件都会加入classpath当中,并打包到apk
可选的compile配置
- compile: main application
- androidTestCompile: test application
- debugCompile: debug Build Type
- releaseCompile: release Build Type.
依赖于远程包
repositories { mavenCentral() } dependencies { compile 'com.google.guava:guava:11.0.2' } android { ... }
先把repository加进来,然后在dependencies以maven或者ivy的形式声明artifacts,就可以了
配置其他maven库
repositories { maven { url "http://repo.mycompany.com/maven2" } }
http://gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
多项目构建
示例:
app,lib1,lib2 都是gradle项目
gradle这么引用他们 :app :libraries:lib1 :libraries:lib2
每个项目都有build.gradle文件
根目录有settings.gradle
结构如下:
为了表示当前目录是一个gradle目录,需要有settings.gradle文件,内容如下:
include ':app', ':libraries:lib1', ':libraries:lib2'
app依赖其他项目,依赖关系这么写
dependencies {
compile project(':libraries:lib1')
}
library项目
上面 :libraries:lib1 :libraries:lib2可以是java项目,:app使用它们的jar包,
但是 如果这些库要访问android api 或者其他android资源,它们不能是普通的java项目,必须是Android Library Projects
创建Android Library Projects
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.5.6' } } apply plugin: 'android-library' android { compileSdkVersion 15 }
使用android-libraray plugin 其他跟普通android项目类似
输出.aar文件,包含编译代码(.jar/.so)和资源文件(manifest, res, assets)
引用Library
dependencies {
compile project(':libraries:lib1')
compile project(':libraries:lib2')
}
发布Library
android { defaultPublishConfig "debug" }
默认只发布release版本,可以通过上面代码修改
如果有变体(后面将提到),要加上变体名
android { defaultPublishConfig "flavor1Debug" }
发布所有版本
android {
publishNonDefault true
}
引用其他发布的artifact
dependencies { flavor1Compile project(path: ':lib1', configuration: 'flavor1Release') flavor2Compile project(path: ':lib1', configuration: 'flavor2Release') }
构建变体
变体:类似普通版和高级版
android {
....
productFlavors {
flavor1 {
...
}
flavor2 {
...
}
}
}
Build Type + Product Flavor = Build Variant
例如
- Flavor1 - debug
- Flavor1 - release
- Flavor2 - debug
- Flavor2 - release