Multichannel packaging of a suite of code in Android Studio

Keywords: Android xml Java SDK


A set of code does the following:

  • Packaging different applicationId s can be installed on the same phone at the same time
  • Different logo, app name,
  • Different third-party SDK access configurations (e.g. WeChat sharing appid, laser push appkey)
  • Ability to distinguish debug from release configurations

Functions used: productFlavor and buildTypes

Principle: Priority buildTypes are greater than productFlavor

Example: A set of code packages apk s for two banks

1. ModificationBuild.gradle.BuilTypes maintains the default debug and release settings, adding two productFlavor s within the android node.

flavorDimensions 'bank'
productFlavors {
    icbc {
    dimension = 'bank'
    manifestPlaceholders = [bankName: 'Industrial and Commercial Bank of China']
    ccb {
    dimension = 'bank'
    applicationId = 'com.cbc. mobilebank
    manifestPlaceholders = [bankName: 'Bank for economic construction']
2. Modify src\main\AndroidManifest.xml, replace the location with'${bankName}'and ${applicationId}

2.1 Set app name,android:label="${bankName}", rememberTools:replaceTo addAndroid:label.(App loGoandroid:iconYou can also use this method to distinguish, but the latter is more recommended)


2.2 Set AllAndroid:authoritiesFor example

android:authorities="${applicationId}. FileProvider "
android:resource="@xml/file_paths" />
<provider android:name=""
android:authorities="${applicationId}.DataProvider" android:exported="false" /> 

2.3 refers to a third-party sdk, such as Aurora Push that needs to be modified

<permission android:name="${applicationId}.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<permission android:name="${applicationId}.permission.MIPUSH_RECEIVE"
android:protectionLevel="signature" />

3. Add three folders "icbc", "icbcRelease", "release" to the src\main sibling directory.When icbcRelease is selected for compilation, the compiler merges files of the same name at a priority such as "icbcRelease > release > icbc>main".

4. Set the picture resources according to the above priorities.Refer to main\res\drawable\Logo.pngAt the file level, add a custom app logo to the folder. If your icbc uses the same logo in debug and release, add it only in the icbc folder.

5. Set the string xml resource.

5.1 New src\main\res\values\Configs.xmlAnd put some parameters that are different for each bank.Or not to increaseConfigs.xmlFiles, appended directly toStrings.xmlInternal.

<?xml version="1.0" encoding="utf-8"?>
<string name="WXAPPId">XXXXXXXXXXXXXXX</string>

5.2 According to the actual situation,'icbc','icbcRelease','release'increases correspondinglyConfigs.xml.Only JPUSH_can be addedAPPKEY, then WXAPPId will use the value of main.

5.3AndroidManifest.xmlPress'@string/JPUSH_in the equal xml fileAPPKEY "Use"

The method used in the 5.4 activity code: "This.getString(R.String.resource_Name; "or"this.getString(R.string.resource_name); "

5.5 How to use other java files (must have Context or pplication):Context.getString(R.String.resource_Name; "or"application.getString(R.string.resource_name); "

Other suggestions:

1. Any other file, including java, can use this priority to achieve different logical effects for different packages.

2. Logo,Configs.xmlThe parameters in manifestPlaceholders can also be defined to achieve different flavor effects, but they cannot produce different effects in a variety of combinations such as "icbc", "icbcRelease", "icbcDebug", "release", "debug".Also in java code, take the value from manifestPlaceholders to compareConfigs.xmlThe value is cumbersome.Therefore, it is recommended that you set up different folders separately, and it will be easier to delete the configuration of a bank in the future.

Posted by WindChill on Sat, 23 May 2020 10:26:31 -0700