Wear OS 上的 Compose 性能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Wear OS 上的性能是应用需要注意的重要事项,因为与较大的移动设备相比,许多 Wear OS 设备的 CPU 和 GPU 资源有限。您可以运用自己掌握的 Jetpack Compose 知识来配置和开发应用,借助 Compose for Wear OS 提升应用性能,因为许多技术是相似的。不过,了解如何在 Wear OS 上测试应用性能很重要。
如需更全面透彻地了解性能概念,请观看 YouTube 上的性能提升最佳实践和打造精美省电的 Wear OS 应用。
提升性能的方法
如需准确了解应用的性能,请尝试在发布模式下运行应用。调试模式有助于发现大量问题,但是会产生严重的性能开销,而且可能会难以发现其他可能影响性能的代码问题。此外,调试模式不使用基准配置文件,这可能会进一步降低性能。您还应使用 R8 编译器来缩减和优化应用。如需详细了解如何移除未使用的资源,请参阅缩减资源。
请熟悉 Android Studio 工具,包括实时编辑、可组合项预览和 Wear OS 模拟器。这样就可减少在手表上调试应用所需的时间,从而提高工作效率。Android Studio 随附手表 AVD。请使用 Compose 预览和实时编辑功能进行测试,直到应用基本上能够按预期工作为止,然后在设备上进行测试,以准确反映应用的性能。
请使用 JankStats 库跟踪和分析应用的性能问题。如需查看示例,请参阅 GitHub 上的 JankStats 示例。
使用基准配置文件
请使用基准配置文件定义关键用户历程所需的类和方法,从而提升应用性能。如需关于此主题的深度指南,请参阅基准配置文件。每个 Compose for Wear OS 库都随附自己的配置文件规则。如果应用依赖于某个库,该库的配置文件规则会与应用的 APK 自动合并,并与其一起分发,而且会在设备上提前进行编译。这样可以缩短启动时间,减少卡顿帧,并在其他方面提高性能。
如果您选择为应用自行定义配置文件,请编写 Macrobenchmark 测试来验证应用的性能是否确实得到了提升。如需查看示例,请参阅 GitHub 上的性能示例。
基准配置文件命令
您可以借助一些实用的命令来使用基准配置文件。首先,运行以下命令来确定配置文件的状态:
adb shell dumpsys package dexopt | grep -A 1 $PACKAGE_NAME
如果状态不是 status=speed-profile
,就表示尚未应用规则来优化应用。
Compose for Wear OS 配置文件规则需要使用后台作业来应用,该作业会在设备充电和空闲时运行。当应用启动并经过足够的时间后,您可以运行以下命令让配置文件安装程序在后台引导配置文件,从而手动触发此作业。此过程通常需要大约 40 秒。
adb shell cmd package bg-dexopt-job
然后,您可以重新运行上一个命令来检查状态现在是否为 speed-profile
。
如需了解在安装时执行优化的情况,请参阅旁加载基准配置文件。
为您推荐
- 注意:当 JavaScript 处于关闭状态时,系统会显示链接文字
- 应用性能测量概览
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Compose performance on Wear OS\n\nPerformance on Wear OS is an essential app consideration, as many Wear OS\ndevices have limited CPU and GPU resources compared to larger mobile devices.\nYou can use your knowledge from [Jetpack Compose](/jetpack/compose/performance) to configure and develop\nyour app to improve performance with Compose for Wear OS, as many of the\ntechniques are similar. However, it's important to understand how to test your\napp's performance on Wear OS.\n\nTo better understand performance concepts, watch\n[Performance best practices](https://www.youtube.com/watch?v=EOQB8PTLkpY) and\n[Create beautiful, power efficient apps for Wear OS](https://www.youtube.com/watch?v=jpUVamtoKOs&t=1052s) on YouTube.\n\nTechniques to improve performance\n---------------------------------\n\nTo accurately understand your app's performance, try running your app in\n[release mode](/studio/run#changing-variant). Debug mode is useful for spotting lots of problems, but it\nimposes a significant performance cost, and can make it hard to spot other code\nissues that might be hurting performance. In addition, debug mode does not use\nbaseline profiles, which can further deteriorate performance. You should also\nuse the [R8 compiler](/studio/build/shrink-code) to shrink and optimize your app. For more information\non removing unused resources, see [Shrink your resources](/studio/build/shrink-code#shrink-resources).\n\nFamiliarize yourself with [Android Studio tools](/jetpack/compose/tooling)\nincluding Live Edit, Composable Preview, and Wear OS emulator. This can reduce\nthe amount of time spent debugging your app on a watch, which can improve your\nproductivity. Android Studio ships with a watch AVD. Test with Compose Previews\nand Live Edit until your app is largely working as expected, then test on a\ndevice for an accurate reflection of your app's performance.\n\nUse the [JankStats library](/topic/performance/jankstats) to track and analyze\nperformance problems in your applications. For an example, see the\n[JankStats sample](https://github.com/android/performance-samples/tree/main/JankStatsSample) on GitHub.\n\nUse a baseline profile\n----------------------\n\n[Use a baseline profile](/jetpack/compose/performance#use-baseline) to improve your app's performance by defining\nclasses and methods needed on critical user journeys. For an in depth guide on\nthis topic, see [Baseline profiles](/topic/performance/baselineprofiles). Each\nCompose for Wear OS library ships with its own profile rules. When your app\ndepends on a library, the library profile rules are automatically merged and\ndistributed with your app's APK and are compiled ahead of time on device. This\ncan reduce start-up times, cut down janky frames, and otherwise improve\nperformance.\n\nIf you choose to define your own profile for an app, verify that it does\nimprove performance by writing Macrobenchmark tests.\nFor an example, see\n[Performance samples](https://github.com/android/performance-samples/blob/main/MacrobenchmarkSample/baseBenchmarks/src/main/java/com/example/benchmark/macro/base/startup/StartupBenchmark.kt) on GitHub.\n\n### Baseline profile commands\n\nThere are some useful commands you can use to help working with baseline\nprofiles. First, determine the status of your profile by running the\nfollowing: \n\n adb shell dumpsys package dexopt | grep -A 1 $PACKAGE_NAME\n\nIf the status is not `status=speed-profile`, the rules have not yet been applied\nto optimize the app.\n\nCompose for Wear OS profile rules are applied using a background job which runs\nwhen the device is charged and idle. You can manually trigger this by running\nthe following command after the app has been launched and enough time has passed\nto allow the profile-installer to bootstrap the profile in the background. This\ntypically takes around 40 seconds. \n\n adb shell cmd package bg-dexopt-job\n\nYou can then re-run the previous command to check that the status is now\n`speed-profile`.\n\nFor situations when the optimisation is performed at install, see\n[Sideload the baseline profile](/topic/performance/baselineprofiles#measuring-baseline).\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Overview of measuring app performance](/topic/performance/measuring-performance)"]]