Advanced knowledge about Android multi channel packaging
- 2021-12-13 16:52:39
- OfStack
Advanced knowledge of multi-channel packaging
Before the article begins, look at the following situation:
android {
productFlavors {
//100 Multi-channel configuration
}
// Configuration of multi-channel signature
signingConfigs {
xiaomi {
storeFile file("../xiaomi.keystore")
storePassword 'xiaomi'
keyAlias 'xiaomi'
keyPassword 'xiaomi'
v1SigningEnabled true
v2SigningEnabled true
}
huawei {
storeFile file("../huawei.keystore")
storePassword 'huawei'
keyAlias 'huawei'
keyPassword 'huawei'
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
debug {
// debug The setting here doesn't work, it may be a compiler problem?
// productFlavors.xiaomi.signingConfig signingConfigs.test
// productFlavors.huawei.signingConfig signingConfigs.test
}
release {
productFlavors.xiaomi.signingConfig signingConfigs.xiaomi
productFlavors.huawei.signingConfig signingConfigs.huawei
//....100 Signature configuration
}
}
// Different resource file configuration in different channels
sourceSets{
xiaomi.res.srcDirs 'src/main/res-xiaomi'
huawei.res.srcDirs 'src/main/res-huawei'
xxx.res.srcDirs 'src/main/res-xxx'
xxx.res.srcDirs 'src/main/res-xxx'
xxx.res.srcDirs 'src/main/res-xxx'
//....100 Resource file configuration
}
// Different dependent files in different channels
dependencies {
xiaomiApi('xxxxxxx')
huaweiImplementation('xxxxxxxx')
xxxApi('xxxxxxx')
xxxApi('xxxxxxx')
xxxApi('xxxxxxx')
//....100 Different dependent configurations of each channel
}
}
It is not difficult to find that when there are many channels, different configurations of different channels will be very cumbersome. Is there a more convenient method? The answer is yes.
Resource file configuration
sourceSets{
def sets = getSourceSets()// Gets a collection of resource settings
productFlavors.all{// Traversing multiple channels
if('huawei'.equals(name))// Make special treatment for special channels
sets.getByName(name).res.srcDir 'src/main/res-xxx'
else
sets.getByName(name).res.srcDir 'src/main/res-'+name
// Equivalent to xiaomi.res.srcDir 'src/main/res-xiaomi'
// huawei.res.srcDir 'src/main/res-huawei'
// .....
}
}
Dependent configuration
def dependenMap =[xiaomi: 'xiaomi Dependency ',
huawei: 'huawei Dependency ',
...
xxx: 'xxx Dependency ']
dependencies{
productFlavors.all{
if('huawei'.equals(name))// Make special treatment for special channels
dependencies.add(name+"Implementation",project(dependenMap[name]))
else
dependencies.add(name+"Api",project(dependenMap[name]))
//xiaomiApi('xiaomi Dependency ')
//huaweiImplementation('xiaomi Dependency ')
//xxxxApi('xxx Dependency ')
}
}
Signature configuration
signingConfigs {
xiaomi{
storeFile file("../xiaomi.keystore")
storePassword 'xiaomi'
keyAlias 'xiaomi'
keyPassword 'xiaomi'
v1SigningEnabled true
v2SigningEnabled true
}
huawei{
storeFile file("../xiaomi.keystore")
storePassword 'xiaomi'
keyAlias 'xiaomi'
keyPassword 'xiaomi'
v1SigningEnabled true
v2SigningEnabled true
}
.... More Signature Configuration
}
buildTypes {
debug {
}
release {
productFlavors.all {
if('huawei'.equals(name))// Make special treatment for special channels
productFlavors[name].signingConfig signingConfigs.xxx
else
productFlavors[name].signingConfig signingConfigs[name]
}
}
}