gradle的多渠道打包很简单,因为gradle已经帮我们做好了很多基础功能。
下面以友盟统计为例说明,一般友盟统计在AndroidManifest.xml里面会有这么一段声明:
<meta-data android:name="UMENG_CHANNEL" android:value="CHANNEL_ID" />
其中CHANNEL_ID就是友盟的渠道标示,多渠道的实现一般就是通过修改CHANNEL_ID值来实现的。
接下来将一步一步来实现多渠道版本打包。
1.在AndroidManifest.xml里配置PlaceHolder,用与在build.gradle文件中来替换成自己想要设置的值
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
2.在build.gradle设置productFlavors,修改PlaceHolder的值
productFlavors { playStore { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "playStore"] } miui { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "miui"] } wandoujia { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"] } }
或者批量修改
productFlavors { playStore {} miui {} wandoujia {} } //批量处理 productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }
按照上面两步即可编译打多渠道包了,命令是 ./gradlew assembleRelease,可以打包所有的多渠道包。
如果只是想打单渠道包,则执行相应的task即可,如gradle assemblePalyStoreRelease就是打PlayStore渠道的Release版本。
3.如果希望可以对最终的文件名做修改,如需要针对不同的需求生成不同的文件。而修改文件名也很简单,参考以下代码即可实现
def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) } android{ applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { File outputDirectory = new File(outputFile.parent); def fileName if (variant.buildType.name == "release") { fileName = "app_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk" } else { fileName = "app_v${defaultConfig.versionName}_${packageTime()}_debug.apk" } output.outputFile = new File(outputDirectory, fileName) } } } }
此方法有一定局限性,就是渠道包多了之后编译花费的时间会很长,这里推荐美团打包的第三种方法。
完整的gradle脚本
// 声明是Android程序 apply plugin: 'com.android.application' // 定义一个打包时间 def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) } android { // 编译SDK的版本 compileSdkVersion 21 // build tools的版本 buildToolsVersion '21.1.2' defaultConfig { // 应用的包名 applicationId "com.**.*" minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" // dex突破65535的限制 multiDexEnabled true // 默认是umeng的渠道 manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"] } // 移除lint检查的error lintOptions { abortOnError false } //签名配置 signingConfigs { debug { // No debug config } release { storeFile file("../yourapp.keystore") storePassword "your password" keyAlias "your alias" keyPassword "your password" } } buildTypes { debug { // buildConfigField 自定义配置默认值 buildConfigField "boolean", "LOG_DEBUG", "true" buildConfigField "String", "API_HOST", "\"http://api.test.com\""//API Hos versionNameSuffix "-debug" minifyEnabled false //是否zip对齐 zipAlignEnabled false shrinkResources false signingConfig signingConfigs.debug } release { // buildConfigField 自定义配置默认值 buildConfigField "boolean", "LOG_DEBUG", "false" buildConfigField "String", "API_HOST", "\"http://api.release.com\""//API Host //// 是否进行混淆 minifyEnabled true zipAlignEnabled true // 移除无用的resource文件 shrinkResources true //混淆规则文件 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { // 输出apk名称为boohee_v1.0_2015-06-15_wandoujia.apk def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk" output.outputFile = new File(outputFile.parent, fileName) } } } } } // 友盟多渠道打包 productFlavors { wandoujia {} _360 {} baidu {} xiaomi {} tencent {} taobao {} ... } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } } dependencies { // 编译libs目录下的所有jar包 compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:support-v4:21.0.3' compile 'com.jakewharton:butterknife:6.0.0' ... }