How can a chicken lay more than one egg? How does one set of code correspond to multiple versions and multiple customers?

The actual development encountered such a requirement: a product to be supplied to different customers, most of the logical code and resource files are the same, server address is not the same. Should we make a complete copy of the entire project and build a new one? If you have to fix common bugs, this is a hassle. However, different customers may have different needs in the later stage, which requires targeted development. How to do?

No pun intended, my approach is to maintain one set of code while still developing different features for different customer versions.

I use the Android Studio 3.0 Canary 9 development environment. Gradle is used for configuration.

####1. Server address, String resources, different dependencies

flavorDimensions "server"
productFlavors {
        C1_releaseServer {
                //BuildConfig.SERVER_ROOT_URL
                buildConfigField("String"."SERVER_ROOT_URL"."\" http://211.7.1.224:80/\ "")
                //R.String.app_name
                resValue("string"."app_name"."Application Name 1")
                dependencies {
                        compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-*.jar')
                }
                applicationId "com.smart1"
                dimension "server"
        }

        C2_releaseServer {
                //BuildConfig.SERVER_ROOT_URL
                buildConfigField("String"."SERVER_ROOT_URL"."\" http://211.7.1.224:80/\ "")
                //R.String.app_name
                resValue("string"."app_name"."Application Name 2"< span style = "box-sizing: border-box! Important;'libs', include: '*.jar', exclude: 'android-support-*.jar')
                }
                applicationId "com.smart2"
                dimension "server"}}Copy the code

FlavorDimensions is the version dimension. Only one dimension is used here, and multiple dimensions can be used if necessary. Multiple dimensions are combined. ApplicationId is different so that multiple APKs can be installed on a single phone at the same time. Note: The package in androidmanifest.xml does not need to be modified.

Run Gradle after you have modified Gradle for Module. You can choose Flavors that you have configured. Click “Run”. Note: If you switch between different Flavors, Gradle will run it again. BuildConfig.*** in the code will compile successfully after it is run.

####2. Different versions of resource files may also use different image resources. The SRC folder contains Flavors resource files. The resource files can be images or Java files.

“Flavors” is displayed in the red box. The resource folder types are displayed on the top of the options.

Once the folder is created, you can copy the resource files into it. Note here that the file with the same name that was in the main folder needs to be deleted.

// productFlavors is the same as productFlavorssourceSets {/ / different resources should be put in different versions of the corresponding folder, in the main, and public resources under the main folder / / C1_releaseServer Java srcDirs / / C1_releaseServer.manifest.srcFile'AndroidManifest.xml'

        C1_releaseServer.res.srcDirs = ['src/main/res'.'src/C1_releaseServer/res']
        C2_releaseServer.res.srcDirs = ['src/C2_releaseServer/res'.'src/main/res']}Copy the code

Finally, run Gradle, then select Build Variant, then run Gradle again, and run it!