Implementation of Multi channel Code Packaging in Android Studio

  • 2021-12-04 11:16:33
  • OfStack

1 set of codes achieves the following effects:

Packaging different applicationId can be installed on the same mobile phone at the same time Different logo and app names, Different third-party SDK access configurations (such as WeChat sharing appid and laser pushing appkey) Can distinguish between debug and release configurations Features Used: productFlavor and buildTypes

Principle: Priority buildTypes is greater than productFlavor

Example: One set of codes packages apk for two banks

1. Modify build. gradle. buildTypes can keep the default settings of debug and release, and add two productFlavor in android node.


flavorDimensions 'bank'
productFlavors {
 icbc {
 dimension = 'bank'
 applicationId='com.icbc.mobilebank
 manifestPlaceholders = [bankName: ' Industrial and Commercial Bank of China ']
 }
 ccb {
 dimension = 'bank'
 applicationId = 'com.cbc. mobilebank
 manifestPlaceholders = [bankName: ' China Construction Bank ']
 }
}

2. Modify src\ main\ AndroidManifest. xml to use the ${bankName} "And ${applicationId} Replace the corresponding position

2.1 Set the name of app, android: label = "${bankName}", and remember to add android: label to tools: replace. (App logoandroid: icon can also be distinguished by this method, but the latter method is more recommended.)


<application
android:name="com.dhcc.app.application.MyApplication"
android:allowClearUserData="true"
android:icon="@drawable/logo"
android:label="${bankName}"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
tools:replace="android:name,android:label"
tools:targetApi="n">

2.2 Set all android: authorities, for example


<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}. FileProvider "
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
 
<provider android:name="cn.jpush.android.service.DataProvider"
android:process=":pushcore"
android:authorities="${applicationId}.DataProvider" android:exported="false" />

2.3 refers to the third party sdk, for example, Aurora push 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" and "release" in src\ main sibling directory. When icbcRelease is selected to compile, the compiler will follow the "icbcRelease > release > icbc > main "compiles files with the same name.

4. Set the picture resource according to the priority above. Refer to main\ res\ drawable\ logo. png file level 1, add custom app logo to the folder. If you use the same logo in debug and release, only add it in icbc folder.

5. Set the string xml resource.

5.1 Create a new src\ main\ res\ values\ configs.xml, and place some parameters different from each bank. You can also append the configs. xml file directly to strings. xml without adding it.


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

5.2 Add the corresponding configs. xml to "icbc", "icbcRelease" and "release" according to the actual situation. You can only increase JPUSH_APPKEY, then WXAPPId will use the value of main.

5.3 Use "@ string/JPUSH_APPKEY" in xml files such as AndroidManifest. xml

5.4 How to use in the 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 recommendations:

1. Any other file, including java, can also use this priority to achieve the effect of different packets typing different logic.

2. The parameters in Logo, configs. xml can also be defined in manifestPlaceholders to achieve different effects of flavor, but they cannot produce different effects of various combinations such as "icbc", "icbcRelease", "icbcDebug", "release" and "debug". At the same time, taking the value of manifestPlaceholders in java code is more troublesome than taking the value of configs. xml. Therefore, it is recommended to establish different folders separately, and it is easy to delete the configuration of 1 bank in the future.

Summarize


Related articles: