概述
Android 阵营多渠道软件分发,如:应用宝,小米商城,豌豆荚,91 助手,魅族应用商城等,如此多的分发渠道,导致了在 APP 的统计数据时候需要针对不同的渠道做区别统计,因此多渠道打包技术就变得很有必要性了。这边采用 Gradle+Umeng 的方式进行多渠道打包。
操作步骤
第一步、创建属性文件../projectname/channels.properties:

文件内容:
1 2 3 4 5
| #默认渠道 channels.default = yingyongbao #全部渠道刘表 #channels.list = meizu channels.list =360,meizu,xiaomi
|
第二步、利用 Gradle 脚本文件获取属性文件的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| File channelFile = file('../channels.properties'); String channelDefault = null; String channelList = null; String[] channelArray = new String[2]; if (channelFile.exists()) { def Properties channelProps = new Properties();
channelProps.load(new FileInputStream(channelFile)); if (channelProps.containsKey('channels.default')) { channelDefault = channelProps.getProperty('channels.default'); } if (channelProps.containsKey('channels.list')) { channelList = channelProps.getProperty('channels.list'); if (channelList != null) { if (channelList.contains(",")) { channelArray = channelList.split(','); } else { channelArray[0] = channelDefault; } } println(channelList) } else { channelArray[0] = channelDefault; } }
|
第三步、 在 app.gradle 脚本文件中设置多渠道名称:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| android { defaultConfig { ... ... manifestPlaceholders = [UMENG_CHANNEL_VALUE: channelDefault] } productFlavors { if (channelArray != null && channelArray.size() > 0) { for (int i = 0; i < channelArray.size(); i++) { def channel = channelArray[i] if (channel != null) { "${channel}" { manifestPlaceholders = [UMENG_CHANNEL_VALUE: channel] } } } } else { playStore {} } } dexOptions { javaMaxHeapSize "4g" } packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' exclude 'META-INF/services/javax.annotation.processing.Processor' } lintOptions { disable 'InvalidPackage' abortOnError false } }
|
1 2 3
| <meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />//使用占位符
|
第五步、使用 ADB 编译生成多渠道包
- 打开 Android stuido 指点的终端 Terminal 工具,使用命令行生成 APK 安装包。
(** 如果 Terminal 无法使用 ADB 命令,请度娘如何使用 Android Studio Terminal 执行 ADB 命令**)
1 2 3
| ./gradlew assemble //同时生成DEBUG和Release包 ./gradlew assembleRelease //生成Release包 ./gradlew assembleDebuge //生成Debug包
|
第六步、Apk 文件的路径
完成 Gradlew 打包程序后,生成的 APK 文件路径为:
${projectName}\app\build\outputs\apk\apkName.apk