通用链接
了解如何确定你的应用分发给用户时的实际大小,以及如何深入了解你的应用大小并对其进行优化。
开发者常关心的问题之一是他们的应用在应用商店中占用多少空间。本指南将帮助你:
🌐 A common concern for developers is how much space their app takes up on the app store. This guide will help you:
- 了解不同的构建工件用于什么
- 确定分发给用户时应用的实际大小
- 了解你的应用大小并对其进行优化
为什么我的应用这么大?
🌐 Why is my app so big?
实际上可能并非如此! 在检查应用的发布构建生成的产物时,那些不熟悉原生 Android 和 iOS 开发的开发者通常会对文件大小感到惊讶——它通常比从应用商店下载的应用要大得多。 这并不是应用将在应用商店分发时的实际大小! 当人们谈论应用大小时,他们指的是用户下载到设备上的应用大小,而不是上传到应用商店或在开发和测试中共享的大小。
有各种类型的构建产物,具有不同的用途,它们几乎都比用户从商店下载应用时看到的要大。这是因为这些构建并没有针对特定设备进行优化,就像从商店下载时那样,而是通常包含了应用在各种设备上运行所需的所有代码和资源。
🌐 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(安卓软件包)
🌐 .apk (Android Package)
当你在 React Native 项目中使用 Gradle 构建 APK 时,默认行为是创建一个通用二进制文件,该文件包含应用支持的所有不同设备类型的所有资源。例如,它包括每种屏幕尺寸、每种 CPU 架构和每种语言的资源,即使单个设备只需要其中的一种。这意味着你可以将这个文件分享给任何人,直接安装到他们的设备上,可能通过 Orbit 或 adb 直接安装,这样就能正常使用。
🌐 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 商店和其他应用商店有一个叫做“应用包”(App Bundles,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 商店的所有新应用必须构建为 Android 应用包 (.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 商店上的“下载大小”(用户在下载应用时在商店列表中看到的大小)。这将是 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 重要指标 下的 应用大小 页面找到此信息。有关更多信息,请参阅 优化应用大小并保持 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.
升级到 React Native 0.73 及以上版本后,为什么我的 APK 大小增加了?
React Native 0.73 将 Android 的 minSdkVersion 升级到了 23。这带来的副作用是更改了 extractNativeLibs 的默认值 ) to false。
🌐 React Native 0.73 bumped the Android minSdkVersion to 23. This had the side effect of changing the default value of extractNativeLibs to false.
如果设置为
false,你的本地库将以未压缩的形式存储在 APK 中。虽然这可能会使你的 APK 更大,但你的应用加载得更快,因为库可以在运行时直接从 APK 加载。
下表显示,虽然 APK 大小增加,这可能会略微影响通过 内部分发 下载的测试者的下载时间,但 Google Play 商店的大小保持不变。
🌐 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.
| SDK | APK(调试版本) | APK(发布版本) | AAB | Google Play |
|---|---|---|---|---|
| 49 | 66 MB | 27.6 MB | 28.2 MB | 11.7 MB |
| 50 | 168.1 MB | 62.1 MB | 27.4 MB | 11.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 文件是包含 .app 打包包和运行 iOS 设备所需的其他资源的 ZIP 文件。它们用于各种类型的分发,包括 App Store、临时分发(Ad Hoc)、企业分发和 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 获取准确的估算:在 App Store Connect 上,导航到 TestFlight,并通过点击构建编号选择你的构建,然后切换到 Build Metadata 标签并点击 App File Sizes。你将看到根据设备类型估算的下载和安装大小列表。实际安装大小也可能会因设备的 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 Analyzer 或 apktool 来检查应用的内容
- 对于 iOS,将 IPA 文件从
app.ipa重命名为app.zip并解压以检查其内容,使用 macOS 工具assetutil来查看 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 应用体积的直接建议。
苹果关于减少 iOS 应用体积的直接建议。