在Android开发中,确定少不了要打多渠道包。Umeng有提供一些多渠道打包的方式,可是太慢了,每一个渠道都要重复的去打,若是有50多个渠道,就须要编译50乘以1个包编译的时间。So,确定是不可能这样干的,这个方案只适合一些渠道比较少的。因此个人项目里采用的美团的多渠道打包的方式。java
具体的能够移动至个人另外一篇文章:Android美团多渠道打包Walle集成git
就如标题所示,原本万事大吉了,能够一键生成多渠道包。**但是等到发布的时候测试和我说,渠道信息没有了,What???**而后网上各类翻资料,去Walle项目看看有没有相似的问题。不出所料,Walle官网也给出了缘由和具体的解决方案。github
360加固致使渠道信息丢失问题解决方案shell
但是这个方案,太蛋疼了哥。我每次都要手动的打Release包,而后上传360加固,下载加固包。而后本地跑一次Phtyon脚本生成渠道包。请告诉我这个操做蛋疼不?缓存
有没有啥办法能够在Android Studio里也一键生成多渠道包而且渠道信息也不会丢失呢?固然是有的,本身动手丰衣足食。bash
把上述这些步骤所有写在一个Gradle脚本里,主模块Gradle apply本身的写的脚本。markdown
(1)配置加固文件夹 把从360加固官网下载的window下载(MAC环境就MAC下载)里的内容有个jiagu文件夹,复制到工程目录,而且更名为reinforce,这里随意我是这样命名的。而后把美团的walle-cli-all.jar复制到reinforce文件夹里lib文件夹下。 app
baidu #百度
xiaomi # 小米
anzhi # 安智
meizu # 魅族
vivo # vivo
oppo # oppo
huawei # 华为
sougou # 搜狗
samsung # 三星
lianxiang # 联想
jinli # 金立
taobao #淘宝
yingyongbao #应用宝
360 #360
anzhi #安智
guanwang #官网
复制代码
或者直接下我弄好的:(Window) 连接: pan.baidu.com/s/1jw_0Wya4… 提取码: igx9oop
(1) Window下的脚本测试
ext { //加固插件路径 reinforce_plugin_path = '../reinforce' //360加固帐号密码 reinforce_plugin_name = 'XXXXX' reinforce_plugin_passward = 'XXXXXX' //签名信息 key_store_path = './xyz.keystore' key_store_passward = 'XXXXX' alias = 'XXXXXX' alias_passward = 'XXXXX' //加固ApK输出路径 reinforce_apk_path = 'build/outputs/apk/release/reinforce/' //加固包名称 reinforce_apk_name = 'build/outputs/apk/release/reinforce/release_encrypted_aligned_signed.apk' //渠道配置文件 chanel_config_path = reinforce_plugin_path + '/channel.txt' //渠道Apk输出路径 channel_apks_path = 'build/outputs/apk/release/channels/' } /** * 注释:编译加固渠道包 * 时间:2019/1/2 0002 14:01 * 做者:郭翰林 */ task buildReinforceRelease() { group '360reinforce' dependsOn('assembleRelease') doLast { //第一步:清空缓存 cleanFilesPath(reinforce_plugin_path + "/.cache") cleanFilesPath(reinforce_plugin_path + "/output") cleanFilesPath(reinforce_plugin_path + "/jiagu.db") cleanFilesPath(reinforce_apk_path) cleanFilesPath(channel_apks_path) //第二步:开始加固 reinforceApk() //第三部:重命名加固包 renameReinforceApk() //第四步:打多渠道包 buildChannelApks() } } /** * 注释:打多渠道包 * 时间:2019/1/4 0004 13:26 * 做者:郭翰林 */ def buildChannelApks() { println('开始编译多渠道包') File reinforceApk = new File(reinforce_apk_name) if (!reinforceApk.exists()) { return } //新建渠道包目录 File channelsPath = new File(channel_apks_path) if (!channelsPath.exists()) { channelsPath.mkdir() } exec { commandLine "powershell", "java -jar", reinforce_plugin_path + "/lib/walle-cli-all.jar batch -f ", chanel_config_path, reinforce_apk_name, channel_apks_path } println('编译多渠道包成功,生成的渠道包路径:' + channelsPath.getAbsolutePath()) } /** * 注释:重命名已加固好的APK * 时间:2019/1/4 0004 12:48 * 做者:郭翰林 */ def renameReinforceApk() { File files = new File('build/outputs/apk/release/reinforce') if (!files.exists()) { return } if (files.isDirectory()) { String[] content = files.list()//取得当前目录下全部文件和文件夹 for (String name : content) { //因为第一步清空缓存,reinforce文件夹内只会有一个已经加固而且签名的包 File signedApk = new File('build/outputs/apk/release/reinforce', name) File renameApk = new File(reinforce_apk_name) if (signedApk.exists() && signedApk.isFile()) { signedApk.renameTo(renameApk) } } } } /** * 注释:使用360加固加固Release包 * 时间:2019/1/2 0002 14:32 * 做者:郭翰林 */ def reinforceApk() { println('开始进行加固操做') File releaseApk = new File('build/outputs/apk/release/app-release.apk') if (!releaseApk.exists()) { throw new FileNotFoundException('Release包不存在,没法进行加固操做') } String releasePath = 'build/outputs/apk/release/app-release.apk' //建立加固文件夹 File reinforcePath = new File(reinforce_apk_path) if (!reinforcePath.exists()) { reinforcePath.mkdir() } exec { commandLine "powershell", "java -jar", reinforce_plugin_path + "/jiagu.jar", "-login", reinforce_plugin_name, reinforce_plugin_passward } exec { commandLine "powershell", "java -jar", reinforce_plugin_path + "/jiagu.jar", "-importsign", key_store_path, key_store_passward, alias, alias_passward } exec { commandLine "powershell", "java -jar", reinforce_plugin_path + "/jiagu.jar", "-jiagu", releasePath, reinforce_apk_path, "-autosign" } println('加固操做结束,加固包路径' + reinforcePath.getAbsolutePath()) } /** * 注释:清空文件夹 * 时间:2019/1/2 0002 14:15 * 做者:郭翰林 */ def cleanFilesPath(String path) { File files = new File(path) if (!files.exists()) { return } println('开始执行清除:' + files.getAbsolutePath()) if (files.isDirectory()) { String[] content = files.list()//取得当前目录下全部文件和文件夹 for (String name : content) { File temp = new File(path, name) if (temp.isDirectory()) {//判断是不是目录 cleanFilesPath(temp.getAbsolutePath())//递归调用,删除目录里的内容 temp.delete() } else { temp.delete() } } } files.delete() } 复制代码
(2)MAC环境下脚本
ext { //加固插件路径 reinforce_plugin_path = "${project.rootDir}/reinforce" reinforce_plugin_name = 'xxxxxx' reinforce_plugin_passward = 'xxxxxxx' //签名信息 key_store_path = "${project.rootDir}/app/xyz.keystore" key_store_passward = 'xxxxxxx' alias = 'xxxxx' alias_passward = 'xxxxxxxx' //Release Apk输出路劲 release_apk_path = "${project.buildDir}/outputs/apk/release/app-release.apk" //加固ApK输出路径 reinforce_apk_path = "${project.buildDir}/outputs/apk/release/reinforce/" //加固包名称 reinforce_apk_name = "${project.buildDir}/outputs/apk/release/reinforce/release_encrypted_aligned_signed.apk" //渠道配置文件 chanel_config_path = "${reinforce_plugin_path}/channel.txt" //渠道Apk输出路径 channel_apks_path = "${project.buildDir}/outputs/apk/release/channels/" } /** * 注释:编译加固渠道包 * 时间:2019/1/2 0002 14:01 * 做者:郭翰林 */ task buildReinforceRelease() { group '360reinforce' dependsOn('assembleRelease') doLast { //第一步:清空缓存 cleanFilesPath(reinforce_plugin_path + "/.cache") cleanFilesPath(reinforce_plugin_path + "/output") cleanFilesPath(reinforce_plugin_path + "/jiagu.db") cleanFilesPath(reinforce_apk_path) cleanFilesPath(channel_apks_path) //第二步:开始加固 reinforceApk() //第三部:重命名加固包 renameReinforceApk() //第四步:打多渠道包 buildChannelApks() //清除无用缓存 cleanFilesPath(reinforce_plugin_path + "/.cache") cleanFilesPath(reinforce_plugin_path + "/output") cleanFilesPath(reinforce_plugin_path + "/jiagu.db") } } /** * 注释:打多渠道包 * 时间:2019/1/4 0004 13:26 * 做者:郭翰林 */ def buildChannelApks() { println('开始编译多渠道包') File reinforceApk = new File(reinforce_apk_name) if (!reinforceApk.exists()) { return } //新建渠道包目录 File channelsPath = new File(channel_apks_path) if (!channelsPath.exists()) { channelsPath.mkdir() } exec { commandLine "bash", "-c", "java -jar ${reinforce_plugin_path}/lib/walle-cli-all.jar batch -f ${chanel_config_path} ${reinforce_apk_name} ${channel_apks_path}" } println('编译多渠道包成功,生成的渠道包路径:' + channelsPath.getAbsolutePath()) } /** * 注释:重命名已加固好的APK * 时间:2019/1/4 0004 12:48 * 做者:郭翰林 */ def renameReinforceApk() { File files = new File(reinforce_apk_path) if (!files.exists()) { return } if (files.isDirectory()) { String[] content = files.list()//取得当前目录下全部文件和文件夹 for (String name : content) { //因为第一步清空缓存,reinforce文件夹内只会有一个已经加固而且签名的包 File signedApk = new File(reinforce_apk_path, name) File renameApk = new File(reinforce_apk_name) if (signedApk.exists() && signedApk.isFile()) { signedApk.renameTo(renameApk) } } } } /** * 注释:使用360加固加固Release包 * 时间:2019/1/2 0002 14:32 * 做者:郭翰林 */ def reinforceApk() { println('开始进行加固操做') File releaseApk = new File(release_apk_path) if (!releaseApk.exists()) { throw new FileNotFoundException("Release包不存在,没法进行加固操做,文件路径:${releaseApk.getAbsolutePath()}") } //建立加固文件夹 File reinforcePath = new File(reinforce_apk_path) if (!reinforcePath.exists()) { reinforcePath.mkdir() } exec { commandLine "bash", "-c", "chmod +x ${reinforce_plugin_path}/java/bin/*" } exec { commandLine "bash", "-c", "java -jar ${reinforce_plugin_path}/jiagu.jar -login ${reinforce_plugin_name} ${reinforce_plugin_passward}" } exec { commandLine "bash", "-c", "java -jar ${reinforce_plugin_path}/jiagu.jar -importsign ${key_store_path} ${key_store_passward} ${alias} ${alias_passward}" } exec { commandLine "bash", "-c", "java -jar ${reinforce_plugin_path}/jiagu.jar -jiagu ${release_apk_path} ${reinforce_apk_path} -autosign" } println('加固操做结束,加固包路径' + reinforcePath.getAbsolutePath()) } /** * 注释:清空文件夹 * 时间:2019/1/2 0002 14:15 * 做者:郭翰林 */ def cleanFilesPath(String path) { File files = new File(path) if (!files.exists()) { return } println('开始执行清除:' + files.getAbsolutePath()) if (files.isDirectory()) { String[] content = files.list()//取得当前目录下全部文件和文件夹 for (String name : content) { File temp = new File(path, name) if (temp.isDirectory()) {//判断是不是目录 cleanFilesPath(temp.getAbsolutePath())//递归调用,删除目录里的内容 temp.delete() } else { temp.delete() } } } files.delete() } 复制代码
一、查找名为buildReinforceRelease的Task任务 二、待生成完毕以后,渠道包在主模块下的build/outputs/apk/release/channels/文件夹下