Android多渠道打包以及分发策略
2017-01-19 18:07
183 查看
因为国内应用市场太多,我不推荐我使用的方法。
所以先附上比较适合国内开发者的方法。
(美团老版多渠道打包)
http://tech.meituan.com/mt-apk-packaging.html
(美团新版多渠道打包)
http://tech.meituan.com/android-apk-v2-signature-scheme.html
新版本主要是针对Android 7.0(Nougat)推出了新的应用签名方案APK Signature Scheme v2。
附上Github地址
https://github.com/Meituan-Dianping/walle
——————分割线——————-
因为现在暂时只面向Google Play就行,所以只考虑最简单的build版本。
编译完后在BuildConfig中应该有一个字段为CHANNEL,
对应在productFlavors的wandoujia的语句:
buildConfigField “String”, “CHANNEL”, “\”wandoujia\”“
(注意要有转义字符,不然会认为是变量)
不同渠道Apk可以采取这个策略来实现不同的操作
完美。
所以先附上比较适合国内开发者的方法。
(美团老版多渠道打包)
http://tech.meituan.com/mt-apk-packaging.html
(美团新版多渠道打包)
http://tech.meituan.com/android-apk-v2-signature-scheme.html
新版本主要是针对Android 7.0(Nougat)推出了新的应用签名方案APK Signature Scheme v2。
附上Github地址
https://github.com/Meituan-Dianping/walle
——————分割线——————-
因为现在暂时只面向Google Play就行,所以只考虑最简单的build版本。
详细步骤
修改app/build.gradle文件
附上app/build.gradle文件,详细看注释apply plugin: 'com.android.application' def releaseTime() { return new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+8")) } android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.foxmail.aroundme.labeltextview" minSdkVersion 14 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } //秘钥的位置,别名和密码,推荐保存到别处 signingConfigs { release { storeFile file("../gzl.jks") storePassword KEYSTORE_PASSWORD keyAlias "gzl" keyPassword KEY_PASSWORD } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } debug { // minifyEnabled true // shrinkResources true // proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } //多渠道种类,只是一个示例 productFlavors { //豌豆荚 wandoujia { //AndroidManifest.xml中定义的meta-data标签, // 需要和meta-data中的android:value="${CHANNEL}对应 manifestPlaceholders = [CHANNEL: "wandoujia"] //Build中生成一个字符用来区分不同渠道的不同策略实现 buildConfigField "String", "CHANNEL", "\"wandoujia\"" } //百度 baidu { manifestPlaceholders = [CHANNEL: "baidu"] buildConfigField "String", "CHANNEL", "\"baidu\"" } //其他渠道 } //重命名apk文件 def String apkName = "Alpha" + "_" + defaultConfig.versionName + "_" + defaultConfig.versionCode + "_dev_" + releaseTime() as String applicationVariants.all { variant -> if (variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) { variant.outputs.each { output -> def appName = 'Label' def oldFile = output.outputFile def buildName = '' variant.productFlavors.each { product -> //循环取到的是productFlavors的每个名字,也是区分名字的关键 buildName = product.name } def releaseApkName = appName+ "_" + buildName + "_${apkName}.apk" output.outputFile = new File(oldFile.parent, releaseApkName) } } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.1' testCompile 'junit:junit:4.12' compile project(':library') }
修改
然后在AndroidManifest中添加<meta-data android:name="UMENG_CHANNEL" android:value="${CHANNEL}" /> <application ....... />
编译完后在BuildConfig中应该有一个字段为CHANNEL,
对应在productFlavors的wandoujia的语句:
buildConfigField “String”, “CHANNEL”, “\”wandoujia\”“
(注意要有转义字符,不然会认为是变量)
修改应用内代码
Java代码中添加if(BuildConfig.CHANNEL.equals("wandoujia")) { Log.d("msg", "wandoujia"); Toast.makeText(MainActivity.this, "wandoujia", Toast.LENGTH_LONG).show(); } else if (BuildConfig.CHANNEL.equals("baidu")) { Log.d("msg", "baidu"); Toast.makeText(MainActivity.this, "baidu", Toast.LENGTH_LONG).show(); }
不同渠道Apk可以采取这个策略来实现不同的操作
完美。
相关文章推荐
- android gradle 多渠道打包分发
- Android Studio2.3关于多渠道打包失败以及多渠道打包流程
- android app多渠道分发打包
- Android应用打包以及多渠道打包
- 用Android studio多渠道打包以及使用不同资源(包括icon或者其他图片等) 方法二
- Android多渠道打包以及发布方法
- Ant_android 多渠道自动打包(1)
- android 多渠道打包
- 使用ANT编译--实现外部传参(多渠道替换)、批处理打包------第二步什么是ANT,如何使用ANT命令为Android项目打包
- Android多渠道打包工具
- python: android批量多渠道打包
- Android Library Project 的使用小结以及脚本打包事项
- Android:Ant 批量多渠道打包
- android技巧:apk文件反编译以及签名打包
- android 多渠道打包
- (转载自eoe论坛)Android上百实例源码分析以及开源分析集合打包
- Android_Ant_多渠道打包/一键自动编译 ——Mac lion 10.8.2下
- 脚本ANT自动打包Android应用(方便多渠道发布)
- android技巧:apk文件反编译以及签名打包(dex2jar&jd,apktool,apk-sign)
- [转]Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包