Android gradle配置productFlavors多渠道打包,一次性编译不同应用商店的分发包渠道包

应用编译打包的时候经常会为不同的应用市场打不同的包,为不同的包做不同的配置。

比如集成友盟统计,会对不同的渠道包配置一个不同的渠道号用于各渠道的统计。

如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.him.autosizingtest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
keyAlias 'myalias'
keyPassword 'password'
storeFile file('D:/mykeystore.jks')
storePassword 'password'
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
productFlavors {
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
yingyongbao {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yingyongbao"]
}
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
}
}

Click and drag to move

直接双击如下图中的assembleRelease,就可以一口气编译所有渠道的release包

imgClick and drag to move

编译完成后,各渠道包在如下路径

imgClick and drag to move

如果说你配置的友盟渠道号和flavor名称一样,还有一种更简洁的写法,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.him.autosizingtest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
keyAlias 'myalias'
keyPassword 'password'
storeFile file('D:/mykeystore.jks')
storePassword 'password'
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
productFlavors {
wandoujia {}
yingyongbao {}
xiaomi {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}

Click and drag to move