简单的多渠道打包build.gradle模板
之前看过一些多渠道打包的方法,现在自己总结了下,感觉可以提取一个build.gradle模板出来.
模板有一下功能:
1.根据versionCode(版本号)来修改app名
2.通过manifestPlaceholders来修改AndroidMainfest中预先定义好的渠道号
build.gradle模板:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.duoqudao"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
signingConfigs {
release {//这里配置你的keyStore
storeFile file("yourJks.jks")
storePassword "passWord"
keyAlias "yourKeyAlias"
keyPassword "passWord"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//根据versionCode的修改apk名
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace(".apk", "-" + versionCode + ".apk")
)
}
}
}
debug {
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace("app-debug", "yourAppName" + versionCode)
)
}
}
debuggable true
}
}
//只需要将你想要打包的应用市场写在下面就可以了
productFlavors {
huawei {}
xiaomi {}
oppo {}
meizu {}
leshi {}
//这里很关键,下面代码会将打包的那个apk的名字通过占位符${CHANNEL_VALUE}赋给AndroidMainfest中
//<meta-data android:name="TD_CHANNEL_ID"
//android:value="${CHANNEL_VALUE}" />
//之后在代码中你可以通过这个CHANNEL_VALUE做很多事情,比如启动页展示应用市场的logo就需要通过CHANNEL_VALUE来判断
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}