//让gradle 引入构建安卓app的插件
apply plugin: 'com.android.application' //自定义变量, 使用的时候不需要 ext 前缀
ext {
minSdkVersion = 15
prop2 = "foo"
} //自定义变量
def customProp2 = ["targetSdkVersion":23, "prop2":"bar"]; //自定义变量
// 根据日期自动生成build号
def calendar = Calendar.getInstance();
def tt = String.format("%d%02d%02d%02d",
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH)+1,
calendar.get(Calendar.DAY_OF_MONTH),
calendar.get(Calendar.HOUR_OF_DAY)); // 读取local.properties文件
def Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream()) android {
compileSdkVersion 24
buildToolsVersion "24.0.0" //签名选项
signingConfigs {
demoSignCfg {
keyAlias PROPERTY_FROM_GRADLE.PROPERTIES
//读取配置
keyPassword properties.getProperty("key.password")
storeFile file('demo.keystore')
storePassword properties.getProperty("key.password")
}
} //编译选项
compileOptions {
//使用jdk1.8 编译
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
} //代码检查选项
lintOptions {
//检查发布构建
checkReleaseBuilds rootProject.ext.checkReleaseBuilds
//遇到错误停止
abortOnError false
} //打包选项
packagingOptions {
//去除的文件
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
} //资源打包选项
aaptOptions {
//不压缩的文件
noCompress 'foo', 'bar'
//过滤文件
ignoreAssetsPattern "!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"
} //编译dex选项
dexOptions {
//堆栈内存最多4g
javaMaxHeapSize "4g"
} //配置
configurations {
//去掉所有的 com.android.support:support-annotations 依赖
all*.exclude group: 'com.android.support', module: 'support-annotations';
} //默认全局配置选项
defaultConfig {
applicationId "com.example.gradle_test"
minSdkVersion customProp.minSdkVersion
targetSdkVersion customProp2.targetSdkVersion
//使用生成的版本号
versionCode Integer.parseInt(tt)
versionName "1.0" //Manifest 里用的占位符: <... android:label="${activityLabel}" />
manifestPlaceholders = [ activityLabel:"defaultName"]
//设置BuildConfig 字段
buildConfigField "String", "BASE_URL", '"https://www.baidu.com/"'
} //构建变种, flavor 和 defaultConfig类型相同
productFlavors {
//变种1
flavor1 {
packageName "com.example.flavor1"
versionCode 20
//覆盖defaultConfig的字段
buildConfigField "String", "BASE_URL", '"https://www.baidu.com/"'
} flavor2 { }
} //配置各种目录
sourceSets {
//主要
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
} //测试资源路径
instrumentTest.setRoot('tests') debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
} //构建类型
buildTypes {
//debug类型(只是个名字而已)
debug {
//app id 后缀: com.example.app.debug
applicationIdSuffix ".debug"
} //发布类型(只是个名字而已)
release {
//关闭混淆
minifyEnabled false
//使用的混淆文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //签名选项
signingConfig signingConfigs.demoSignCfg
//移除无用资源
shrinkResources true //版本名后缀
versionNameSuffix ".0" //指定打包文件名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
//输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "apk_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
} //过滤abi库
ndk {
//只打包如下平台的so
abiFilters "x86", "armeabi-v7a", "armeabi", 'mips'
}
} //自定义
jnidebug {
// 继承自上面的debug.
initWith debug applicationIdSuffix ".jnidebug"
jniDebuggable true
}
} //依赖仓库
repositories {
maven { url "https://jitpack.io" }
}
} //依赖管理
dependencies {
//编译/运行时依赖
compile fileTree(dir: 'libs', include: ['*.jar'])
//$rootProject.ext 指的是 项目的那个build.gradle里面定义了一个ext 变量
compile ("com.android.support:design:$rootProject.ext.SupportVersion"){
//不引用如下包
exclude module: 'support-v4'
exclude module: 'appcompat-v7'
//不传递引用
transitive false
}
compile (project(":libray")){
//不引用如下包
exclude module: 'support-v4'
//不传递引用
transitive false
}
//测试依赖 testCompile 'junit:junit:4.12' //外部提供, 不打包 provided 'com.android.support:appcompat-v7:23.4.0' //falvor1需要的依赖, f1Compile, f1Provided..... flavor1Compile 'com.android.support:appcompat-v7:24.0.0' } //清理构建后资源的task task clean(type: Delete) { delete rootProject.buildDir }
gradle android dsl 属性大全 http://google.github.io/android-gradle-dsl/current/index.html
以上配置如有错误, 还请指出