Domain Specific Language (DSL) to describe and manipulate the build logic
Build files are Groovy based and allow mixing of declarative elements through the DSL and using code to manipulate the DSL elements to provide custom logic.
Built-in dependency management through Maven and/or Ivy.
Very flexible. Allows using best practices but doesn’t force its own way of doing things.
Plugins can expose their own DSL and their own API for build files to use.
Good Tooling API allowing IDE integration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
android {
compileSdkVersion 17
}
|
利用adt 22导出 build.gradle.
复制别人写好的build.gradle 文件.
根据gradle 规则,手写android 的build.gradle 文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
signingConfigs { myConfig{ storeFile file("gradle.keystore") storePassword "gradle" keyAlias "gradle" keyPassword "gradle" } } buildTypes{ release { signingConfig signingConfigs.myConfig } } |
gradle clean
gradle build
,这次在build/apk 你看到了多了一个[项目名]-release-unaligned, 从字面上面我就可以知道,这个只是没有进行zipAlign 优化的版本而已
。
而[项目名]-release 就是我们签名,并且zipAlign 的apk包了. ###打混淆包### 只需要在原来的基础上加上,完整的 proguad.gradle 代码
build.gradle
1 2 3 4 5 6 7 8 |
buildTypes{ release { signingConfig signingConfigs.myConfig runProguard true proguardFile 'proguard-android.txt' } } |
1 2 3 4 5 6 7 8 |
productFlavors { playstore { packageName='com.youxiachai.androidgradle.playstore' } hiapk { packageName='com.youxiachai.androidgradle.amazonappstore' } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } hiapk { manifest.srcFile 'hiapk/AndroidManifest.xml' } playstore { manifest.srcFile 'hiapk/AndroidManifest.xml' } instrumentTest.setRoot('tests') } |
gradle clean
,
gradle build
,省下的时间去喝杯咖啡,睡个觉什么的都好
。。。
###外部依赖### android gradle 对于外部jar 包的应用支持maven/ivy 管理的包,也支持指定具体文件,前面已经在上文说过
。
上面演示的完整 build.gradle gist 里面也有写
。
你需要加上如下代码即可:
build.gradle
1 2 3 |
dependencies { compile files('libs/android-support-v4.jar') } |
完整的Gradle Plugin User Guide 其中里面有个错误是compile files('libs/android-support-v4.jar')
不是compile file('libs/android-support-v4.jar')
教程是基于android gradle0.3 ,在0.4中只是多了混淆打包,这块已经在文中补充了。
一个德国人写的Android-Gradle-Examples
dependencies{}
更多的介绍。
debug, release,这两种类型的默认配置如下:
Property name | Default values for debug | Default values for release / other |
debuggable | true | false |
jniDebugBuild | false | false |
renderscriptDebugBuild | false | false |
renderscriptOptimLevel | 3 | 3 |
packageNameSuffix | null | null |
versionNameSuffix | null | null |
signingConfig | android.signingConfigs.debug | null |
zipAlign | false | true |
defaultConfig {} 配置参数列表
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 |
packageName | null | value from manifest if present |
testPackageName | null | app package name + “.test” |
testInstrumentationRunner | null | android.test.InstrumentationTestRunner |
signingConfig | null | null |
runProguard | false | false |
proguardFile | 'proguard-android.txt' or 'proguard-android-optimize.txt' | 'proguard-android.txt' or 'proguard-android-optimize.txt' |
build 结构目录
tree 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 |
build/ ├── apk ├── assets │ ├── debug │ └── release ├── classes │ ├── debug │ │ └── com │ │ └── example │ │ └── gradle │ └── release │ └── com │ └── example │ └── gradle ├── dependency-cache │ ├── debug │ └── release ├── incremental │ ├── aidl │ │ ├── debug │ │ └── release │ ├── dex │ │ ├── debug │ │ └── release │ ├── mergeAssets │ │ ├── debug │ │ └── release │ └── mergeResources │ ├── debug │ └── release ├── libs ├── manifests │ ├── debug │ └── release ├── res │ ├── all │ │ ├── debug │ │ │ ├── drawable-hdpi │ │ │ ├── drawable-mdpi │ │ │ ├── drawable-xhdpi │ │ │ ├── drawable-xxhdpi │ │ │ ├── layout │ │ │ ├── menu │ │ │ ├── values │ │ │ ├── values-sw720dp-land │ │ │ ├── values-v11 │ │ │ └── values-v14 │ │ └── release │ │ ├── drawable-hdpi │ │ ├── drawable-mdpi │ │ ├── drawable-xhdpi │ │ ├── drawable-xxhdpi │ │ ├── layout │ │ ├── menu │ │ ├── values │ │ ├── values-sw720dp-land │ │ ├── values-v11 │ │ └── values-v14 │ └── rs │ ├── debug │ └── release ├── source │ ├── aidl │ │ ├── debug │ │ └── release │ ├── buildConfig │ │ ├── debug │ │ │ └── com │ │ │ └── example │ │ │ └── gradle │ │ └── release │ │ └── com │ │ └── example │ │ └── gradle │ ├── r │ │ ├── debug │ │ │ └── com │ │ │ └── example │ │ │ └── gradle │ │ └── release │ │ └── com │ │ └── example │ │ └── gradle │ └── rs │ ├── debug │ └── release └── symbols ├── debug └── release 88 directories |