多渠道打包
什么是多渠道打包?
渠道是国内Android平台特有的名词,由于Google Play在国内无法使用,所以出现了众多的应用市场,360市场,应用宝等,一个渠道代表一个应用市场。
为什么要多渠道打包?
如果你的应用只针对一个平台,那么无需多渠道(几乎不可能)。只要应用提供了多种下载途径,那么会用到多渠道,例如官网下载,豌豆荚下载等。
多渠道的意义
- 通过多渠道,可以更加详细的统计app被下载的途径。
- 通过多渠道,可以查看app在每个渠道上的表现。
- 通过多渠道,更有利于app的推广
多渠道打包原理
为每个平台或市场的apk指定一个唯一的标识符,通过标识符区分不同的平台。通常在Mainifest.xml
中指定。
集成友盟
添加对友盟库的依赖
`compile 'com.umeng.analytics:analytics:latest.integration'`
在Manifest.xml中去声明appkey,渠道及权限
<!--友盟 App Key-->
<meta-data
android:name="UMENG_APPKEY"
android:value="588c5ab6ae1bf87552001aee"/>
<!--友盟渠道号 通过占位符动态修改渠道号-->
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CCHANNEL_VALUE}"/>
<!--友盟权限-->
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
在模块的build.gradle配置打包信息
在android{….defaultConfig{}}配置文件中添加
multiDexEnabled true//突破应用方法数65535的限制
manifestPlaceholders = [UMENG_CCHANNEL_VALUE :"umeng"] //默认的渠道号
最后是这样的
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "rc.loveq.meizhi"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//突破应用方法数65535的限制
multiDexEnabled true
//默认的渠道号
manifestPlaceholders = [UMENG_CCHANNEL_VALUE :"umeng"]
}
添加签名文件配置,签名文件可以在AS中 Build->Generate Signed APK中生成
//添加签名文件配置,必须写在buildTypes之前
signingConfigs{
debug{}
//为release添加签名文件配置
release{
storeFile file("umeng.jks")
storePassword "umeng123"
keyAlias "umeng"
keyPassword "umeng123"
}
}
配置buildTypes
buildTypes {
release {
minifyEnabled false //是否启动混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//指定apk使用的签名文件
signingConfig signingConfigs.release
//指定release包apk输出名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "${variant.productFlavors[0].name}"+".apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
配置多渠道脚本
productFlavors{
xiaomi{
manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"xiaomi"]
}
wandoujia{
manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"wandoujia"]
}
}
通过上面几步,现在build.gradle是这样的
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "rc.loveq.meizhi"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//突破应用方法数65535的限制
multiDexEnabled true
//默认的渠道号
manifestPlaceholders = [UMENG_CCHANNEL_VALUE :"umeng"]
}
//添加签名文件配置,必须写在buildTypes之前
signingConfigs{
debug{}
//为release添加签名文件配置
release{
storeFile file("umeng.jks")
storePassword "umeng123"
keyAlias "umeng"
keyPassword "umeng123"
}
}
buildTypes {
release {
minifyEnabled false //是否启动混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//指定apk使用的签名文件
signingConfig signingConfigs.release
//指定release包apk输出名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "${variant.productFlavors[0].name}"+".apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
//方法一:
productFlavors{
xiaomi{
manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"xiaomi"]
}
wandoujia{
manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"wandoujia"]
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
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.1.0'
testCompile 'junit:junit:4.12'
compile 'com.umeng.analytics:analytics:latest.integration'
}
在AS终端输入gradlew assembleRelease
(需要配置环境变量)
多渠道脚本其他配置方法
//方法二
productFlavors{
xiaomi{}
wandoujia{}
}
productFlavors.all{
flavor->flavor.manifestPlaceholders=[UMENG_CCHANNEL_VALUE :name]
}
多渠道打包一些实际应用场景
有些时候,我们要测试不同的的功能,但是又不想卸载重装应用,我们可以这样用:
productFlavors{
okhttp{
//包名为在applicationId拼接applicationIdSuffix
//但是在实际应用运行中,通过代码获取包名时还是原来的包名,即applicationId
applicationIdSuffix "okhttp"
//通过脚本动态修改应用的app_name
//必须把工程中 <!--<string name="app_name">MeiZhi</string>-->注释掉
//否则打包不通过
resValue "string","app_name","okhttp"
}
jpush{
applicationIdSuffix "jpush"
resValue "string","app_name","jpush"
}
}