Initialize mPaaS

更新时间:
复制 MD 格式

mPaaS initialization requires modifying the Application object. The steps differ depending on whether you use the hotpatching feature.

Without the hotpatching feature

Prerequisites: Your app has a custom Application class. No obfuscation configuration is required for this path.

In your Application class, override onCreate() and call MP.init():

@Override
public void onCreate() {
    super.onCreate();

    MP.init(
      this,
      MPInitParam.obtain().setCallback(new MPInitParam.MPCallback() {
        @Override
        public void onInit() {
            Log.d("TAG", "mPaaS init finished");
        }
      })
    );
}
Note
  • If you are integrating with Kotlin, use the mPaaS KTX extension. For more information, see mPaaS Kotlin extension.

  • If you continue to use the QuinoxlessFramework initialization method, your calls are not affected. No changes to your code or business logic are required.

Important

Do not filter processes before calling MP.init. The initialization code must run in the main process, the tools child process, and the push child process.

Using the hotpatching feature

Prerequisites: Your project has ProGuard or R8 configured. The @Keep annotation is available.

  1. Modify the Application object to inherit from QuinoxlessApplicationLike. Annotate the class with @Keep to prevent obfuscation.

    This example uses MyApplication as the class name:

    @Keep  // Required: prevents ProGuard/R8 from obfuscating this class
    public class MyApplication extends QuinoxlessApplicationLike implements Application.ActivityLifecycleCallbacks {
    
      private static final String TAG = "MyApplication";
    
      @Override
      protected void attachBaseContext(Context base) {
          super.attachBaseContext(base);  // Required: mPaaS hooks into base context here
          Log.i(TAG, "attacheBaseContext");
      }
    
      @Override
      public void onCreate() {
          super.onCreate();
          Log.i(TAG, "onCreate");
          registerActivityLifecycleCallbacks(this);
      }
    
      @Override
      public void onMPaaSFrameworkInitFinished() {
          // Called when the mPaaS framework finishes initialization
          LoggerFactory.getTraceLogger().info(TAG, getProcessName());
      }
    
      @Override
      public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
          Log.i(TAG, "onActivityCreated");
      }
    
      @Override
      public void onActivityStarted(Activity activity) {
    
      }
    
      @Override
      public void onActivityResumed(Activity activity) {
    
      }
    
      @Override
      public void onActivityPaused(Activity activity) {
    
      }
    
      @Override
      public void onActivityStopped(Activity activity) {
    
      }
    
      @Override
      public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    
      }
    
      @Override
      public void onActivityDestroyed(Activity activity) {
    
      }}
  2. In the AndroidManifest.xml file, set the application to the mPaaS-provided Application object, then register MyApplication under the meta-data key mpaas.quinoxless.extern.application:

     <application
     android:name="com.alipay.mobile.framework.quinoxless.QuinoxlessApplication" >
     <meta-data
              android:name="mpaas.quinoxless.extern.application"
              android:value="com.mpaas.demo.MyApplication"
              />
     </application>
  3. Import the Apache HTTP client.

    The RPC and hotpatching features require the Apache HTTP client. Add the following code to the AndroidManifest.xml file. For more information, see Use the Apache HTTP client.

    <uses-library android:name="org.apache.http.legacy" android:required="false"/>