为数十亿用户降低数据成本
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
某些国家/地区的数据流量套餐支出占普通用户月收入的 10% 以上。这意味着,最大限度缩减应用的下载大小并让用户控制应用使用数据的方式,可为许多用户带来极大的实惠。最大限度缩减下载大小还有助于节省内部存储空间,而这对某些设备而言是稀缺资源。
下文介绍的一些策略有助于优化应用通过网络以及在内部存储空间中使用的数据量。
缩减应用大小
就网络数据和内部存储空间而言,缩减应用大小是帮助用户减少流量消耗的一种基本方式。本部分将介绍以下几种缩减应用大小的方法。
缩减 APK 图形化资源大小
- 图形化资源往往是影响 APK 大小的最大因素。对其进行优化可以减少下载内容大小,从而缩短用户的安装时间。
- 对于图标等图形化资源,请使用可缩放矢量图形 (SVG) 格式。SVG 图像的尺寸比位图图形小,可在运行时渲染至任何分辨率。Android 支持库可向后兼容至 Android 2.1(API 级别 7)的矢量资源。您可通过这篇 Medium 博文了解矢量使用入门。
- 对于照片等非矢量图像,请使用 WebP 缩短图像加载时间,并节省网络带宽。事实证明,同样的图像文件,采用 WebP 格式通常比采用 PNG 和 JPG 格式小,而图像质量却毫不逊色。即便是采用有损设置,WebP 也能生成与原始图像近乎完全相同的图像。Android 自 Android 4.0(API 级别 14:Ice Cream Sandwich)起提供对有损 WebP 的支持,自 Android 4.2(API 级别 17:Jelly Bean)起提供对无损、透明 WebP 的支持。
- 如果您有许多大型图像采用多种密度,不妨考虑使用多 APK 支持按密度拆分您的 APK。由此可生成多个针对特定密度的 build,这意味着使用低密度设备的用户无需因下载未使用的高密度资源而招致惩罚。
- 有关缩减 APK 大小的详细信息,请参阅缩减 APK 大小和缩减代码与资源。此外,您可以在这篇 Medium 系列博文中找到有关缩减 APK 大小的详细指南。
缩减代码大小
- Android 项目中的每个库都在向 APK 添加可能不会用到的代码。请谨慎使用外部库,因为并非所有库都可在移动应用中使用。确保应用使用的库已针对移动使用进行了优化。
- 考虑利用 ProGuard 等工具来优化已编译代码。ProGuard 可以找出并移除 APK 中未使用的代码。此外,您还可以通过在
build.gradle
中设置 minifyEnabled=true
、shrinkResources=true
,在构建时实现资源缩减,这样做可以自动从 APK 中移除未使用的资源。
- 使用 Google Play 服务时,您应该有选择地只在 APK 中加入必要的 API。
- 如需了解有关缩减 APK 代码大小的详细信息,请参阅有关如何避免依赖注入框架的 Android 培训内容。
允许将应用移至外部 (SD) 存储空间
- 低成本设备自带的内置存储空间通常很小。用户可通过 SD 卡扩展存储空间;不过,应用需要显式声明其支持安装至外部存储空间,用户方可移动数据。
- 您可利用 AndroidManifest.xml 中的
android:installLocation
标记,允许将应用安装至外部存储空间。如需了解有关允许将应用移至外部存储空间的详细信息,请参阅有关应用安装位置的 Android 指南。
降低应用安装后的磁盘占用率
- 让应用的磁盘占用率保持较低水平,意味着在设备的可用空间不足时,用户卸载应用的可能性较小。务必规定缓存的界限,如此可防止应用的磁盘占用率无限增长下去。请务必将缓存数据存放在
getCacheDir()
,系统可以根据需要删除存放在此处的文件,这样它们就不会显示为应用专用存储空间。
提供可配置的网络使用
Android 平台提供多种方式,使用户能够控制应用对网络的使用,从而根据自己的需求优化此配置。例如,当用户首次使用应用时,应用可以引导用户完成各种与网络相关的设置。您还可以在应用外部提供网络偏好设置屏幕。
针对用户的网络选择提供新手入门体验
- 即便应用需要很高的数据流量,允许用户降低流量消耗的应用仍大受欢迎。如果应用的带宽占用量相当大(例如,视频在线播放应用),可以为用户提供新手入门体验来配置网络使用。例如,您可以允许用户在使用移动网络时强制播放比特率较低的视频串流。
- 其他供用户控制数据同步、预提取和网络使用行为(例如,只在使用 Wi-Fi 网络时预提取所有已加星标的新闻类别)的设置也有助于用户根据自身需求来定制应用的行为。
- 如需了解有关管理网络使用的详细信息,请参阅有关管理网络使用情况的 Android 培训内容。
提供网络偏好设置屏幕
其他资源
如需详细了解本主题,请参阅下面列出的其他资源:
博文
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Reduced data cost for billions\n\n\u003cbr /\u003e\n\n\nData plans in some countries can cost upwards of 10% of a typical user's monthly income. This\nmeans that minimizing your app's download size and letting the user control how your app uses\ndata can have a large, tangible benefit to many users. Minimizing download size also helps\nconserve space in internal storage, which is a scarce resource in some devices.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\nHere you can find some strategies to help optimize the amount of data your app uses, both over\nthe network and in internal storage.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nReduce app size\n---------------\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\nReducing app size is one of the fundamental ways you can help your user consume less data, in\nterms of both network data and internal storage. This section describes several approaches to\nreducing app size.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Reduce APK graphical asset size\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n- Graphical assets are often the largest contributor to the size of the APK. Optimizing these can result in smaller downloads and thus faster installation times for users.\n- For graphical assets such as icons, use the Scalable Vector Graphics (SVG) format. SVG images are tiny in size compared to bitmap graphics and can be rendered at runtime to any resolution. The [Android Support Library](/tools/support-library) provides a backward-compatible implementation for vector resources to Android 2.1 (API level 7). Get started with vectors with [this Medium post](https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88).\n- For non-vector images, such as photos, use [WebP](https://developers.google.com/speed/webp/) to reduce image load times and save network bandwidth. WebP is proven to result in smaller file sizes than its PNG and JPG counterparts, with at least the same image quality. Even at lossy settings, WebP can produce a nearly identical image to the original. Android has included lossy WebP support since Android 4.0 (API level 14: Ice Cream Sandwich) and support for lossless, transparent WebP since Android 4.2 (API level 17: Jelly Bean).\n- If you have many large images across multiple densities, consider using [Multiple\n APK support](/google/play/publishing/multiple-apks) to split your APK by density. This results in builds targeted for specific densities, meaning users with low-density devices won't have to incur the penalty of downloading unused high-density assets.\n- For more information about reducing APK size, see [Reduce APK Size](/topic/performance/reduce-apk-size) and [Shrink Your Code and Resources](/studio/build/shrink-code). In addition, you can find a detailed guide on reducing APK size in this [series of Medium posts](https://medium.com/@wkalicinski/smallerapk-part-4-multi-apk-through-abi-and-density-splits-477083989006).\n\n### Reduce code size\n\n- Every library in your Android project is adding potentially unused code to the APK. Be particularly careful about using external libraries because not all libraries are designed for use in mobile apps. Ensure that the libraries your app is using are optimized for mobile use.\n- Consider optimizing your compiled code using a tool such as [ProGuard](/tools/help/proguard). ProGuard identifies code that isn't being used and removes it from your APK. Also [enable resource shrinking](http://tools.android.com/tech-docs/new-build-system/resource-shrinking) at build time by setting `minifyEnabled=true`, `shrinkResources=true` in `build.gradle`---this automatically removes unused resources from your APK.\n- When using Google Play services, you should [selectively include](/google/play-services/setup#add_google_play_services_to_your_project) only the necessary APIs into your APK.\n- For more information on reducing code size in your APK, see the Android training on how to [Avoid\n dependency injection frameworks](/training/articles/memory#DependencyInjection).\n\n### Allow app to be moved to external (SD) storage\n\n- Low-cost devices often come with little on-device storage. Users can extend this with SD cards; however, apps need to explicitly declare that they support being installed to external storage before users can move them.\n- Allow your app to be installed to external storage using the [`\n android:installLocation`](/guide/topics/manifest/manifest-element#install) flag in your AndroidManifest.xml. For more information on enabling your app to be moved to external storage, see the Android guide on [App Install\n Location](/guide/topics/data/install-location).\n\n\u003cbr /\u003e\n\n### Reduce post-install app disk use\n\n\u003cbr /\u003e\n\n- Keeping your app's disk use low means that users are less likely to uninstall your app when the device is low on free space. It's important to apply bounds around your caches---this prevents your app's disk use from growing indefinitely. Be sure you put your cached data in [getCacheDir()](/reference/android/content/Context#getCacheDir())---the system can delete files placed here as needed, so they won't show up as storage committed to the app.\n\n\u003cbr /\u003e\n\nOffer configurable network use\n------------------------------\n\n\nThe Android platform includes a number of ways you can give the user control\nover your app's network use, optimizing it for their own needs. For example,\non first use, your app can walk the user through a variety of network-related settings.\nYou can also provide a network preferences screen from outside the app.\n\n### Provide onboarding experiences for users' network\nchoices\n\n\u003cbr /\u003e\n\n- Apps that allow users to reduce data use are well received, even if they have heavy data requirements. If your app uses a considerable amount of bandwidth (for example, video streaming apps), you can provide an onboarding experience for users to configure network use. For example, you could allow the user to force lower-bitrate video streams on cellular networks.\n- Additional settings for users to control data syncing, prefetching, and network use behavior (for example, prefetch all starred news categories on Wi-Fi only), also help users tailor your app's behavior to their needs.\n- For more information on managing network use, see the Android training on [Managing\n Network Usage](/training/basics/network-ops/managing).\n\n### Provide a network preferences\nscreen\n\n- You can navigate to the app's network settings from outside the app by means of a network preferences screen. You can invoke this screen from either the system settings screen or the system data usage screen.\n- To provide a network preferences screen that users can access from within your app as well as from the system settings, in your app include an activity that supports the [ACTION_MANAGE_NETWORK_USAGE](/reference/android/content/Intent#ACTION_MANAGE_NETWORK_USAGE) action.\n- For further information on adding a network preferences screen, see the Android training on [Implementing a Preferences Activity](/training/basics/network-ops/managing#prefs).\n\n\u003cbr /\u003e\n\nAdditional resources\n--------------------\n\nTo learn more about this topic, view the following additional resources:\n\n### Blog posts\n\n- [Nurture trust through cost transparency](https://medium.com/google-design/nurture-trust-through-cost-transparency-b61a5947d2fc)"]]