Wear OS 的 Compose 效能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Wear OS 的效能是應用程式首要考慮的因素,畢竟和更大型的行動裝置相比,許多 Wear OS 裝置的 CPU 和 GPU 資源都有限。由於許多技術都十分類似,您可以運用從 Jetpack Compose 學到的知識來設定並開發應用程式,改善 Wear OS 的 Compose 效能。不過,請務必瞭解如何在 Wear OS 測試應用程式效能。
如果想深入瞭解效能概念,請至 YouTube 觀看「效能最佳做法」和「建立美觀而省電的 Wear OS 應用程式」。
改善效能的技巧
如果想正確瞭解應用程式的效能,請嘗試在版本模式中執行應用程式。偵錯模式有助於找出許多問題,但需要投入大量效能,而且會難以找出其他可能影響效能的程式碼問題。另外,偵錯模式不會使用基準設定檔,可能會進一步拖慢效能。您也應該使用 R8 編譯器,來最小化並最佳化應用程式。若想深入瞭解如何移除未使用的資源,請參閱「縮減資源」。
熟悉 Android Studio 工具操作方式,包括即時編輯、可組合項預覽、Wear OS 模擬器,可以減少在手錶上偵錯應用程式所花的時間,提升工作效率。Android Studio 內附手錶 AVD。請用 Compose 預覽和即時編輯測試應用程式,大致上運作正常後,再用裝置測試實際的應用程式效能。
使用 JankStats 程式庫追蹤並分析應用程式的效能問題。請參閱 GitHub 的 JankStats 範例。
使用基準設定檔
使用基準設定檔,定義關鍵使用者旅程需要使用的類別和方法,藉此改善應用程式效能。如果想深入瞭解本主題,請參閱「基準設定檔」。每個 Wear OS 程式庫的 Compose 都有內建專用的設定檔規則。如果應用程式依附程式庫,系統便會自動合併程式庫的設定檔規則,並和應用程式 APK 一起發布,且會在裝置端提前編譯。這樣可以減少啟動時間和造成資源浪費的影格,同時改善效能。
如果您自行為應用程式定義設定檔,請編寫 Macrobenchmark 測試,確定設定檔確實可以改善效能。請見 GitHub 的 Performance 範例。
基準設定檔指令
有些指令非常實用,能協助您更有效使用基準設定檔。首先,請執行下列指令判斷設定檔的狀態:
adb shell dumpsys package dexopt | grep -A 1 $PACKAGE_NAME
如果狀態並非 status=speed-profile
,表示系統尚未套用規則最佳化應用程式。
系統會利用背景工作套用 Wear OS 的 Compose 設定檔規則,因此只會在裝置充電和待機時執行。您可以在應用程式啟動足夠時間後執行以下指令,藉此手動觸發套用規則,讓設定檔安裝程式得以在背景成功套用設定檔。這項程序通常需要約 40 秒。
adb shell cmd package bg-dexopt-job
然後,您便可以再度執行上述指令,檢查狀態是否已為 speed-profile
。
如果應用程式在安裝時執行最佳化,請參閱「側載基準設定檔」。
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["Performance 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\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[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\nBaseline 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- Note: link text is displayed when JavaScript is off\n- [Overview of measuring app performance](/topic/performance/measuring-performance)"]]