確認啟動設定檔最佳化
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Android Studio 或查看 R8 版本中繼資料,確認啟動設定檔是否正常運作。
透過 Android Studio 確認
如要確認 DEX 版面配置最佳化功能,請使用 Android Studio 開啟 APK,並驗證 DEX 檔案中的類別。請確認主要 classes.dex
並未完全填滿。如果應用程式包含單一 DEX 檔案,您可以在啟用啟動設定檔後,檢查應用程式是否包含兩個 DEX 檔案。
如果啟動類別不符合單一 DEX 檔案的限制,Android Studio 會發出警告。如要取得包含啟動類別中非啟動方法數量的診斷資訊,請在套用啟動設定檔時,對 settings.gradle
檔案進行以下變更,確保 R8 編譯器至少已更新至 8.3.36-dev 版:
Kotlin
pluginManagement {
buildscript {
repositories {
mavenCentral()
maven {
url = uri("https://storage.googleapis.com/r8-releases/raw")
}
}
dependencies {
classpath("com.android.tools:r8:8.3.6-dev")
}
}
}
Groovy
pluginManagement {
buildscript {
repositories {
mavenCentral()
maven {
url uri('https://storage.googleapis.com/r8-releases/raw')
}
}
dependencies {
classpath 'com.android.tools:r8:8.3.6-dev"
}
}
}
使用 Gradle 建構時,請務必在下列指令的 assembleRelease
後方新增 --info
。
./gradlew assembleRelease --info
然後,診斷結果會列印到終端機。
如果應用程式或任何程式庫參照任何去除糖衣的 API,這些類別的內含相容性實作項目一律會包含在最後一個 DEX 檔案中。這個經過去除糖化的最後 DEX 檔案不會參與 DEX 版面配置最佳化作業。
從 AGP 8.8 開始,R8 會在 Android App Bundle (AAB) 中輸出中繼資料,您可以使用這些資料檢查 DEX 版面配置最佳化是否成功。如要確認最佳化是否有效,請按照下列步驟操作:
建構應用程式的 AAB:
./gradlew app:bundleRelease
檢查是否至少有一個 DEX 檔案包含文字 "startup": true
。
開啟中繼資料:
unzip -j -o path-to-aab BUNDLE-METADATA/com.android.tools/r8.json && jq .dexFiles r8.json
AAB 的路徑可能類似 app/build/outputs/bundle/release/app-release.aab
。
檢查輸出內容,應該會類似下列內容:
inflating: r8.json
[
{
"checksum": "f0b4b0ddb295812607f44efe03cf7a830056ccfddbdb81db3760d2281940e951",
"startup": true
}
]
如果中繼資料中只顯示 "startup": false
,您需要啟用啟動設定檔,並確保啟動設定檔未經過模糊處理。
請確認中繼資料中的 SHA-256 值與 AAB 中的值相符。如要取得所有 DEX 檔案的 SHA-256 值,請執行以下操作:
unzip -o path-to-aab */dex/*.dex && sha256sum */dex/*
輸出內容應如下所示:
Archive: app/build/outputs/bundle/release/myapp-release.aab
inflating: base/dex/classes.dex
f0b4b0ddb295812607f44efe03cf7a830056ccfddbdb81db3760d2281940e951 base/dex/classes.dex
比較雜湊值與步驟 1 中的「總和檢查碼」值。如果 SHA-256 值不相符,則可能有編譯步驟會干擾 R8 輸出 DEX 檔案的功能。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Confirm Startup Profiles optimization\n\nYou can check that your Startup Profiles are working using either Android Studio\nor looking at the R8 build metadata.\n\nConfirm with Android Studio\n---------------------------\n\nTo confirm DEX layout optimization, use Android Studio to open the APK and\nverify the classes in the DEX files. Make sure the primary `classes.dex` isn't\ncompletely filled. If your app consists of a single DEX file, you can check\nwhether the app contains two DEX files after enabling the Startup Profile.\n\nAndroid Studio warns you if the startup classes don't fit in a single DEX file.\nTo get diagnostic information that includes the amount of non-startup methods in\nthe startup classes, make sure the R8 compiler is updated to at least version\n8.3.36-dev by making the following changes to the `settings.gradle` file when\nyou apply the Startup Profile: \n\n### Kotlin\n\n```kotlin\npluginManagement {\n buildscript {\n repositories {\n mavenCentral()\n maven {\n url = uri(\"https://storage.googleapis.com/r8-releases/raw\")\n }\n }\n dependencies {\n classpath(\"com.android.tools:r8:8.3.6-dev\")\n }\n }\n}\n```\n\n### Groovy\n\n```groovy\npluginManagement {\n buildscript {\n repositories {\n mavenCentral()\n maven {\n url uri('https://storage.googleapis.com/r8-releases/raw')\n }\n }\n dependencies {\n classpath 'com.android.tools:r8:8.3.6-dev\"\n }\n }\n}\n```\n\nMake sure you add `--info` after\n`assembleRelease` in the following command when building with Gradle. \n\n ./gradlew assembleRelease --info\n\nThe diagnostic is then printed to the terminal.\n\nIf your app or any libraries reference any [desugared APIs](/studio/write/java8-support#library-desugaring), the bundled\ncompatibility implementations of these classes are always contained in the last\nDEX file. This desugared last DEX file doesn't participate in DEX layout\noptimizations.\n\nConfirm with bundle metadata\n----------------------------\n\n| **Note:** This workflow has only been tested on Linux.\n\nStarting with AGP 8.8, R8 outputs metadata in your Android App Bundle (AAB) that\nyou can use to check if the DEX layout optimization was successful. To check if\nthe optimization worked, do the following:\n\n1. Build your app's AAB:\n\n ./gradlew app:bundleRelease\n\n2. Check that there's at least one DEX file that contains the text\n `\"startup\": true`.\n\n 1. Open the metadata:\n\n unzip -j -o \u003cvar translate=\"no\"\u003epath-to-aab\u003c/var\u003e BUNDLE-METADATA/com.android.tools/r8.json && jq .dexFiles r8.json\n\n The path to your AAB might be something like\n `app/build/outputs/bundle/release/app-release.aab`.\n 2. Check the output, which should look something like this:\n\n inflating: r8.json\n [\n {\n \"checksum\": \"f0b4b0ddb295812607f44efe03cf7a830056ccfddbdb81db3760d2281940e951\",\n \"startup\": true\n }\n ]\n\n If you only see `\"startup\": false` in the metadata, you need to\n [enable startup profiles](/topic/performance/baselineprofiles/dex-layout-optimizations#create-startup) and ensure that your startup\n profile isn't obfuscated.\n3. Check that the SHA-256 values from the metadata match those from the AAB. To\n get the SHA-256 values for all your DEX files run the following:\n\n unzip -o \u003cvar translate=\"no\"\u003epath-to-aab\u003c/var\u003e */dex/*.dex && sha256sum */dex/*\n\n The output should look something like this: \n\n Archive: app/build/outputs/bundle/release/myapp-release.aab\n inflating: base/dex/classes.dex\n f0b4b0ddb295812607f44efe03cf7a830056ccfddbdb81db3760d2281940e951 base/dex/classes.dex\n\n Compare the hash values to the \"checksum\" values from step 1. If the SHA-256\n values don't match, there might be a compilation step interfering with R8's\n ability to output DEX files."]]