Legacy Dynamic Environment Switching

Updated at:

Dynamic environment switching lets you change an application's environment on a mobile device without repackaging the client.

Note
  • The dynamic environment switching feature is only available in Apsara Stack environments.

  • Dynamic environment switching is suitable for development scenarios that involve managing and frequently switching between multiple environments.

  • When you use dynamic environment switching, a new environment configuration file is written to the application. Therefore, you must request file storage permissions for your application.

Because updating the environment configuration modifies the Security Guard signature verification image yw_1222.jpg, the mPaaS security signature verification mechanism imposes two limitations on dynamic environment switching:

  • This feature is for development use only. Before you release your application, remove the corresponding configurations. Otherwise, the release package will throw a RuntimeException.

  • You must disable the network request signature verification switch in the mPaaS console. Otherwise, requests will fail due to a mismatch in the signature verification image.

    0

Add the dynamic environment switching SDK

  1. Add the dependencies.

    • For the native AAR-based approach, add the following dependency to the dependencies block in the build.gradle file of your project's main module:

      dependencies {
      ···
      implementation 'com.mpaas.mocksettings:mocksettings-build:10.1.60a.1575@aar'
      ···
      }
    • For the portal and bundle-based approach, add the following dependencies to the dependencies block in the build.gradle file of your portal project's main module:

      dependencies {
      ···
      bundle 'com.mpaas.mocksettings:mocksettings-build:1.0.0.200421111458@jar'
      manifest 'com.mpaas.mocksettings:mocksettings-build:1.0.0.200421111458:AndroidManifest@xml'
      ···
      }
  2. Use the SDK.

    • In the native AAR-based approach, override the getPackageManager method in the Application class to replace PackageManager with MockSettingsPackageManager.

      private MockSettingsPackageManager mockSettingsPackageManager;
      
      @Override
      public PackageManager getPackageManager() {
          if (mockSettingsPackageManager == null) {
              mockSettingsPackageManager = new MockSettingsPackageManager(this, super.getPackageManager());
          }
          return mockSettingsPackageManager;
      }
    • For the portal and bundle-based approach, modify the application element in the AndroidManifest.xml file of your portal project's main module as follows:

      <application
           android:name="com.alipay.mobile.quinox.MockSettingsLauncherApplication"
           ···
           >
           ···
      </application>
  3. Add the following permission and ensure it is dynamically requested at runtime:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  4. Build a debug package or enable debugging in the AndroidManifest.xml file:

    <application
         android:debuggable="true"
         ···
         >
         ···
    </application>

Switch environments

  1. Scan the QR code to download the mPaaS Settings app.

    1636438936 After installation, the mPaaS Settings app icon appears as follows:

    ECCC694D-374F-4ff2-B6CF-8F4E0BE592EF

  2. Place the config file downloaded from the mPaaS console in your phone's SD card or internal storage.

  3. Add environments. Use the mPaaS Settings app to add the config file to the list.

    1. Open the mPaaS Settings app.

    2. On the Environment List screen, tap Add environment configuration file at the bottom.

    3. Locate the environment configuration file.

    4. Add the configuration files for the production and test environments to the environment list.

  4. Switch environments.

    1. From the list, select an environment and tap Switch to set it as the current environment.

    2. Then, launch the corresponding application. If test requests are sent successfully, the switch is successful.

      When you switch to another environment and then restart the application, the system reports a 3000 error because the required operationType does not exist in the new environment. This is the expected behavior after a successful switch.