hello~大家好!做了这么久安卓了之前用过Ec进行多渠道的打包工具现在使用的AS一直都想写一篇关于AS的多渠道打包方式,今天无意间看到一篇博客 康小白Code的写的很不错就转载啦~感谢大神的指点!
做Android开发的绕不开多渠道这件事,因为Android应用市场大大小小有几百个,而我们需要在发布应用之后统计各个渠道的用户下载量,这个时候就要多渠道打包,也就是对各个渠道进行标识,以便于统计。多渠道和统计时息息相关的,因为我们做多渠道打包的目的就是要统计,友盟统计是大家日常开发中常用的渠道统计工具,使用相当普遍,接下来我讲到的打包方法就是基于友盟统计实施的。
正文:
一、按照友盟官方文档说明,渠道信息通常需要在AndroidManifest.xml中配置如下值:
1
统计的重点就是value值Channel_ID,即渠道标识,例如:360,qq,wandoujia等等,在这里不直接写出某个渠道名,而是写一个占位符,我们会在build.gradle中进行设置,之后gradle编译的时候会动态的替换掉这个占位符。代码如下:
android:value="${CHANNEL_VALUE}" />
1
2
3
二、在app的build.gradle的android{}中添加如下内容:
android {
flavorDimensions "default"
productFlavors {
web {}
baidu {}
c360 {}
qq {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
也可以单独设置:
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"]
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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'
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
storeFile file(props[‘release.keystore’])是我的keystore位置,在local.properties中添加如图代码:
五、执行打包命令
到这一步,所有配置已经完成,从此以后我们就可以用一行命令打出N个包啦!
我们只需在Android Studio的命令行Terminal下输入以下命令即可:
linux下打包全部的Release版渠道包:
./gradlew assembleRelease
1
windows下打包全部的Release版渠道包
gradlew.bat assembleRelease
1
如果想要Debug的包:
gradlew.bat assembleDebug
1
如果只要豌豆荚的包:
gradlew.bat assemblewandoujia
1
如果只要豌豆荚的Release版:
gradlew.bat assemblewandoujiaRelease
1
可以看出这个命令是可以自由组合的,大家按照自己的需求去试试!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"
...
}
---------------------
作者:康小白Code
来源:CSDN
原文:https://blog.csdn.net/k_bb_666/article/details/79113222
版权声明:本文为博主原创文章,转载请附上博文链接!