遷移免安裝應用程式來支援 Android App Bundle

如果您的 Android 免安裝應用程式模組仍會使用已淘汰的 Android Gradle 外掛程式 (com.android.feature) 功能,則必須改用基礎應用程式外掛程式 (com.android.application) 和動態功能外掛程式 (com.android.dynamic-feature)。

在 Android Gradle 外掛程式 3.3.0 以上版本中,基本應用程式外掛程式支援免安裝體驗。也就是說,如果基本應用程式模組符合免安裝體驗的需求,就會自動獲得此優勢。接著,您可以使用動態功能外掛程式,加入額外功能,讓使用者可以隨選下載。這項設定可讓您更輕鬆地透過單一專案支援安裝版和免安裝應用程式體驗,並受益於透過 Android App Bundle 發布應用程式。

下表進一步說明瞭遷移的外掛程式:

模組說明 舊版外掛程式 目前的外掛程式
這個模組包含基本程式碼、資源和功能,適用於已安裝或免安裝應用程式體驗。 com.android.feature (具有 baseFeature = true) com.android.application

注意:這個模組包含建構應用程式並封裝為 Android App Bundle 或 APK 所需的所有資訊清單和簽署資訊。

其他模組化功能,供使用者隨選下載 com.android.feature com.android.dynamic-feature (在模組資訊清單中包含 dist:instant="true"dist:onDemand="false")
只有已安裝版本應用程式可以使用的功能的程式碼和資源。 com.android.application com.android.dynamic-feature (在模組資訊清單中包含 dist:instant="false"dist:onDemand="false")

本頁面說明如何遷移現有的免安裝應用程式專案,以便建構支援免安裝的 Android App Bundle。以及如何建構、測試及發布免安裝即用的 Android App Bundle。

如果您要為應用程式建立新的免安裝體驗,請改為參閱「建立免安裝即用功能模組」。

瞭解異動內容

如果您要遷移專案改為使用動態功能外掛程式,Android App Bundle 可讓您透過新的方式建構及發布應用程式,大幅簡化向使用者發布最佳化 APK 的流程。

應用程式套件會封裝應用程式的所有已編譯程式碼和資源以便上傳,藉此簡化發布程序,但會延後產生及簽署 APK 並寫入 Google Play。上傳完畢後,Google Play 的新應用程式提供模型就會使用應用程式套件,根據各個使用者的裝置設定產生並提供經過最佳化的 APK,讓使用者只下載執行應用程式所需的程式碼和資源。您不再需要為不同裝置建構、簽署及管理多個 APK,而且使用者下載的內容不但檔案較小,還會經過最佳化處理。

使用現在淘汰的功能外掛程式時,建構免安裝應用程式需要建立基本功能模組,其中包含所有模組 (包括免安裝應用程式模組) 的共用程式碼和資源。程式碼的其餘部分包含在多個非基本功能模組中,其中包含免安裝體驗的進入點。對於已安裝的應用程式版本,您的專案可能會包含一個獨立的應用程式模組,其中包含安裝應用程式所需的程式碼和活動。

遷移應用程式以支援 Android App Bundle 時,應用程式模組會收回基本模組的角色,並將額外安裝或免安裝體驗整理為功能模組。也就是說,您的專案目前更類似標準應用程式專案,包含免安裝即用的基本模組,且能夠納入其他模組化免安裝體驗。

如要遷移現有的免安裝應用程式專案,並採用 Android App Bundle 更最佳化的發行模型,請按照下方各節的步驟操作。

將基本功能模組轉換為應用程式模組

