SoFunction
Updated on 2025-03-01

Advanced knowledge about Android multi-channel packaging

Advanced knowledge through multi-channel packaging

Before starting the article, let’s take a look at the following situation:

android {  
  productFlavors {  
      //100 multi-channel configurations  }
//Configuration of multi-channel signature  signingConfigs {
     xiaomi {
        storeFile file("../")
        storePassword 'xiaomi'
        keyAlias 'xiaomi'
        keyPassword 'xiaomi'
        v1SigningEnabled true
        v2SigningEnabled true
     }
     huawei {
        storeFile file("../")
        storePassword 'huawei'
        keyAlias 'huawei'
        keyPassword 'huawei'
        v1SigningEnabled true
        v2SigningEnabled true
     }
  }
  buildTypes {
       debug {
// The debug setting here does not work, maybe it is a compiler problem?//          
//          
       }
       release {
            
            
           //...100 signature configuration       }
  }
//Different resource file configurations  sourceSets{
       'src/main/res-xiaomi'
       'src/main/res-huawei'
       'src/main/res-xxx'
       'src/main/res-xxx'
       'src/main/res-xxx'
      //...100 resource file configuration  }
//Different dependencies from different channels  dependencies {
      xiaomiApi('xxxxxxx')
      huaweiImplementation('xxxxxxxx')
      xxxApi('xxxxxxx')
      xxxApi('xxxxxxx')
      xxxApi('xxxxxxx')
      //...100 channels depend on configuration  }    
}

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 way? The answer is yes.

Resource file configuration

sourceSets{
    def sets = getSourceSets()//Get resource settings collection    {//Traveling through multiple channels        if('huawei'.equals(name))//Do special treatment for special channels          (name). 'src/main/res-xxx'
        else
          (name). 'src/main/res-'+name
        //Equivalent to 'src/main/res-xiaomi'        //       'src/main/res-huawei'
        //      .....
    }
}

Depend on configuration

def dependenMap =[xiaomi: 'xiaomi dependencies',
                  huawei: 'huawei dependency',
                  ...
                  xxx: 'xxx dependency']
dependencies{
    {
        if('huawei'.equals(name))//Do special treatment for special channels           (name+"Implementation",project(dependenMap[name]))
        else
           (name+"Api",project(dependenMap[name]))
        
        //xiaomiApi('xiaomi dependency')        //huaweiImplementation('xiaomi dependency')        //xxxxApi('xxx dependency')    }
}

Signature configuration

signingConfigs {
    xiaomi{
        storeFile file("../")
        storePassword 'xiaomi'
        keyAlias 'xiaomi'
        keyPassword 'xiaomi'
        v1SigningEnabled true
        v2SigningEnabled true
    }
    huawei{
        storeFile file("../")
        storePassword 'xiaomi'
        keyAlias 'xiaomi'
        keyPassword 'xiaomi'
        v1SigningEnabled true
        v2SigningEnabled true
    }
    ....More signature configurations
}
buildTypes {
    debug {
        
    }
    release {
          {
             if('huawei'.equals(name))//Do special treatment for special channels               productFlavors[name].signingConfig        
             else
               productFlavors[name].signingConfig signingConfigs[name]       
         }
    }
}

This is the article about advanced knowledge about Android multi-channel packaging. For more related content on Android multi-channel packaging, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!