2K 透過 Android Game Development Kit 將 ANR 發生率降低 35%
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
背景

Cat Daddy Games 是一間位於華盛頓州柯克蘭的 2K 工作室,也是 NBA 2K Mobile 的開發者。該公司團隊想要減少「應用程式無回應」(ANR) 錯誤,藉此改善遊戲的整體品質和穩定性。當 Android 應用程式的 UI 執行緒封鎖時間過長時,就會發生 ANR。在這種情況下,應用程式的主要執行緒 (負責更新 UI) 無法繪製或處理使用者輸入事件,導致使用者感到困擾。如果應用程式在前景執行,系統會顯示對話方塊,讓使用者強制退出應用程式。
具體措施
減少 ANR 一直是《Cat Daddy》的首要任務。品質確保團隊無時無刻努力解決常見的 ANR 模式,他們發現應用程式暫停後,快速恢復運作時,大量觸控輸入可能會導致 ANR。我們進一步調查了 Firebase Crashlytics,發現此 ANR 類型為 android.os.MessageQueue.nativePollOnce,這是 NBA 2K Mobile 最常見的 ANR 類型。
Cat Daddy 也發現,改善 GameActivity 的輸入處理方式可避免這類 ANR,因此他們決定改用 GameActivity。
GameActivity 是 Android Game Development Kit 的元件,旨在協助 Android 遊戲處理應用程式 C/C++ 程式碼中的應用程式週期指令、輸入事件和文字輸入內容。GameActivity 針對 NativeActivity
提供許多以遊戲為主的改善功能,例如 Fragment
、算繪至 SurfaceView
,以及支援熱門遊戲開發相關程式庫的其他支援。
GameActivity 也會在輸入緩衝區中使用雙重緩衝,讓遊戲更妥善地處理高輸入量的案例。
成果
透過實作 GameActivity,Cat Daddy 大幅改善了遊戲的輸入處理方式,進而導致 ANR 錯誤減少了 35%。這樣不僅可以改善使用者體驗,還能打造更穩定的遊戲體驗。
還有一項額外好處,因為 GameActivity 繼承自 FragmentActivity
,所以 CatDaddy 也能整合 EmbeddedWebView 和 EmbeddedVideoView,這是某些熱門社交遊戲整合的必要項目。
開始使用
使用 GameActivity 和 Android Game Development Kit 的其餘部分,改善自家的遊戲體驗。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2022-12-09 (世界標準時間)。
[null,null,["上次更新時間:2022-12-09 (世界標準時間)。"],[],[],null,["# 2K reduces ANR rate by 35% with the Android Game Development Kit\n\nBackground\n----------\n\nCat Daddy Games is a wholly-owned\n[2K](https://play.google.com/store/apps/dev?id=6681606924556273560)\nstudio based in Kirkland, Washington and the developer of NBA 2K Mobile.\nThe team wanted to improve the overall quality and stability of their games,\nspecifically by reducing \"Application Not Responding\" errors (ANRs). ANRs\noccur when the UI thread of an Android app is blocked for too long. When\nthat happens, the app's main thread, which is responsible for updating the\nUI, can't draw or process user input events, causing frustration to the user.\nIf the app is running in the foreground, the system displays a dialog that\nallows the user to force-quit the app.\n\nWhat they did\n-------------\n\nReducing ANRs has been a high priority for Cat Daddy. The QA team worked\nrelentlessly and nailed down a common ANR pattern: they found that when the app\nwas paused and then quickly resumed, a large amount of touch input could cause\nan ANR. Further investigation with\n[Firebase Crashlytics](https://firebase.google.com/docs/crashlytics) showed\nthat this ANR was of type android.os.MessageQueue.nativePollOnce, the most\ncommon type of ANR for NBA 2K Mobile.\n\nCat Daddy also saw that the improved input handling in GameActivity could avoid\nthis type of ANRs, so they decided to move to GameActivity.\n\n[GameActivity](/games/agdk/game-activity) is a component of the\n[Android Game Development Kit](/stories/games/games/agdk), which\nis designed to assist Android games in processing app cycle commands, input\nevents, and text input in the application's C/C++ code. GameActivity offers a\nnumber of game-focused improvements over `NativeActivity`, such as\n[`Fragment`](/reference/androidx/fragment/app/Fragment), rendering to a\n[`SurfaceView`](/reference/android/view/SurfaceView), and other support\nfor popular game development-related libraries.\n\nGameActivity also uses double buffering in its input buffer, allowing the game\nto better handle this case of high input volume.\n\nResults\n-------\n\nBy implementing GameActivity, Cat Daddy was able to significantly improve the\ninput handling of the game, leading to a 35% reduction in ANR errors. This\nimproved the user experience and created a more stable gameplay experience.\n\nAs an added bonus, because GameActivity inherits from\n[`FragmentActivity`](/reference/androidx/fragment/app/FragmentActivity),\nCatDaddy was also able to integrate EmbeddedWebView and EmbeddedVideoView,\nwhich were required for some of the most popular social game integrations.\n\nGet started\n-----------\n\nImprove your own game experience with [GameActivity](/games/agdk/game-activity)\nand the rest of the [Android Game Development Kit](/games/agdk)."]]