在不使用 Gradle 的情况下构建 Microbenchmark
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍在使用 Microbenchmark 库时如何配置非 Gradle 构建系统。
虽然 Microbenchmark 库随附的 Gradle 插件可直接与 Android Gradle 插件集成,但您也可以在其他构建系统(例如 Bazel 或 Buck)中使用 Microbenchmark 库。
插桩
通过在测试清单的插桩代码块中指定 AndroidBenchmarkRunner
或子类,可将其用作插桩测试运行程序:
<manifest
package="com.example.library.test" ...>
<instrumentation android:name="androidx.benchmark.junit4.AndroidBenchmarkRunner" />
...
</manifest>
为了获得准确的测量结果,基准测试不得设置为可调试。如未正确设置可调试标志,该库会抛出错误,而不会报告无效的结果。在本地运行期间,此设置可能需要切换为 debuggable=true
,才能与 Android Studio 性能分析器搭配使用。
您可将 Microbenchmark 构建为通过以下两种方式运行:在自插桩 APK 中运行,或使用一个测试 APK 对另一个 APK 进行插桩测试。
自插桩 APK
如果使用自插桩 APK(作为 Gradle 针对 com.android.library
中 androidTest
目录的输出),则必须在单个 APK 的 Android 清单中停用可调试属性:
<manifest
package="com.example.library.test" ...>
<instrumentation
android:name="androidx.benchmark.junit4.AndroidBenchmarkRunner"
android:targetPackage="com.example.library.test"/>
<application android:debuggable="false"/>
</manifest>
使用测试 APK 对应用 APK 进行插桩测试
如果您的 build 会输出两个 APK,即应用 APK 和测试 APK(作为 Gradle 针对 com.android.app
软件包中 androidTest
目录的输出),则必须将应用 APK 设为 debuggable=false
。Android 操作系统会忽略测试 APK 的可调试标志。
<!-- Test manifest. -->
<manifest
package="com.example.android.app.test" ...>
<instrumentation
android:name="androidx.benchmark.junit4.AndroidBenchmarkRunner"
android:targetPackage="com.example.android.app"/>
<!-- This debuggable is ignored by the OS. -->
</manifest>
<!-- App being tested. -->
<manifest
package="com.example.android.app" ...>
<application android:debuggable="false"/>
</manifest>
Android Studio 和 Gradle 均不支持对应用模块 APK 进行微基准测试。这是因为,如果支持依赖于不可调试、经过优化或经过缩减的 APK 变体的额外测试目录,而不从基准测试向应用代码发出缩减中断调用,这会增加测试的复杂性。
编译
我们建议您在运行测试之前使用
以下命令:
adb shell cmd package compile -f -m speed com.example.benchmark
缩减和优化
建议对基准测试进行缩减和优化,以获得更接近发布版的性能。如需查看代码示例,请参阅基准测试示例项目。
代码覆盖率
我们建议基准测试应在停用代码覆盖率选项的情况下运行,而不应使用 JaCoCo 之类的工具修改任何库或 DEX。
因此,我们建议将基准测试作为与其他插桩测试隔离的源代码集,并使用发布版依赖项单独构建。在启用和不启用代码覆盖率选项的情况下,这样都可以避免多次构建测试。
基准测试所依赖的库的调试变体,尤其是在本地构建的调试变体,可能在构建时启用了代码覆盖率选项。
运行测试
您可以从命令行运行测试,并指定要搭配使用的类,如下例所示:
adb shell am instrument -w com.example.benchmark/androidx.benchmark.junit4.AndroidBenchmarkRunner
如需在不使用 Gradle 的情况下在运行时配置 Microbenchmark 库,请参阅 Microbenchmark 插桩参数。
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Build Microbenchmarks without Gradle\n\nThis page describes configuring a non-Gradle build system when using the\nMicrobenchmark library.\n\nAlthough the Microbenchmark library ships a Gradle plugin to integrate directly\nwith the Android Gradle plugin, you can also use it in other build systems, such\nas [Bazel](https://bazel.build) or\n[Buck](https://buck.build).\n\nInstrumentation\n---------------\n\nUse [`AndroidBenchmarkRunner`](/reference/kotlin/androidx/benchmark/junit4/AndroidBenchmarkRunner) or a subclass as your instrumentation runner\nby specifying it in the instrumentation block of the test manifest: \n\n```xml\n\u003cmanifest\n package=\"com.example.library.test\" ...\u003e\n\n \u003cinstrumentation android:name=\"androidx.benchmark.junit4.AndroidBenchmarkRunner\" /\u003e\n ...\n\u003c/manifest\u003e\n```\n\nTo get accurate measurements, benchmarks must not be [debuggable](/guide/topics/manifest/application-element#debug). If you\ndon't set the debuggable flag correctly, the library throws an error, rather\nthan reporting invalid results. You might need to toggle this setting during\nlocal runs for use with Android Studio profilers, which require\n`debuggable=true`.\n\nYou can build Microbenchmarks to run in two ways: within a self-instrumenting\nAPK, or with one test APK instrumenting another APK.\n\n### Self-instrumenting APKs\n\nWith a self-instrumenting APK---as output by Gradle for an [`androidTest`](/reference/tools/gradle-api/8.3/null/com/android/build/api/variant/AndroidTest)\ndirectory from `com.android.library`---you must disable debuggable in the single\nAPK's Android manifest: \n\n```xml\n\u003cmanifest\n package=\"com.example.library.test\" ...\u003e\n\n \u003cinstrumentation\n android:name=\"androidx.benchmark.junit4.AndroidBenchmarkRunner\"\n android:targetPackage=\"com.example.library.test\"/\u003e\n\n \u003capplication android:debuggable=\"false\"/\u003e\n\u003c/manifest\u003e\n```\n\n### App APK instrumented by test APK\n\nIf your build outputs two APKs---an app APK and test APK, as output by Gradle for\nthe `androidTest` directory from the `com.android.app` package---you must set the app APK to\n`debuggable=false`. The test APK's debuggable flag is ignored by the Android OS. \n\n```xml\n\u003c!-- Test manifest. --\u003e\n\u003cmanifest\n package=\"com.example.android.app.test\" ...\u003e\n\n \u003cinstrumentation\n android:name=\"androidx.benchmark.junit4.AndroidBenchmarkRunner\"\n android:targetPackage=\"com.example.android.app\"/\u003e\n \u003c!-- This debuggable is ignored by the OS. --\u003e\n\u003c/manifest\u003e\n\n\u003c!-- App being tested. --\u003e\n\u003cmanifest\n package=\"com.example.android.app\" ...\u003e\n\n \u003capplication android:debuggable=\"false\"/\u003e\n\u003c/manifest\u003e\n```\n\nAndroid Studio and Gradle don't support microbenchmarking an app module APK.\nThis is due to the complexity of supporting an additional testing directory that\ndepends on a non-debuggable, optimized, or minified variant of the APK, but\nwithout minification breaking calls from benchmarks into app code.\n\nCompilation\n-----------\n\nWe recommend compiling your microbenchmark APK before running tests, using the\nfollowing command: \n\n adb shell cmd package compile -f -m speed com.example.benchmark\n\nMinification and optimization\n-----------------------------\n\nWe recommend using minification and optimization for your benchmarks to get\nperformance that is close to release. For example code, see the [Benchmark\nsample project](https://github.com/android/performance-samples/blob/main/MicrobenchmarkSample/microbenchmark/build.gradle.kts).\n\nCode coverage\n-------------\n\nWe recommend running benchmarks with coverage disabled and without any library\nor DEX mangling by tools such as JaCoCo.\n\nFor this reason, we recommend you isolate benchmarks as a source set from other\ninstrumentation tests and build them separately with release dependencies. This\navoids having to build tests more than once, both with and without coverage.\n\nDebug variants of libraries that your benchmark depends on, especially those\nbuilt locally, might be built with coverage enabled.\n\nRun your tests\n--------------\n\nYou can run your tests from the command line and specify the classes to run\nwith, as shown in the following example: \n\n adb shell am instrument -w com.example.benchmark/androidx.benchmark.junit4.AndroidBenchmarkRunner\n\nTo configure the Microbenchmark library at runtime without Gradle, see\n[Microbenchmark instrumentation arguments](/studio/profile/microbenchmark-instrumentation-args).\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Write a Microbenchmark](/topic/performance/benchmarking/microbenchmark-write)\n- [Create Baseline Profiles {:#creating-profile-rules}](/topic/performance/baselineprofiles/create-baselineprofile)"]]