降低數十億使用者的資料成本
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
部分國家/地區的行動資費方案費用可能會超過一般使用者的每月收入的 10%。也就是說,盡可能縮減應用程式的下載大小,讓使用者能控制應用程式使用資料的方式,能為許多使用者帶來巨大的實質好處。此外,將下載大小縮減也有助於節省內部儲存空間的空間,在部分裝置上會佔用少量資源。
這裡提供幾項策略,協助您最佳化透過網路和內部儲存空間應用程式使用的資料量。
縮減應用程式大小
針對網路資料和內部儲存空間,縮減應用程式大小是幫助使用者減少數據用量的基本方法之一。本節說明幾種縮減應用程式大小的方法。
縮減 APK 圖形資產大小
- 圖像資源通常是在 APK 大小中影響最大的因素。最佳化這些設定可以減少下載次數,讓使用者安裝速度更快。
- 圖示等圖形資產應使用可擴充向量圖形 (SVG) 格式。與點陣圖圖形相比,SVG 圖片的尺寸較小,而且可在執行階段以任何解析度算繪。Android 支援資料庫提供回溯相容的實作方式,用於 Android 2.1 (API 級別 7) 的向量資源。如要開始使用向量,請參閱
這篇 Medium 文章。
- 如果是相片等非向量圖片,請使用 WebP 縮短圖片載入時間並節省網路頻寬。事實證明,WebP 的檔案大小比 PNG 和 JPG 檔案小,且圖片品質至少要相同。即使設定有損設定,WebP 仍能產生與原始圖片幾乎相同的圖片。自 Android 4.0 (API 級別 14:Ice Cream Sandwich) 以來,Android 已納入有損 WebP 支援,並自 Android 4.2 (API 級別 17:Jelly Bean) 開始支援無損、透明的 WebP。
- 如果您有許多密度的大型圖片,可以考慮支援多個 APK,依密度分割 APK。此舉會導致版本鎖定特定密度的版本,也就是說,低密度裝置的使用者不必因此對未使用的高密度資產下載造成負面影響。
- 如要進一步瞭解如何縮減 APK 大小,請參閱「縮減 APK 大小」和「縮減程式碼和資源」。此外,您可以參閱
這篇 Medium 文章中的詳細指南,瞭解如何縮減 APK 大小。
縮減程式碼大小
- Android 專案中的每個程式庫都會在 APK 中新增可能未使用的程式碼。由於並非所有程式庫都是專為行動應用程式使用而設計,因此請格外謹慎使用外部程式庫。請確保應用程式使用的程式庫已針對行動裝置進行最佳化調整。
- 建議您使用 ProGuard 等工具將編譯過的程式碼最佳化。ProGuard 會找出未使用的程式碼,並從 APK 中移除。此外,您也可以在
build.gradle
中設定 minifyEnabled=true
(shrinkResources=true
) 以在建構時間
啟用資源縮減功能,系統會自動從 APK 中移除未使用的資源。
- 使用 Google Play 服務時,您應
選擇性地僅將必要的 API 加入 APK。
- 如要進一步瞭解如何縮減 APK 中的程式碼大小,請參閱 Android 訓練課程,瞭解如何避免使用依附元件插入架構。
允許應用程式移至外部 (SD) 儲存空間
- 低成本裝置通常具有少量的裝置儲存空間。使用者可以透過 SD 卡擴充這項功能;不過,應用程式需要明確宣告提供支援至外部儲存空間後,使用者才能移動這些項目。
- 在 AndroidManifest.xml 中使用
android:installLocation
標記,允許將應用程式安裝至外部儲存空間。如要進一步瞭解如何啟用應用程式移至外部儲存空間,請參閱「應用程式安裝位置」這篇 Android 指南。
減少安裝應用程式後的磁碟用量
- 如果應用程式的磁碟用量偏低,表示裝置可用空間不足時,使用者解除安裝應用程式的可能性較低。請務必為快取套用邊界,以免應用程式的磁碟用量無限期增長。請務必將快取資料存放在
getCacheDir()
,因為系統可能會視需要刪除放在這裡的檔案,使其不會顯示在承諾使用的儲存空間中。
提供可設定的網路用量
Android 平台提供多種方式,讓使用者能控管應用程式的網路使用情況,並依照自身需求進行最佳化。舉例來說,首次使用時,應用程式可引導使用者完成各種網路相關設定。您也可以在應用程式外提供網路偏好設定畫面。
針對使用者的網路選擇提供新手上路體驗
- 對於可減少資料使用的應用程式,開發人員相當重視應用程式,即使其有高數據用量的要求,也能照常使用。如果應用程式使用大量頻寬 (例如影片串流應用程式),您可以為使用者提供新手上路體驗,讓他們設定網路用量。舉例來說,您可以允許使用者透過行動網路強制執行較低的位元率影片串流。
- 使用者可透過其他設定控管資料同步處理、預先擷取和網路使用行為 (例如只在 Wi-Fi 上預先擷取所有已加星號的新聞類別),也可以協助使用者根據自身需求調整應用程式行為。
- 如要進一步瞭解如何管理網路用量,請參閱管理網路用量中的 Android 訓練。
提供網路偏好設定畫面
- 您可以透過網路偏好設定畫面,從應用程式外前往應用程式的網路設定。您可以從系統設定畫面或系統資料使用畫面叫用這個畫面。
- 若要提供使用者可從您的應用程式和系統設定存取的網路偏好設定畫面,請在應用程式中加入支援
ACTION_MANAGE_NETWORK_USAGE
動作的活動。
- 如要進一步瞭解如何新增網路偏好設定畫面,請參閱「
實作偏好設定活動」中的 Android 訓練課程。
其他資源
如要進一步瞭解這個主題,請參閱下列其他資源:
網誌文章
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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)"]]