通用链接

了解如何确定你的应用分发给用户时的实际大小,以及如何深入了解你的应用大小并对其进行优化。


开发者普遍关心的是他们的应用在应用商店中占用了多少空间。本指南将帮助你:

¥A common concern for developers is how much space their app takes up on the app store. This guide will help you:

  • 了解不同的构建工件用于什么

    ¥Understand what different build artifacts are used for

  • 确定分发给用户时应用的实际大小

    ¥Figure out the actual size of your app when distributed to users

  • 了解你的应用大小并对其进行优化

    ¥Get insights into your app size and optimize it

为什么我的应用这么大?

¥Why is my app so big?

实际上,可能不是!在检查应用发布版本的结果时,不熟悉原生 Android 和 iOS 开发的开发者通常会对文件大小感到惊讶 — 通常,它比他们从应用商店下载应用时预期的要大得多。这不是你的应用的实际大小,它将在应用商店中分发!当人们谈论应用大小时,他们指的是他们将下载到设备的应用的大小,而不是将上传到应用商店或在开发和测试中共享的大小。

¥It probably isn't, actually! When examining the resulting artifact of a release build for an app, it's common for developers who are unfamiliar with native Android and iOS development to be surprised by the file size — it's usually much larger than they would expect for an app if they were to download it from an app store. This is not the actual size of your app, which will be distributed on app stores! When people talk about app sizes, they mean the size of the app that they will download to their device, not the size that will be uploaded to app stores or shared in development and testing.

有各种类型的构建工件可用于不同的目的,它们几乎都比用户从商店下载你的应用时看到的大小要大。这是因为这些构建并未像从商店下载时那样针对特定设备进行优化,而是通常包含你的应用在各种设备上运行所需的所有代码和资源。

¥There are various types of build artifacts that serve different purposes, and they are almost all larger than what users will see when they download your app from a store. This is because these builds are not optimized to target specific devices like they would when downloading from a store, but rather they typically include all of the code and resources that your app needs to run on a wide range of devices.

Android 应用

¥Android apps

你将与两种类型的 Android 构建工件交互:APK 和 AAB。

¥There are two types of Android build artifacts that you will interact with: APKs and AABs.

.apk(Android 包)

¥.apk (Android Package)

当你在 React Native 项目中使用 Gradle 构建 APK 时,默认行为是创建一个通用二进制文件,其中包含你的应用支持的所有不同设备类型的所有资源。例如,它包括每个屏幕尺寸、每个 CPU 架构和每种语言的资源,即使单个设备只需要其中一种。这意味着你可以与任何人共享此文件以直接安装到他们的设备上,也许直接使用 Orbitadb,这样就可以了。

¥When you build an APK with Gradle in a React Native project, the default behavior is to create a universal binary, which contains all the resources for all the different device types that your app supports. For example, it includes asset for every screen size, every CPU architecture, and every language, even though a single device will only need one of each. This means you can share this one file with anybody to install directly to their device, perhaps with Orbit or adb directly, and that will work.

当然,如果你正在运行一个为数百万用户提供服务的非常受欢迎的应用商店,你不想向每个用户发送相同的 50 MB 文件,特别是如果他们只使用 APK 中一小部分资源。这就是为什么 Google Play 商店和其他应用商店有一项名为 "应用包"(Android)的功能,允许你上传单个二进制文件,然后商店将根据每个用户的设备需求为他们生成自定义二进制文件。

¥Of course, if you're running an incredibly popular app store that serves millions of users, you don't want to send the same 50 MB file to every single user, especially if they're only going to use a fraction of the resources in the APK. This is why the Google Play Store and other app stores have a feature called "App Bundles" (Android) that allows you to upload a single binary and then the store will generate a custom binary for each user based on their device's needs.

.aab(Android 应用包)

¥.aab (Android App Bundle)

在 Android 上,提交到 Play Store 的所有新应用都必须作为 Android App Bundle (.aab) 构建。将二进制文件提交到各自的商店后,你将能够看到各种设备类型的下载大小。

¥On Android, all new apps submitted to the Play Store must be built as an Android App Bundle (.aab). Once you have submitted the binaries to their respective stores, you will be able to see the download size for various device types.

确定 Android 应用下载和安装大小

