Gradle多渠道打包

国内Android市场众多渠道,为了统计每个渠道的下载及其它数据统计,就需要我们针对每个渠道单独打包

以友盟多渠道打包为例

在AndroidManifest.xml里面

1 <meta-data
2     android:name="UMENG_CHANNEL"
3     android:value="Channel_ID" />

里面的Channel_ID就是渠道标示。我们的目标就是在编译的时候这个值能够自动变化,步骤如下:

1、在AndroidManifest.xml里配置PlaceHolder

1 <meta-data
2     android:name="UMENG_CHANNEL"
3     android:value="${UMENG_CHANNEL_VALUE}" />

2、在build.gradle设置productFlavors

 1 android {  
 2     productFlavors {
 3         xiaomi {
 4             manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
 5         }
 6         _360 {
 7             manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
 8         }
 9         baidu {
10             manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
11         }
12         wandoujia {
13             manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
14         }
15     }  
16 }

或者批量修改

 1 android {  
 2     productFlavors {
 3         xiaomi {}
 4         _360 {}
 5         baidu {}
 6         wandoujia {}
 7     }  
 8 
 9     productFlavors.all { 
10         flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] 
11     }
12 }

assemble 这个命令,会结合 Build Type 创建自己的task,如:

gradle assembleDebug
gradle assembleRelease

除此之外 assemble 还能和 Product Flavor 结合创建新的任务,其实 assemble 是和 Build Variants 一起结合使用的,而 Build Variants = Build Type + Product Flavor 

如果我们想打包wandoujia渠道的release版本,执行如下命令:

gradle assembleWandoujiaRelease

如果我们只打wandoujia渠道版本,则:

gradle assembleWandoujia

这个命令会生成wandoujia渠道的Release和Debug版本

想打包全部Release版本:

gradle assembleRelease

assemble 命令创建task有如下用法:

  • **assemble**: 允许直接构建一个Variant版本,例如assembleFlavor1Debug。

  • **assemble**: 允许构建指定Build Type的所有APK,例如assembleDebug将会构建Flavor1Debug和Flavor2Debug两个Variant版本。

  • **assemble**: 允许构建指定flavor的所有APK,例如assembleFlavor1将会构建Flavor1Debug和Flavor1Release两个Variant版本。

完整的build.gradle示例如下:

 1 apply plugin: 'com.android.application'
 2 
 3 def releaseTime() {
 4     return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
 5 }
 6 
 7 android {
 8     compileSdkVersion 21
 9     buildToolsVersion '21.1.2'
10 
11     defaultConfig {
12         applicationId "com.boohee.*"
13         minSdkVersion 14
14         targetSdkVersion 21
15         versionCode 1
16         versionName "1.0"
17         
18         // dex突破65535的限制
19         multiDexEnabled true
20         // 默认是umeng的渠道
21         manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
22     }
23 
24     lintOptions {
25         abortOnError false
26     }
27 
28     signingConfigs {
29         debug {
30             // No debug config
31         }
32 
33         release {
34             storeFile file("../yourapp.keystore")
35             storePassword "your password"
36             keyAlias "your alias"
37             keyPassword "your password"
38         }
39     }
40 
41     buildTypes {
42         debug {
43             // 显示Log
44             buildConfigField "boolean", "LOG_DEBUG", "true"
45 
46             versionNameSuffix "-debug"
47             minifyEnabled false
48             zipAlignEnabled false
49             shrinkResources false
50             signingConfig signingConfigs.debug
51         }
52 
53         release {
54             // 不显示Log
55             buildConfigField "boolean", "LOG_DEBUG", "false"
56 
57             minifyEnabled true
58             zipAlignEnabled true
59             // 移除无用的resource文件
60             shrinkResources true
61             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
62             signingConfig signingConfigs.release
63 
64             applicationVariants.all { variant ->
65                 variant.outputs.each { output ->
66                     def outputFile = output.outputFile
67                     if (outputFile != null && outputFile.name.endsWith('.apk')) {
68                         // 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
69                         def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
70                         output.outputFile = new File(outputFile.parent, fileName)
71                     }
72                 }
73             }
74         }
75     }
76 
77     // 友盟多渠道打包
78     productFlavors {
79         wandoujia {}
80         _360 {}
81         baidu {}
82         xiaomi {}
83         tencent {}
84         taobao {}
85         ...
86     }
87 
88     productFlavors.all { flavor ->
89         flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
90     }
91 }
92 
93 dependencies {
94     compile fileTree(dir: 'libs', include: ['*.jar'])
95     compile 'com.android.support:support-v4:21.0.3'
96     compile 'com.jakewharton:butterknife:6.0.0'
97     ...
98 }
原文地址:https://www.cnblogs.com/l2rf/p/4998211.html