Android Studio多渠道打包的配置方法
作者:franksight 时间:2023-06-15 23:19:48
说到多渠道,这里不得不提一下友盟统计,友盟统计是大家日常开发中常用的渠道统计工具,而我们的打包方法就是基于友盟统计实施的。按照友盟官方文档说明,渠道信息通常需要在AndroidManifest.xml中配置如下值:
<meta-data android:value="Channel ID" android:name="UMENG_CHANNEL"/>
上面的value值Channel_ID就是渠道标识。我们的期望的就是在编译时候这个值能够自动变化以满足区分多渠道的需求。
(一)在AndroidManifest.xml里设置动态渠道变量
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
(二)在build.gradle设置productFlavors
这里假定我们需要打包的渠道为360、小米、百度、豌豆荚
android {
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
qh360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}
}
或者批量修改
android {
productFlavors {
xiaomi {}
qh360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
所谓ProductFlavors其实就是可定义的产品特性,配合 manifest merger 使用的时候就可以达成在一次编译过程中产生多个具有自己特性配置的版本。上面这个配置的作用就是,为每个渠道包产生不同的 UMENG_CHANNEL_VALUE 的值。
(三)执行打包操作
在AndroidStudio菜单栏点击Build菜单–>Generate signed APK–>选择key,并输入密码
然后下一步,选择打包渠道
最后点击完成按钮,我们已经成功的将五个渠道包打好。
(四)执行打包命令 ./gradlew assembleRelease
除了使用AndroidStudio图形打包操作以外,我们也可以使用命令行进行打包操作,具体步骤如下:
在AndroidStudio窗口左下角打开Terminal面板,输入gradlew assembleRelease命令
如果系统中没有安装Gradle,则会自动下载完成安装及初始化
打包成功后控制台会提示BUILD SUCCESSRUL
在app–>build–>outputs–>apk路径中就可以看到打包成功后的APK
注意,此时这里的APK包名显示为unsigned,也就是说未签名,我们可以继续在build.gradle文件中配置签名信息
signingConfigs {
release{
storeFile file("../wooyun_keystore") //签名文件路径
storePassword "your password"
keyAlias "your alias"
keyPassword "your password" //签名密码
}
}
然后再次执行gradlew assembleRelease命令
这次生成的就是含有签名的渠道包。
当我们的渠道包版本比较多时,可以自定义所打APK包名称,用以区分
// 自定义输出配置,这里我们加上APK版本号1.0
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为frank_v1.0_wandoujia.apk
def fileName = "frank_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
assemble是Gradle中的编译打包命令,有如下用法:
如果我们想打包wandoujia渠道的release版本,执行如下命令就好了:
gradlew assembleWandoujiaRelease
如果我们想打包wandoujia渠道的debug版本,执行如下命令就好了:
gradlew assembleWandoujiaDebug
如果我们只打wandoujia渠道版本,则:
gradlew assembleWandoujia
此命令会生成wandoujia渠道的Release和Debug版本
同理我想打全部Release版本:
gradlew assembleRelease
这条命令会把Product Flavor下的所有渠道的Release版本都打出来。
下面是我个人的build.gradle
配置文件,分享给大家,以作参考
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.frank.example"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
//签名
signingConfigs {
debugConfig {
storeFile file("../frank_keystore") //签名文件
storePassword "your password"
keyAlias "your alias"
keyPassword "your password" //签名密码
}
release{
storeFile file("../wooyun_keystore") //签名文件
storePassword "your password"
keyAlias "your alias"
keyPassword "your password" //签名密码
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// 自定义输出配置
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为frank_v1.0_wandoujia.apk
def fileName = "frank_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
productFlavors {
xiaomi {}
qh360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
来源:https://blog.csdn.net/whj9073/article/details/77600451
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现简单的抽牌游戏
Java实现简单学生管理系统
![](https://img.aspxhome.com/file/2023/1/69901_0s.png)
android 判断网络是否可用与连接的网络是否能上网
Spring Security 控制授权的方法
![](https://img.aspxhome.com/file/2023/4/58024_0s.png)
使用SharedPreferences在Android存储对象详细代码
使用spring容器在初始化Bean时前和后的操作
JavaWeb简单文件上传流程的实战记录
![](https://img.aspxhome.com/file/2023/9/71889_0s.png)
c#解压文件的实例方法
Java 3种方法实现进制转换
详解使用Spring Security进行自动登录验证
![](https://img.aspxhome.com/file/2023/8/72288_0s.jpg)
spring-boot中使用spring-boot-devtools的实现代码
IDEA报错:无效的源发行版解决方案
![](https://img.aspxhome.com/file/2023/4/62454_0s.png)
Bean Searcher配合SpringBoot的使用详解
![](https://img.aspxhome.com/file/2023/3/83143_0s.jpg)
springcloud注册hostname或者ip的那些事
![](https://img.aspxhome.com/file/2023/9/65369_0s.png)
android实现点击按钮控制图片切换
![](https://img.aspxhome.com/file/2023/0/126770_0s.gif)
java自定义异常打印内容详解
![](https://img.aspxhome.com/file/2023/2/77082_0s.jpg)
Android发送xml数据给服务器的方法
基于SpringBoot实现用户身份验证工具
Activiti开发环境的配置
![](https://img.aspxhome.com/file/2023/9/83499_0s.jpg)
c# AES字节数组加密解密流程及代码实现
![](https://img.aspxhome.com/file/2023/7/94537_0s.png)