background

I recently encountered a requirement where the main code remains the same and I need to select different resources to type different packages based on the applicationId:” com.xxx.android.packPage “. Add productFlavors{} to build. Gradlet

1. Build. Add productFlavors gradlet

BuildConfigField can set global variables, which can be read by calling buildconfig.xx on the Activity after compilation.

ProductFlavors {test {applicationId "com.xx.test" versionCode 1 versionName "1.0.0" buildConfigField("String", "xx"); "\"xx\"") } }Copy the code
  1. Configuration of resources

    1. In the project app-> SRC -> create the test resource directory

As shown in figure:

2.After compiling

3. Package and remove assets from the assets file

Android. ApplicationVariants. All {variant - > if (variant. BuildType. Name = = 'release') {/ / release version The variant. MergeAssets. DoLast {/ / delete assets all the zip file folder / / includes: [' *.zip '] delete (fileTree (dir: variant.mergeAssets.outputDir)) } }Copy the code

2. Remove files such as.zip and.class

Android. ApplicationVariants. All {variant - > if (variant. BuildType. Name = = 'release') {/ / release version The variant. MergeAssets. DoLast {/ / delete assets all the zip file folder / / includes: [' *.zip '] delete (fileTree (dir: variant.mergeAssets.outputDir,includes: ['*.zip','*.class'])) } } }Copy the code