您必須先編輯基本功能模組的 build.gradle 檔案,才能將檔案轉換為主要應用程式模組,如下所示:

  1. 刪除 baseFeature true 行。
  2. 移除使用 featureapplication 依附元件設定的所有依附元件。

    Groovy

    dependencies {
        ...
        // delete any lines that look like
        // feature project(":foo")
        // feature project(":bar")
        // application project(":app")
    }
    

    Kotlin

    dependencies {
        ...
        // delete any lines that look like
        // feature(project(":foo"))
        // feature(project(":bar"))
        // application(project(":app"))
    }
    
  3. applicationId 及其他預期應用程式模組中的建構指令碼設定,從目前的 com.android.application 模組移至 com.android.feature 模組。以下提供一些範例。在這個步驟中,視您的特定 build.gradle 設定而定,比較從先前應用程式模組複製 build.gradleandroid 區塊,並貼到新應用程式模組的 build.gradle 檔案中,可能較為容易。不過,這麼做時請務必小心。

    Groovy

    android {
        ...
        defaultConfig {
            // You need to move the application ID from the app module
            // to your feature module.
            applicationId "com.example.myapp"
            ...
        }
        // Some additional build configurations you might want to
        // copy from your current ‘app’ module may include ProGuard
        // rules and code shrinking settings.
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile(
                  'proguard-android-optimize.txt'),
                  'proguard-rules.pro'
            }
        }
    }
    

    Kotlin

    android {
        ...
        defaultConfig {
            // You need to move the application ID from the app module
            // to your feature module.
            applicationId = "com.example.myapp"
            ...
        }
        // Some additional build configurations you might want to
        // copy from your current ‘app’ module may include ProGuard
        // rules and code shrinking settings.
        buildTypes {
            getByName("release") {
                minifyEnabled = true
                proguardFiles(
                    getDefaultProguardFile("proguard-android-optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }
    }
    
  4. 在資訊清單中新增適當的組合發布標記,即可將功能模組標示為免安裝即用功能,如下所示。

    <manifest ... xmlns:dist="http://schemas.android.com/apk/distribution">
        <dist:module dist:instant="true" />
        ...
    </manifest>
    
  5. 將功能模組變更為 com.android.application,將功能模組轉換為基本應用程式模組:

    Groovy

    // Replace  "plugins { id 'com.android.feature' }"
    // with the following
    plugins {
      id 'com.android.application'
    }
    

    Kotlin

    // Replace  "plugins { id("com.android.feature") }"
    // with the following
    plugins {
        id("com.android.application")
    }
    

將舊的應用程式模組轉換為安裝期間功能模組

如果舊版應用程式模組中沒有程式碼或資源,您可以直接刪除,因為您在上一節所述的步驟已將功能模組轉換為應用程式的基本應用程式模組。

不過,如果您的舊版應用程式模組中有用來代表使用者安裝應用程式的功能和資源,請按照本節的步驟,將應用程式模組轉換為功能模組。

建立功能模組時,必須將外掛程式類型從 com.android.application 變更為 com.android.dynamic-feature,以及其他 build.gradle 變更,如下所示:

  1. 將外掛程式類型從 com.android.application 變更為 com.android.dynamic-feature

    Groovy

    // Replace "plugins { id 'com.android.feature' }"
    // with the following:
    plugins {
      id 'com.android.dynamic-feature'
    }
    

    Kotlin

    // Replace "plugins { id("com.android.application") }"
    // with the following:
    plugins {
        id("com.android.dynamic-feature")
    }
    
  2. 如上一節所述,請確認您已將 com.android.application 外掛程式所需的建構設定移至基礎應用程式模組,例如 applicationIdproguardFiles 規則。

  3. 將模組重新命名為「installed_feature」:

    1. 從選單列中依序選取「View」>「Tool Windows」>「Project」,開啟「Project」窗格。
    2. 在功能模組上按一下滑鼠右鍵,然後依序選取「Refactor」>「Rename」
    3. 在隨即顯示的對話方塊中,選取「Rename module」,然後按一下「OK」
    4. 輸入模組的新名稱,然後按一下「OK」
  4. 與步驟 3 類似,將您在上一節建立的新應用程式模組重新命名為「應用程式」之類的名稱。

  5. 在功能模組的 build.gradle 檔案中,新增「應用程式」模組的實作依附元件,如下所示。

    Groovy

    dependencies {
        ...
        // In the feature module, add an implementation dependency
        // on the base app module.
        implementation project(":app")
    }
    

    Kotlin

    dependencies {
        ...
        // In the feature module, add an implementation dependency
        // on the base app module.
        implementation(project(":app"))
    }
    
  6. 將功能加入新應用程式模組的 build.gradle 檔案。

    Groovy

    android {
        ...
        // In the base app module, specify each feature module.
        dynamicFeatures = [":installed_feature"]
    }
    

    Kotlin

    android {
        ...
        // In the base app module, specify each feature module.
        dynamicFeatures.addAll(listOf(":installed_feature"))
    }
    
  7. 在功能模組的資訊清單中,將適當的套件發布標記新增至資訊清單,即可將功能模組標示為可安裝模組。

    <manifest ... xmlns:dist="http://schemas.android.com/apk/distribution">
        <dist:module dist:instant="false" dist:onDemand="false"
                dist:title="@string/title_dynamic_feature">
            <dist:fusing dist:include="true" />
        </dist:module>
        ...
    </manifest>
    

將其他功能模組轉換為免安裝即用功能模組

如果您已將應用程式的額外功能模組化為多個功能模組,請按照本節所述步驟將這些模組轉換為免安裝即用功能模組。

針對專案中的每個其餘功能模組,請按照下列步驟將其轉換為免安裝即用功能:

  1. build.gradle 檔案中的外掛程式類型變更為 com.android.dynamic-feature,如下所示:

    Groovy

    // Replace 'com.android.feature' with 'com.android.dynamic-feature'
    plugins {
      id 'com.android.dynamic-feature'
    }
    

    Kotlin

    // Replace "com.android.feature" with "com.android.dynamic-feature"
    plugins {
        id("com.android.dynamic-feature")
    }
    
  2. 將以下內容新增至資訊清單,即可將每個功能模組標示為免安裝即用功能。

    <manifest ... xmlns:dist="http://schemas.android.com/apk/distribution">
        <dist:module dist:instant="true" dist:onDemand="false"
                dist:title="@string/title_dynamic_feature">
            <dist:fusing dist:include="true" />
        </dist:module>
        ...
    </manifest>
    
  3. 將功能模組新增至新應用程式模組的 build.gradle 檔案,並在該檔案將 installed_feature 新增至功能模組清單。

    Groovy

    android {
       ...
       dynamicFeatures = [":installed_feature", ":feature_1", ":feature_2"]
       // or whichever name exists for the instant enabled feature module
    }
    

    Kotlin

    android {
       ...
       dynamicFeatures.addAll(listOf(":installed_feature", ":feature_1", ":feature_2"))
       // or whichever name exists for the instant enabled feature module
    }
    

建構、測試及發布新的免安裝即用應用程式套件

完成本頁中的步驟後,您的專案即可產生單一構件 (Android App Bundle),用於將應用程式安裝和免安裝版本發布到 Google Play 管理中心,並分別推出免安裝和已安裝測試群組。此外,應用程式套件也能讓您依照每位使用者的裝置設定提供最佳化 APK,讓使用者只下載執行應用程式所需的程式碼和資源。也就是說,您不再需要為不同裝置建構、簽署及管理多個 APK,而且使用者下載的內容不但檔案較小,還會經過最佳化處理。

如要開始建構及測試免安裝即用應用程式套件,請參閱「建構應用程式套件」。