¥Determining Android app download and install size

通常,应用开发者关心 Play Store 上的 "下载大小"(用户下载应用时在商店列表中看到的内容)。这将是 Google Play 从你的 AAB 生成的 APK 的大小,该 APK 是根据用户的设备量身定制的。

¥Typically, what app developers care about the "download size" on the Play Store (what the users see in the store listing when they go to download the app). This will be the size of the APK that Google Play generates from your AAB, which is tailored to the user's device.

查看最终将发送给用户的应用大小的唯一真正准确的方法是将你的应用上传到商店并在物理设备上下载。Google Play 还在你的开发者仪表板上提供了预期下载大小的可靠估计。你可以在 Google Play 开发者控制台 上的 Android Vitals 中的应用大小页面下找到它。欲了解更多信息,请参阅 优化应用大小并保持在 Google Play 应用大小限制内

¥The only truly accurate way to see what your final app size will be shipped to users is to upload your app to the stores and download it on a physical device. Google Play also provides a reliable estimate for the expected download size on your developer dashboard. You can find this under the App size page in Android vitals on the Google Play Developer Console. For more information, see Optimize your app’s size and stay within Google Play app size limits.

Why did my APK size increase after upgrading to React Native 0.73?

Expo SDK 50 中使用的 React Native 0.73 将 Android minSdkVersion 升级到 23。这会产生将默认值 extractNativeLibs 更改为 false 的副作用。

¥React Native 0.73, which is used in Expo SDK 50, bumped the Android minSdkVersion to 23. This had the side effect of changing the default value of extractNativeLibs to false.

如果设置为 false,你的原生库将以未压缩的形式存储在 APK 中。虽然你的 APK 可能更大,但你的应用加载速度更快,因为库在运行时直接从 APK 加载。

¥If set to false, your native libraries are stored uncompressed in the APK. Although your APK might be larger, your application loads faster because the libraries load directly from the APK at runtime.

下表显示,虽然 APK 大小增加了,这可能会稍微影响使用 内部分配 的测试人员的下载时间,但 Google Play Store 大小保持不变。

¥The following table shows that while the APK size increased, which may slightly impact download time for testers with internal distribution, the Google Play Store size remained the same.

SDKAPK(调试版本)APK(发布版本)AABGoogle Play
4966 MB27.6 MB28.2 MB11.7 MB
50168.1 MB62.1 MB27.4 MB11.7 MB

如果你希望恢复到以前的行为,则可以在 gradle.properties 中将 useLegacyPackaging 设置为 true,或使用 expo-build-properties

¥If you would like to revert to the previous behavior, you can set useLegacyPackaging to true in your gradle.properties or by using expo-build-properties.

iOS 应用

¥iOS apps

App Store 上最小 React Native 应用(使用空白模板创建)不到 4 MB 的下载大小。

¥The download size on the App Store of a minimal React Native app (created using the blank template) is just under 4 MB.

你将与两种类型的 iOS 构建工件交互:APP 和 IPA。

¥There are two types of iOS build artifacts that you will interact with: APPs and IPAs.

.app(iOS 应用包)

¥.app (iOS application bundle)

这是你应用的实际应用包。当你下载并将应用的构建安装到 iOS 模拟器中时,你正在下载 .app 包。这些可以针对特定架构,也可以是通用二进制文件。.app 的大小不一定会告诉你太多有关你的应用在商店中的下载大小的信息。你无法将 .app 文件直接安装到物理 iOS 设备上。

¥This is the actual application bundle for your app. When you download and install a build of your app into an iOS Simulator, you are downloading the .app bundle. These can either target specific architectures or be universal binaries. The size of your .app doesn't necessarily tell you too much about what the download size of your app will be on the store. You can't install a .app file directly to a physical iOS device.

.ipa(iOS 应用商店包)

¥.ipa (iOS App Store Package)

IPA 文件是 ZIP) 文件,其中包含 .app 包和在 iOS 设备上运行应用所需的其他资源。它们用于各种类型的分发,包括 App Store、Ad Hoc、Enterprise 和 TestFlight。

¥IPA files are ZIP) files that include the .app bundle and other resources that are needed to run the app on an iOS device. They are used for various types of distribution, including App Store, Ad Hoc, Enterprise, and TestFlight.

