什么是多渠道打包

① 渠道是国内Android平台特有的名词

② 由于Google Play在国内无法使用,因此出现了众多市场

③ 一个渠道即代表一个应用市场

④ 渠道并不是什么高大上的东西,也不高深

多渠道打包使用场景

① 应用只针对某个平台,则无需多渠道(几乎不存在)

② 只要应用提供了多种下载途径,就会用到多渠道

多渠道打包的意义

① 通过多渠道,可以更详细的统计App被下载的途径

② 通过多渠道,我们可以查看App在每个渠道上的表现

③ 通过多渠道,更有利于我们App的推广

多渠道打包原理

原理很简单,就是为我们每个平台或市场的Apk指定一个唯一的标识符,打包的时候不断地去替换这个唯一标识符。Android中我们通常在AndroidManifest.xml中为其指定,也可以在代码中指定,但一般不用。

使用友盟进行多渠道打包步骤

1. 上友盟官网注册并创建新应用,获得Appkey

2. 下载SDK并集成,推荐使用Android Studio去集成

3. 在AndroidManifest.xml中配置渠道号和Appkey

4. 在应用modulez中的build.gradle中编写多渠道脚本

Android Apk 多渠道打包_Android

Android Apk 多渠道打包_android_02

介绍下Android Studio中工程几个文件的作用:

settings.gradle

Android Apk 多渠道打包_android_03

project:build.gradle

Android Apk 多渠道打包_多渠道打包_04

module:build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.jackie.multichannelpackage"
minSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true; //突破应用方法数65535的限制
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"] //设置默认渠道号
}

//指定打包的类型,debug和release就够了
buildTypes {
release {
minifyEnabled false //是否启用混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

//指定签名
signingConfig signingConfigs.release

//指定release包的输出文件名就是渠道名
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)
}
}
}
}
}

//定义signingConfig.release 添加我们的签名文件配置
signingConfigs {
debug {}

/**
*为我们的release添加签名文件配置
* 签名文件通过Build -> Generate Signed APK 来生成
*/
release {
storeFile file("common.jks")
storePassword "rzq123456"
keyAlias "qndroid"
keyPassword "rzql123456"
}
}

//多渠道支持
productFlavors {
xiaomi {
// manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]

//指定资源变动,替换strings.xml中app_name的值
// resValue "string", "app_name", "xiaomi_app"
}

wandoujia {
// manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]

// resValue "string", "app_name", "wandoujia_app"
}

//根据功能点打出不同的包
// okhttp {
// applicationIdSuffix "okhttp"
// resValue "string", "app_name", "okhttp"
// }
//
// jpush {
// applicationIdSuffix "jpush"
// resValue "string", "app_name", "jpush"
// }
}

productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}

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.0.1'
testCompile 'junit:junit:4.12'

//添加友盟统计库依赖
compile 'com.umeng.analytics:analytics:latest.integration'
}

添加上面的配置后,使用gradlew命令去自动的打出所有渠道的包

Android Apk 多渠道打包_多渠道打包_05