All Products
Search
Document Center

Mobile Platform as a Service:How to switch among multiple development environments for mPaaS Android projects

Last Updated:Jun 12, 2023

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 Create mPaaS 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.

  • HTML5 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 HTML5 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.

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 HTML5 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. 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 HTML5 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 :

//Configure the mPaaS App development environment and copy the .config configuration file to the main Module.
def setAppConfigEnv(String type){
    File configFile = file("${rootDir}/app").listFiles().find{File f ->
        f.name.endsWith(".config")
    }
    if(configFile != null && configFile.exists()){
        delete(configFile)
    }

    copy {
        from "buildEnv/${type}"
        into "${rootDir}/app"
        include "**/*.config"
    }
}

Switch encryption configuration of RPC data

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

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

Switch configuration of HTML5 container

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

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

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 HTML5 container configuration :

[
  {
    "value": "YES",
    "key": "h5_shouldverifyapp"
  },
  {
    "value": "your public key",
    "key": "h5_shouldverifyapp_pubkey"
  }
]

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

   /**
         * Analyze the public key for offline package signature verification according to the configuration file of the HTML5 container.
         * @return The public key for offline package signature verification
         */
        public String getAppVerificationPubkey(){
            String configStr = getFromAssets("config/custom_config.json");
            JSONArray jsonArray = JSON.parseArray(configStr);
            if(jsonArray != null && jsonArray.size() > 0){
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String key = jsonObject.getString("key");
                    if(key.equals("h5_shouldverifyapp_pubkey")){
                        String value = jsonObject.getString("value");
                        return value;
                    }
                }
            }
            return null;
        }

        /**
         * Get the file content under the assets directory in portal  
         * @param fileName The file name
         * @return File content
         */
        public String getFromAssets(String fileName){
            try {
                MicroApplicationContext context = MPFramework.getMicroApplicationContext();
                InputStreamReader inputReader = new InputStreamReader(context.getApplicationContext().getResources().getAssets().open(fileName));
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line= "";
                String Result= "";
                while((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
  1. Set the public key for offline package signature verification:

String pubkey = getAppVerificationPubkey();
    if(!TextUtils.isEmpty(pubkey)){
        MPNebula.enableAppVerification(pubkey);
    }

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:

// Switch the development environment according to environment types
// type = dev/test/uat/prod can be expanded according to the configuration directory
def switchBuildEnv(String type){
    setAppConfigEnv(type)
    if(file("buildEnv/${type}/custom_config.json").exists()){
        setNebulaEnv(type)
    }
    if(file("buildEnv/${type}/mpaas_netconfig.properties").exists()){
        setNetConfigEnv(type)
    }
}

Sample code

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

buildTypes {
     // Switch development environment
    switchBuildEnv('prod')
    release {
        ...
    }
    debug {
        ...
    }
}

Click here to download the code sample.