它们包括安全和代码签名信息,例如配置文件和权利。App Store 将处理 IPA 文件并将其拆分为针对每种设备类型的较小二进制文件,因此 IPA 的大小也不代表应用的下载大小。

¥They include security and code signing information, such as the provisioning profile and entitlements. The App Store will process the IPA file and split it into smaller binaries for each device type, so the size of the IPA also does not represent the download size of your app.

确定 iOS 应用下载和安装大小

¥Determining iOS app download and install size

通常,应用开发者关心 App Store 上的 "下载大小"(用户下载应用时在商店列表中看到的内容)。这将是商店从你的通用 IPA 生成的拆分 IPA 的大小。

¥Typically, app developers care about the "download size" on the App Store (what the users see in the store listing when they go to download the app). This will be the size of the split IPA generated by the store from your universal IPA.

查看最终将发送给用户的应用大小的唯一真正准确的方法是将你的应用上传到 App Store 并在物理设备上下载。你可以从 TestFlight 获得准确的估计值:在 应用商店连接 上,导航到 TestFlight 并通过单击版本号选择你的版本,然后切换到“构建元数据”选项卡并单击“应用文件大小”。你将看到根据设备类型估算的下载和安装大小列表。实际安装大小也可能因设备的 iOS 版本而略有不同。

¥The only truly accurate way to see what your final app size will be shipped to users is to upload your app to the App Store and download it on a physical device. You can get accurate estimates from TestFlight: on App Store Connect, navigate to TestFlight and select your build by clicking on the build number, then switch to the Build Metadata tab and click App File Sizes. You will see a list of estimated download and install sizes depending on the device type. Actually install sizes may also vary slightly depending on the iOS version of the device.

优化应用大小

¥Optimizing app size

当你向应用添加功能时,你将添加代码、库和资源,这可能会增加其大小。如果应用大小对你和你的用户很重要,你可能需要定期检查大小并对其进行优化。以下部分将帮助你了解可以采取哪些措施来优化应用的几个方面。

¥As you add features to your app, you will add code, libraries, and assets, which may increase its size. If app size is important to you and your users, you may want to routinely review the size and optimize it. The following sections will help you understand what you can do to optimize several aspects of your app.

支持富有表现力的面向对象 API

¥Static assets

应用大小膨胀的最常见来源之一是资源,例如字体、图标、图片、视频和声音。这些可以来自你直接导入代码的资源,以及 JavaScript 和原生库。你无法通过查看应用资源目录来获得完整的图片。

¥One of the most common sources of app size bloat is assets, such as fonts, icons, images, videos, and sounds. These can come from the assets that you import directly into your code, as well as JavaScript and native libraries. You won't be able to get a complete picture by reviewing your app assets directory.

首先检查构建工件以确定其中包含哪些资源。

¥Start by examining a build artifact to determine what assets are included in it.

  • 对于 Android,你可以使用 Android APK 分析器apktool 检查应用的内容

    ¥For Android, you can use Android APK Analyzer or apktool to inspect the contents of your app

  • 对于 iOS,将 IPA 文件从 app.ipa 重命名为 app.zip 并将其提取以检查内容,使用 macOS 实用程序 assetutil 检查 Assets.car。

    ¥For iOS, rename an IPA file from app.ipa to app.zip and extract it to examine the contents, using the macOS utility assetutil to inspect Assets.car.

JavaScript 包大小

¥JavaScript bundle size

要分析 JavaScript 包,使用 Expo Atlas。你可能会发现你认为非常小的库实际上对打包包有很大影响,或者你在停止使用后忘记删除某个库,等等。

¥To analyze JavaScript bundles, use Expo Atlas. You may find libraries that you thought were very small actually have a large impact on the bundle, or that you forgot to remove a library after you stopped using it, and so on.

平台特定优化

¥Platform-specific optimizations

独立于 React Native 和 Expo,你可以使用以下工具优化 Android 和 iOS 应用:

¥Independent of React Native and Expo, you can optimize your app for Android and iOS by using the following tools:

Android 开发者:减小应用大小

来自 Google 的直接建议,关于减少 Android 应用的大小。

Apple 开发者:减小应用大小

来自 Apple 的直接建议,关于减少 iOS 应用的大小。