做Android开发的绕不开多渠道这件事,因为Android应用市场大大小小有几百个,而我们需要在发布应用之后统计各个渠道的用户下载量,这个时候就要多渠道打包,也就是对各个渠道进行标识,以便于统计。多渠道和统计时息息相关的,因为我们做多渠道打包的目的就是要统计,友盟统计是大家日常开发中常用的渠道统计工具,使用相当普遍,接下来我讲到的打包方法就是基于友盟统计实施的。
一、按照友盟官方文档说明,渠道信息通常需要在AndroidManifest.xml中配置如下值:
value="Channel ID" android:name="UMENG_CHANNEL"/>
统计的重点就是value值Channel_ID,即渠道标识,例如:360,qq,wandoujia等等,在这里不直接写出某个渠道名,而是写一个占位符,我们会在build.gradle中进行设置,之后gradle编译的时候会动态的替换掉这个占位符。代码如下:
data
android:name="UMENG_CHANNEL"
android:value="${CHANNEL_VALUE}" />
二、在app的build.gradle的android{}中添加如下内容:
android {
flavorDimensions "default"
productFlavors {
web {}
baidu {}
c360 {}
qq {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
}
也可以单独设置:
android {
flavorDimensions "default"
productFlavors {
web {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "web "]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu "]
}
c360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360 "]
}
qq {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qq "]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}
}
productFlavors是android节点的一个子节点。你需要打什么渠道的包,就在这里按友盟的要求用渠道名给UMENG_CHANNEL_VALUE赋值。上面这个配置的作用就是,为每个渠道包产生不同的 UMENG_CHANNEL_VALUE 的值。
三、自定义APK名称,多渠道打包
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
variant.productFlavors.each { flavor ->
def project = "cpm"
def separator = "_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
def apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + ".apk"
if (buildType == "release") {
apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + separator + formattedDate + ".apk"
}
output.outputFileName = apkName
}
}
}
output.outputFileName是apk输出位置,其它的是自定义apk名称,最终输出位置如下图:
app–>build–>outputs–>apk
四、配置签名信息,代码如下:
Properties props = new Properties()
props.load(project.rootProject.file('local.properties').newDataInputStream())
android {
signingConfigs {
release {
storeFile file(props['release.keystore'])
storePassword '123456'
keyAlias '123456'
keyPassword '123456'
}
}
}
storeFile file(props[‘release.keystore’])是我的keystore位置,在local.properties中添加如图代码:
五、执行打包命令
我们只需在Android Studio的命令行Terminal下输入以下命令即可:
linux下打包全部的Release版渠道包:
./gradlew assembleRelease
windows下打包全部的Release版渠道包
gradlew.bat assembleRelease
如果想要Debug的包:
gradlew.bat assembleDebug
如果只要豌豆荚的包:
gradlew.bat assemblewandoujia
如果只要豌豆荚的Release版:
gradlew.bat assemblewandoujiaRelease
可以看出这个命令是可以自由组合的,大家按照自己的需求去试试!OK!到此一行命令就能打出我们想要的N个包啦!希望我写的内容对大家有所帮助,有什么不对的地方欢迎指正!!!
下面贴上我项目里build.gradle配置文件,以供参考:
apply plugin: 'com.android.application'
Properties props = new Properties()
props.load(project.rootProject.file('local.properties').newDataInputStream())
android {
compileSdkVersion rootProject.ext.compileSdkVersion
flavorDimensions "default"
defaultConfig {
applicationId "com.exmple.myApp"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
multiDexEnabled true
versionCode 11000
versionName "1.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file(props['release.keystore'])
storePassword '123456'
keyAlias '123456'
keyPassword '123456'
}
}
buildTypes {
debug {
debuggable true
minifyEnabled false
signingConfig signingConfigs.release
}
//线上环境
release {
debuggable false
minifyEnabled false
signingConfig signingConfigs.release
}
}
lintOptions {
ignoreWarnings true
checkReleaseBuilds false
}
repositories {
flatDir {
dirs 'libs'
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir "src/main/jniLibs"
}
productFlavors {
web {}
baidu {}
c360 {}
qq {}
oppo {}
vivo {}
huawei {}
mi {}
meizu {}
lenovo {}
leshi {}
sogou {}
wandoujia {}
samsung {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name, JPUSH_CHANNEL: name]
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//自定义APK名称,多渠道打包
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
variant.productFlavors.each { flavor ->
def project = "cpm"
def separator = "_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
def apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + ".apk"
if (buildType == "release") {
apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + separator + formattedDate + ".apk"
}
output.outputFileName = apkName
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
implementation "com.android.support:design:$supportLibraryVersion"
implementation "com.android.support:cardview-v7:$supportLibraryVersion"
...
}