android {
defaultConfig {
flavorDimensions ""
/*
manifestPlaceholders是一个数组变量,赋值的时候我们应该把所有placeholder组成一个数组赋值给manifestPlaceholders。
*/
manifestPlaceholders = [
WHOAREYOU : "第一个值",
RENZHENMING: "我是第二个值"
]
}
buildTypes {
release {
//配置之后会在类BuildConfig中生成一个变量LOG_DEBUG
buildConfigField "boolean", "LOG_DEBUG", "false"
//去除无效资源,也就是没有被引用的文件,需要配合mififyEnable使用,只有当两者都为true的时候才会起到真正的删除无效代码和无引用资源的目的
shrinkResources true
//开启代码混淆
useProguard true
//signingConfig signingConfigs
//压缩代码
minifyEnabled true
//执行混淆规则文件位置
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"
shrinkResources false
useProguard false
//signingConfig signingConfigs
//minifyEnabled true,useProguard false 表示只压缩代码不进行混淆
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' }
}
//配置apk的输出名称
//升级gradle到3.0.1的时候(3.0以上),app.gradle报了如下错误:Cannot set the value of read-only property
// ‘outputFile’ for ApkVariantOutputImpl_Decorated,即outputFile只是可读的
/*applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def filename
if (outputFile != null && outputFile.name.endsWith(".apk")) {
if (variant.buildType.isDebuggable()) {
// 输出apk名称为app__huawei_xxx.apk 2017/7/24 14:11
filename = "app_${variant.productFlavors[0].name}_debug_${defaultConfig.versionCode}.apk"
} else {
filename = "app_${variant.productFlavors[0].name}_release_${defaultConfig.versionCode}.apk"
}
output.outputFile = new File(outputFile.parent, filename)
}
}
}*/
//gradle 3.0以上的正确配置
//Android plugin 3.0 建议使用
//Use all() instead of each()
//Use outputFileName instead of output.outputFile if you change only file name (that is your case)
applicationVariants.all { variant ->
variant.outputs.all {
def filename
def createTime = new Date().format("MM-dd-HH:mm:ss", TimeZone.getTimeZone("GMT+08:00"))
if (outputFile != null && outputFile.name.endsWith(".apk")) {
if (variant.buildType.isDebuggable()) {
// 输出apk名称为app__huawei_xxx.apk 2017/7/24 14:11
filename = "app_${variant.productFlavors[0].name}_debug_versionCode${defaultConfig.versionCode}_${createTime}.apk"
} else {
filename = "app_${variant.productFlavors[0].name}_release_versionCode${defaultConfig.versionCode}_${createTime}.apk"
}
//指定生成的apk的名字
outputFileName = filename
//指定生成的apk的路径
variant.getPackageApplication().outputDirectory = new File("apks/")
}
}
}
/**
* 签名文件的存放与配置
*/
signingConfigs {
huawei {
storeFile file(storeFile_)
storePassword storePassword_
keyAlias keyAlias_
keyPassword keyPassword_
}
tengxun {
storeFile file(storeFile_)
storePassword storePassword_
keyAlias keyAlias_
keyPassword keyPassword_
}
}
productFlavors {
// All flavors must now belong to a named flavor dimension
//这里配置一定要在defaultConfig中配置flavorDimensions,哪怕是空的
//也能编译通过,必须有
huawei {
manifestPlaceholders = [APP_NAME : "@string/app_name_huawei",
APP_CHANNEL: "huawei",
APP_ICON : "@mipmap/ic_launcher_huawei"]
}
tengxun {
manifestPlaceholders = [APP_NAME : "@string/app_name_tengxun",
APP_CHANNEL: "tengxun",
APP_ICON : "@mipmap/ic_launcher"]
}
}
}