缩减应用大小
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
应用大小直接关系到下载成功与否,尤其是在网络连接质量不佳或网速慢的新兴市场。这可能会导致应用的使用率降低,进而缩小受众群体的范围和覆盖面。不过,您可以通过多种方式来缩减应用的大小。
最佳实践
以 Android App Bundle 格式上传应用
如需在发布到 Google Play 时立即缩减应用大小,最简单的方法就是将应用上传为 Android APP Bundle,这是一种全新的发布格式,其中包含应用的所有编译好的代码和资源,但 APK 生成及签名工作则交给 Google Play 来处理。
缩减运行时代码大小
检查应用在运行时不使用的代码,例如任何大型类或自动生成的代码。R8 等代码优化器有助于优化和缩减代码大小,但它们无法处理由运行时常量保护的代码。将检查标志替换为编译时常量,以充分利用各种优化工具。
您可以在 Gradle 配置文件中启用代码和资源缩减流程:
android {
buildTypes {
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
}
}
}
移除不必要的布局
将未使用的布局与细微的界面更改合并,并移除所有不必要的布局,以缩减应用代码的整体大小。此外,您还可以尽可能地动态渲染布局和视图。这让您可以避免绘制静态模板,而且可以应用备用布局而不产生技术开销。
重新评估不常用的功能
通过停用日活跃用户数 (DAU) 指标较低的功能,针对 Android(Go 版本)进行优化。例如,移除复杂的动画、大型 GIF 文件或其他任何对于应用的成功所不必需的艺术添加功能。
使用 Dynamic Delivery
Play Feature Delivery 使用了 app bundle 的多种高级功能,让您可按条件分发或按需下载应用的某些功能。您可以使用功能模块实现自定义分发。功能模块的独特优势在于,能够自定义如何以及何时将应用的不同功能下载到搭载 Android 5.0(API 级别 21)或更高版本的设备上。
缩减可翻译字符串的大小
您可以使用 Android Gradle resConfigs
属性来移除应用不需要的备用资源文件。如果您使用的库包含语言资源(例如 AppCompat 或 Google Play 服务),则您的应用包含库消息的所有已翻译的语言字符串,无论应用的翻译语言是什么。如果您只想保留应用正式支持的语言,可以使用 resConfig
属性指定这些语言。系统会移除未指定语言的所有资源。
如需将语言资源设置为仅限英语和法语,您可以按照以下方式修改 defaultConfig
:
android {
defaultConfig {
...
resConfigs "en", "fr"
}
}
有选择地翻译
如果某个字符串在应用界面中不可见,则无需翻译该字符串。用于调试、异常消息或网址的字符串在代码中应该是字符串字面量,而不是资源。
例如,无需翻译网址。
<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>
您可能会认得 <
和 >
,它们是 <
和 >
的转义字符。此处之所以需要这些转义字符,是因为如果您将 <a>
标记放在 <string>
标记内,则 Android 资源编译器会因为无法识别该标记而将其丢弃。不过,这意味着您需要将 HTML 标记和网址翻译成 78 种语言。您可以改为移除 HTML:
<string name="car_frx_device_incompatible_sol_message">
This device doesn\'t support Android Auto.
</string>
将原生二进制文件与通用依赖项结合
如果您的应用使用通用底层依赖项实现不同的 Java 原生接口 (JNI),那么多个二进制文件就会因为包含多余的组件而增加了 APK 大小。您可以将多个 JNI 二进制文件组合成单个 JNI 二进制文件,同时将 Java 文件和 JNI 文件分开。这会大幅缩减 APK 的大小。
本页面上的内容和代码示例受内容许可部分所述许可的限制。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,["# 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."]]