縮減應用程式大小
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
應用程式大小太小,就和下載成功直接相關
網路連線品質不佳或網路連線品質不佳的新興市場
速度。這可能會導致應用程式使用率降低,進而縮小目標對象的範圍並降低觸及率。不過,您可以透過多種方式縮減應用程式大小。
最佳做法
以 Android App Bundle 格式上傳應用程式
如要將應用程式發布至 Google Play,同時縮減其大小,最簡便的方式是以 Android App Bundle 格式進行上傳。這種新的發布格式包含應用程式所有已編譯的程式碼和資源,且需要較長的處理時間,才能產生及簽署 APK,發布到 Google Play 上。
縮減執行階段程式碼大小
檢查應用程式在執行階段未使用的程式碼,例如任何大型類別或自動產生的程式碼。R8 等程式碼最佳化工具可協助最佳化及縮減程式碼大小,但無法處理受到執行階段常數保護的程式碼。更換支票
含有編譯時間常數的標記,以便充分運用各種最佳化功能
工具。
您可以在 Gradle 設定檔中啟用程式碼和資源縮減:
android {
buildTypes {
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
}
}
}
移除不必要的版面配置
合併含有小幅 UI 變動且未使用的版面配置,並移除不必要的版面配置,藉此縮減整體應用程式程式碼大小。此外,建議您盡可能動態轉譯版面配置和檢視區塊。這樣一來,就能避免繪製靜態範本,可直接套用替代版面配置,無須浪費額外的技術資源。
重新評估不常用的功能
停用每日活躍使用者 (DAU) 指標偏低的功能,特別針對 Android (Go 版本) 進行最佳化調整。例如移除複雜的動畫、大型 GIF 檔案,或是對應用程式成效而言非必要的任何其他美感附加項目。
使用 Dynamic Delivery
Play Feature Delivery 使用應用程式套件的進階功能,以便視情況提供或隨選下載應用程式的特定功能。您可以透過功能模組按照使用者的需求提供功能。功能模組有一個獨特的優點,就是讓您自行選擇在什麼時間、透過何種方式將應用程式的不同功能下載到裝置上。凡是搭載 Android 5.0 (API 級別 21) 以上版本的裝置,都支援這項功能。
縮減可翻譯字串大小
您可以利用 Android Gradle resConfigs
屬性移除應用程式不需要的額外資源檔案。如果您使用的程式庫包含語言資源 (例如 AppCompat 或 Google Play 服務),則應用程式會納入程式庫訊息的所有已翻譯語言字串 (無論應用程式是否需要翻譯)。如果只想保留應用程式官方支援的語言,可以使用 resConfig
屬性指定這些語言。系統會移除任何未指定語言的資源。
如要將語言資源限制為英文和法文,可按照以下方式編輯 defaultConfig
:
android {
defaultConfig {
...
resConfigs "en", "fr"
}
}
使用選擇性翻譯
如果特定字串不會顯示在應用程式的 UI 中,就無需翻譯。用於偵錯、例外狀況訊息或網址的字串應為程式碼中的字串常值,而非資源。
舉例來說,您可以不必費心翻譯網址這類字串。
<string name="car_frx_device_incompatible_sol_message">
This device doesn\'t support Android Auto.\n
<a href="https://support.google.com/androidauto/answer/6395843">Learn more</a>
</string>
您可能已經知道 <
和 >
的作用,因為這些是 <
和 >
的逸出字元。如果您在 <string>
標記內置入 <a>
標記,Android 資源編譯器就會因無法識別這些標記而不予處理,因此需要加入這些標記。不過,這意味著您要將 HTML 標記和網址翻譯成 78 種語言。您可改為移除 HTML:
<string name="car_frx_device_incompatible_sol_message">
This device doesn\'t support Android Auto.
</string>
結合原生二進位檔和常見依附元件
如果應用程式採用多項不同的 Java Native Interface (JNI) 實作項目,但使用共通的基礎依附元件,則會導致多個二進位檔因為包含多餘的元件而增加 APK 大小。您可以將多個 JNI 二進位檔合併為單一 JNI 二進位檔,同時保持 Java 和 JNI 檔案各自獨立。這樣做可大幅縮減 APK 大小。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Reduce app size\n\nSmall app size is directly related to download success, particularly in\nemerging markets with poor network device connections or low network\nspeeds. This can result in lower app usage rates, which in turn lowers the\nscope and reach of your audience. However, there are multiple ways to help\nreduce the size of your app.\n\nBest practices\n--------------\n\n### Upload app as Android App Bundle\n\nThe easiest way to gain immediate app size savings when publishing to Google\nPlay is by uploading your app as an [Android App Bundle](/guide/app-bundle),\nwhich is a new publishing format that includes all your app's compiled code and\nresources, and defers APK generation and signing to Google Play.\n\n### Reduce runtime code size\n\nCheck for code that your app doesn't use at runtime, for example any large\nclasses or auto-generated code. Code optimizers like\n[R8](/studio/build/shrink-code) can help optimize and shrink code size, but\nthey can't deal with code guarded by runtime-constants. Replace the check\nflags with compile-time constants to make the best use of various optimization\ntools.\nYou can enable code and resource shrinking in your gradle configuration file: \n\n android {\n buildTypes {\n getByName(\"release\") {\n isMinifyEnabled = true\n isShrinkResources = true\n }\n }\n }\n\n### Remove unnecessary layouts\n\nMerge unused layouts with small UI changes and remove any unnecessary\nlayouts to reduce overall app code size. Additionally, you can dynamically\nrender layouts and views wherever possible. This lets you avoid drawing static\ntemplates and apply alternate layouts without the technical overhead.\n\n### Re-evaluate infrequently used features\n\nSpecifically optimize for Android (Go edition) by disabling features that\nhave low daily active user (DAU) metrics. Examples of this include removing\ncomplex animations, large GIF files, or any other aesthetic additions not\nnecessary for app success.\n\n### Utilize dynamic delivery\n\n[Play Feature Delivery](/guide/playcore/feature-delivery) uses advanced\ncapabilities of app bundles, allowing certain features of your app to be\ndelivered conditionally or downloaded on demand. You can use feature modules\nfor custom delivery. A unique benefit of feature modules is the ability to\ncustomize how and when different features of your app are downloaded onto\ndevices running Android 5.0 (API level 21) or higher.\n\n### Reduce translatable string size\n\nYou can use the Android Gradle `resConfigs` property to remove\nalternative resource files that your app doesn't need. If you're using a\nlibrary that includes language resources (such as AppCompat or Google Play\nServices), then your app includes all translated language strings for library\nmessages, regardless of app translation. If you'd like to keep only the\nlanguages that your app officially supports, you can specify those languages\nusing the `resConfig` property. Any resources for languages not specified are\nremoved.\n\nTo limit your language resources to just English and French, you can edit\n`defaultConfig` as shown below: \n\n\n android {\n defaultConfig {\n ...\n resConfigs \"en\", \"fr\"\n }\n }\n\n### Use selective translation\n\nIf a given string isn't visible in the app's UI, then you don't have to\ntranslate it. Strings for the purpose of debugging, exception messages,\nor URLs should be string literals in code, not resources.\n\nFor example, don't bother translating URLs. \n\n \u003cstring name=\"car_frx_device_incompatible_sol_message\"\u003e\n This device doesn\\'t support Android Auto.\\n\n <a href=\"https://support.google.com/androidauto/answer/6395843\">Learn more</a>\n \u003c/string\u003e\n\nYou may recognize `<` and `>`, as these are escape characters for\n`\u003c` and `\u003e`. They're needed here because if you were to put an `\u003ca\u003e` tag inside\nof a `\u003cstring\u003e` tag, then the Android resource compiler drops them since it\ndoesn't recognize the tag. However, this means that you're translating the HTML\ntags and the URL to 78 languages. Instead, you can remove the HTML: \n\n \u003cstring name=\"car_frx_device_incompatible_sol_message\"\u003e\n This device doesn\\'t support Android Auto.\n \u003c/string\u003e\n\n### Combine native binaries with common dependencies\n\nIf your app has different Java Native Interface (JNI) implementations\nwith common underlying dependencies, then the various binaries are increasing\nthe APK size with redundant components. You can combine several JNI binaries\ninto a single JNI binary file while keeping the Java and JNI files separate.\nThis can reduce your APK size quite dramatically."]]