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

警告:Google Play 免安裝應用程式將停止提供服務。2025 年 12 月起,您無法透過 Google Play 發布免安裝應用程式,且所有 Google Play 服務免安裝 API 都將停止運作。Play 不會再透過任何機制向使用者提供免安裝應用程式。

我們根據開發人員的意見,並持續投入資源改善 Google Play Instant 推出後的生態系統,做出這項變更。

如要持續促進使用人數成長,建議開發人員使用深層連結,將使用者導向標準版應用程式或遊戲,並在適當情況下將他們重新導向至特定路徑或功能。

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

在 Android Gradle 外掛程式 3.3.0 以上版本中,基礎應用程式外掛程式支援免安裝應用程式。也就是說,如果基礎應用程式模組符合免安裝體驗的條件,您就能自動享有這項優勢。然後,您可以使用 Dynamic Feature 外掛程式,加入使用者可視需要下載的額外功能,做為即時體驗。這種設定可讓您更輕鬆地透過單一專案支援安裝版和免安裝應用程式體驗,並透過 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 應用程式套件。本文也會說明如何建構、測試及發布免安裝的 Android App Bundle。

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

瞭解異動內容

將專案遷移至使用 Dynamic Feature 外掛程式後,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 設定而定,將先前應用程式模組的 android 區塊從 build.gradle 複製並貼到新應用程式模組的 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. 在隨即顯示的對話方塊中,選取「重新命名模組」,然後按一下「確定」
    4. 輸入模組的新名稱,然後按一下「確定」
  4. 與步驟 3 類似,將您在上一個部分建立的新應用程式模組重新命名為「app」等名稱。

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

    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 應用程式套件),您可以使用這個構件,將應用程式的已安裝和免安裝版本發布至 Google Play 管理中心,並分別為免安裝和已安裝測試群組推出版本。此外,應用程式套件還能為每位使用者的裝置設定提供最佳化 APK,因此使用者只會下載執行應用程式所需的程式碼和資源。也就是說,您不必再為了支援不同裝置而建立、簽署及管理多個 APK,使用者也能獲得更小、更最佳化的下載內容。

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