全部產品
Search
文件中心

Mobile Platform as a Service:載入架構與定製

更新時間:Jul 13, 2024

mPaaS Android 架構提供了一整套的載入邏輯。基於此架構,研發團隊可以進行多業務線開發。本文描述架構的啟動流程以及如何在架構下添加自己的代碼以對接啟動。

啟動流程

Application

傳統 Android apk 運行時首先載入 AndroidManifest 檔案 application 節點中 android:name 配置的 Application。

由於 mPaaS Android 架構重寫了載入流程, android:name 中配置 mPaaS Android 架構的 com.alipay.mobile.quinox.LauncherApplication 類。

<application
    android:name="com.alipay.mobile.quinox.LauncherApplication"
    android:allowBackup="true"
    android:debuggable="true"
    android:hardwareAccelerated="false"
    android:icon="@drawable/appicon"
    android:label="@string/name"
    android:theme="@style/AppThemeNew" >
</application>

啟動頁

由於架構載入 bundle 可能會比較耗時,因此需要一個啟動頁等待架構啟動完成之後再跳轉到程式首頁,所以在 AndroidManifest 檔案中配置了架構提供的 com.alipay.mobile.quinox.LauncherActivity 應用啟動頁。

配置如下:

<activity
android:name="com.alipay.mobile.quinox.LauncherActivity"
android:configChanges="orientation | keyboardHidden | navigation"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

為方便開發人員對啟動過程的理解,也為了避免啟動過程被誤改誤刪、被幹擾,mPaaS 的啟動過程被適度封裝。因此,上述的 LauncherApplicationLauncherActivity 對開發人員完全不可見。

為了讓用戶端 App 在啟動過程中實現自身的初始化邏輯,mPaaS 設計了 LauncherApplicationAgentLauncherActivityAgent 代理。作為開發人員,您可以通過繼承這兩個類,在相應的回調中實現自身的初始化邏輯。當您在 bundle 工程中定義了這兩個類時,在使用 ProGuard 進行代碼混淆的時候則需要對這兩個類進行防混淆設定,詳情請參見 混淆 Android 檔案

啟動流程圖

mPaaS Android 架構載入流程如下:2

  1. 架構啟動後主線程會建立啟動頁 LauncherActivity ,然後回調 LauncherActivityAgentpreInit 方法。

  2. 架構進行 multidex。過程中會回調 LauncherApplicationAgentpreInit 方法,讀取當前 .apk 中每個 bundle 的描述檔案,並對每個 bundle 建立對應的類載入器,載入其中的資源檔。

  3. 初始化完成後,回調 LauncherActivityAgentLauncherApplicationAgentpostInit 方法。

定製

實際上,架構已在 Launcher 工程中自動建立了兩個類 MockLauncherApplicationAgentMockLauncherActivityAgent 繼承了 LauncherApplicationAgentLauncherActivityAgent 這兩個回調介面。在架構的初始化過程中,它們分別在 LauncherApplictionLauncherActivity 中被調用。

在 Portal 的 AndroidManifest.xml 中配置如下。開發人員也可在 Bundle 中實現這兩個代理類,在以上配置中修改對應 meta-datavalue 值:

     <application
          android:name="com.alipay.mobile.quinox.LauncherApplication" >

         <!-- Application 的回調配置 -->
         <meta-data
            android:name="agent.application"
            android:value="com.mpaas.demo.launcher.framework.MockLauncherApplicationAgent"/>

        <!-- Activity 的回調配置 -->
        <meta-data
            android:name="agent.activity"
            android:value="com.mpaas.demo.launcher.framework.MockLauncherActivityAgent"/>
        <!-- 啟動頁的布局配置 -->
        <meta-data
            android:name="agent.activity.layout"
            android:value="layout_splash"/>

     </application>

代理類

agent.application 配置的是啟動過程代理 ApplicationAgent,如下所示:

  public class MockLauncherApplicationAgent extends LauncherApplicationAgent {
      @Override
      protected void preInit() {
          super.preInit();
          //架構初始化前
      }

      @Override
      protected void postInit() {
          super.postInit();
          //架構初始化後
      }
  }

用戶端 App 可以在 LauncherApplicationAgent 的實作類別中,進行一些 Application 層級的初始化工作。preInit() 回調發生在架構初始化之前,因此不要在這裡調用架構(MicroApplicationContext)的相關介面。而 postInit() 回調發生在架構初始化完成之後,是可以使用的。

agent.activity 配置的是啟動 Activity 的代理,如下所示:

public class MockLauncherActivityAgent extends LauncherActivityAgent {

        @Override
        public void preInit(Activity activity) {
                super.preInit(activity);
                //Launcher Activity 啟動前
        }

        @Override
        public void postInit(final Activity activity) {
            super.postInit(activity);
                //Launcher Activity 啟動後
                //跳轉程式首頁的邏輯
                startActivity(activity,YOUR_ACTIVITY);
        }
}

同上述的 LauncherApplicationAgent 類似,LauncherActivityAgent 的兩個回調也分別發生在架構初始化之前和之後,使用方法也類似。

修改啟動頁布局

在 Portal 的 AndroidManifest.xml 中還配置了啟動頁的布局檔案,如下所示:

<application
android:name="com.alipay.mobile.quinox.LauncherApplication" >
<!-- 啟動頁的布局配置 -->
<meta-data
android:name="agent.activity.layout"
android:value="layout_splash"/>

</application>

修改 value 值為自訂的布局檔案名稱。

說明

需要把布局檔案和引用的相關資源放置在 Portal 工程裡。