确认启动配置文件优化
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 Android Studio 或查看 R8 build 元数据来检查启动配置文件是否正常运行。
使用 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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."]]