All Products
Search
Document Center

How to switch among multiple development environments for mPaaS Android projects

Last Updated: Mar 10, 2021

Background

The mPaaS Android 60 baseline provides the ability to switch between multiple development environments. The mPaaS Android 60 baseline can help developers switch among multiple development environments, such as development DEV, test TEST, experience User Acceptance Testing (UAT), and online PROD.

Changes involving mPaaS

For mPaaS developers, the following changes of the configuration files are involved in switching the development environment:

  • mPaaS project configuration: Configure the.config file Ant-mpaas-xxx-Android.config in the project under the app directory of the portal. This file can be used to store service address and gateway service address for application pushing service. For details, see Creating an application in the console.
  • RPC data encryption configuration : Configure the mpaas_netconfig.properties file in the project under the assets directory of the portal. This file can be used to store RPC data encryption methods and public encryption keys. For details, see Data Encryption > Android Configuration
  • H5 container configuration: Configure the custom_config.json file in the project under the assets directory of the portal. This file can be used to store the switches of offline package signature verification and immersive title bar. For details, see H5 container configuration.
  • Public key for offline package signature verification: The public key is used to decrypt the offline package in the project. The public key is configured in the code and can be set through the Nebula component. For details, see Configure offline package.
  • UC SDK key : You need to apply this key when you use the UC kernel in the project. UC SDK key is configured in the AndroidManifest file. For details, see Add UC SDK.

When switching the development environment, you need to manually replace or modify the preceding configuration. The next section will introduce how to implement the switching between development environments through Gradle configuration.

Solution

To switch the development environment, you need to modify two types of configurations:

  • Pure configuration files: You can use the files for the configuration of the mPaaS project, RPC data encryption configuration, and H5 container configuration. The configuration files can be replaced by the Gradle script during the compilation.
  • Configuration in the code : You can configure keys such as public key for offline package signature verification, and UCSDK key. The keys can be written in the configuration files. The switching operation can be implemented by reading the configuration files.

Preparation

  • Under portal-app, create various development environment directories to store configuration files according to project requirements.
  • Copy the mPaaS project configuration of the corresponding environment to the directory.
  • If you need to switch to RPC data encryption configuration, copy mpaas_netconfig.properties to the corresponding directory.
  • If you need to switch to H5 container configuration, copy custom_config.json to the corresponding directory.

Switch the configuration file

Switch mPaaS project configuration

Configure the following code in the gradle of portal-app :

  1. //Configure the mPaaS App development environment and copy the .config configuration file to the main Module.
  2. def setAppConfigEnv(String type){
  3. File configFile = file("${rootDir}/app").listFiles().find{File f ->
  4. f.name.endsWith(".config")
  5. }
  6. if(configFile != null && configFile.exists()){
  7. delete(configFile)
  8. }
  9. copy {
  10. from "buildEnv/${type}"
  11. into "${rootDir}/app"
  12. include "**/*.config"
  13. }
  14. }

Switch encryption configuration of RPC data

Configure the following code in the gradle of portal-app :

  1. //Configure the development environment for RPC data encryption. Copy the configuration file to the assets resource directory.
  2. def setNetConfigEnv(String type){
  3. copy {
  4. from "buildEnv/${type}/mpaas_netconfig.properties"
  5. into "${rootDir}/app/src/main/assets"
  6. }
  7. }

Switch configuration of H5 container

Configure the following code in the gradle of portal-app :

  1. //Configure the development environment of the offline package. Copy the configuration file to the assets resource directory.
  2. def setNebulaEnv(String type){
  3. copy {
  4. from "buildEnv/${type}/custom_config.json"
  5. into "${rootDir}/app/src/main/assets/config"
  6. }
  7. }

Switch code configuration

Switch the public key for offline package signature verification

1.Write the key for offline package signature verification into the custom_config.json file of H5 container configuration :

  1. [
  2. {
  3. "value": "YES",
  4. "key": "h5_shouldverifyapp"
  5. },
  6. {
  7. "value": "your public key",
  8. "key": "h5_shouldverifyapp_pubkey"
  9. }
  10. ]

2.Obtain the public key for offline package signature verification by reading the H5 container configuration. Switch the public key for offline package signature verification by switching the H5 container configuration :

  1. /**
  2. * Analyze the public key for offline package signature verification according to the configuration file of the H5 container.
  3. * @return The public key for offline package signature verification
  4. */
  5. public String getAppVerificationPubkey(){
  6. String configStr = getFromAssets("config/custom_config.json");
  7. JSONArray jsonArray = JSON.parseArray(configStr);
  8. if(jsonArray != null && jsonArray.size() > 0){
  9. for (int i = 0; i < jsonArray.size(); i++) {
  10. JSONObject jsonObject = jsonArray.getJSONObject(i);
  11. String key = jsonObject.getString("key");
  12. if(key.equals("h5_shouldverifyapp_pubkey")){
  13. String value = jsonObject.getString("value");
  14. return value;
  15. }
  16. }
  17. }
  18. return null;
  19. }
  20. /**
  21. * Get the file content under the assets directory in portal
  22. * @param fileName The file name
  23. * @return File content
  24. */
  25. public String getFromAssets(String fileName){
  26. try {
  27. MicroApplicationContext context = MPFramework.getMicroApplicationContext();
  28. InputStreamReader inputReader = new InputStreamReader(context.getApplicationContext().getResources().getAssets().open(fileName));
  29. BufferedReader bufReader = new BufferedReader(inputReader);
  30. String line= "";
  31. String Result= "";
  32. while((line = bufReader.readLine()) != null)
  33. Result += line;
  34. return Result;
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. return null;
  39. }
  1. Set the public key for offline package signature verification:
  1. String pubkey = getAppVerificationPubkey();
  2. if(!TextUtils.isEmpty(pubkey)){
  3. MPNebula.enableAppVerification(pubkey);
  4. }

Switch UC SDK key

The UC SDK key is stored in AndroidManifest.xml:

  1. <application>
  2. ...
  3. <meta-data android:name="UCSDKAppKey" android:value="${GRADLE_APP_UCKEY}"/>
  4. ...
  5. </application>

Switch by configuring manifestPlaceholders in the gradle of portal-app</>:

  1. buildTypes {
  2. release {
  3. manifestPlaceholders = [GRADLE_APP_UCKEY : 'your release uc key']
  4. ...
  5. }
  6. debug {
  7. ...
  8. manifestPlaceholders = [GRADLE_APP_UCKEY : 'your debug uc key']
  9. ...
  10. }
  11. }

Switch development environment

Configure in the gradle of portal-app. After you call this method and pass in environment parameters, automatic switching can be implemented:

  1. // Switch the development environment according to environment types
  2. // type = dev/test/uat/prod can be expanded according to the configuration directory
  3. def switchBuildEnv(String type){
  4. setAppConfigEnv(type)
  5. if(file("buildEnv/${type}/custom_config.json").exists()){
  6. setNebulaEnv(type)
  7. }
  8. if(file("buildEnv/${type}/mpaas_netconfig.properties").exists()){
  9. setNetConfigEnv(type)
  10. }
  11. }

Sample code

Call the switch method in the gradle of portal-app:

  1. buildTypes {
  2. // Switch development environment
  3. switchBuildEnv('prod')
  4. release {
  5. ...
  6. }
  7. debug {
  8. ...
  9. }
  10. }

Click here to download the code sample.