React-native: APK Size is doubled

Created on 21 Feb 2019  Β·  26Comments  Β·  Source: facebook/react-native

πŸ› Bug Report

The APK size is almost doubled in RN 58. In RN 57 the release build of the app was about ~8mb. Now it's ~16mb.

To Reproduce

Create a new project with react-native init and then generate a signed APK using this.

Expected Behavior

~8mb APK size.

Environment

React Native Environment Info:
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5 CPU 760 @ 2.80GHz
Memory: 1.58 GB / 7.99 GB
Binaries:
Yarn: 1.13.0 - C:Program Files (x86)Yarnbinyarn.CMD
npm: 6.4.1 - C:Program Filesnodejsnpm.CMD
IDEs:
Android Studio: Version 3.2.0.0 AI-181.5540.7.32.5014246

Bug Android Locked Docs

Most helpful comment

This is because of JavaScript Core update.
Just change:
def enableSeparateBuildPerCPUArchitecture = false to def enableSeparateBuildPerCPUArchitecture = true in your android/app/build.gradle so APK file will be splitted per architecture resulting in smaller APK files that all should be updated to Google Play and Store will automatically pick up the correct version for user.

All 26 comments

is that because 0.58 support arm64-v8a?
try to remove arm64-v8a support in build.gradle and compile your project again to see the result?

Was the JSC updated on Android? That'd increase the appSize by ~4mb even with abi splitting.

2x 4 = 8mb increase in universal apk I'd guess.

@creambyemute This is the MB growth in my case, RN 0.57.8 app size was around 12.5MB and in 0.58.4 it is at around 20MB

it's about using JSC

If you can use Android App Bundle.
Its extract every platform from APK and user just download what is needed by platform (ARM/X86 32/64bit) and display resolution.

Well, it's not really a bug then. Should this be an issue at all?

@darekg11 it is not issue. RN 0.58 adds support for ARM v8. RN 0.59 adding x86_64.
By gradle tools 3.2 (what is default in RN 0.58) is supported new Android App bundling.
In final our new production app based on RN 0.58 is slimmer than on RN 0.57.

Damn i was wondering why the debug apk was <13 mb since when i used RN 0.56 it was 6 mb.. 6 MB! . I guess its time to generate separate apk files for google play . This should be somewhere in the docs. It was driving me insane, i thought it was my build tools version

Just choose this. It is not APK anymore. But .aab
image

EDIT:

Google Play console must have App key signing enabled.

I create a new project with _react-native init_ (RN 0.59) and then generate a signed APK. The APK size is ~30MB what happend??

APK always contains support for 3 platforms (ARM, ARM64, X86, X86_64 is not bundled for 0.59.0) and includes new JSC (javascript core). Use AAP (Android App Bundle).

It is not a bug.

This is because of JavaScript Core update.
Just change:
def enableSeparateBuildPerCPUArchitecture = false to def enableSeparateBuildPerCPUArchitecture = true in your android/app/build.gradle so APK file will be splitted per architecture resulting in smaller APK files that all should be updated to Google Play and Store will automatically pick up the correct version for user.

Hey all πŸ‘‹thanks for sending this issue over.

The solution provided by @darekg11 is the correct approach. This is also now documented here in a bit more detail.

Thanks for the documentation. However i think that there should also be documentation of the usage of Android app bundle with android studio and without android studio [using only the command line]

@LiteKat thanks for the feedback, could you send a PR to the docs with the changes you think are needed as mentioned above

Thanks

The thing is, universal apk file is like 27mb now, which was around 7mb before for a clean react-native project.
even they added new architectures support such as arm64-v8a and x86_64, that doesnt mean it should go this much high.

Before latest updates it was around 7mb while it was supporting both armeabi-v7a and x86. and for separated apk files, they were only 3mb and 5mb. but now after these latest updates they all changed and now even x86 and armeabi-v7a are more than 7mb (which was the universal apk file size!)

Same issue. RN 57 app size was 9mb and now (RN 59.9) around 20mb. What should I do?

when upgrading to rn 59 I removed this code from app/build.gradle:

ndk {
     abiFilters ...
}

my app size went up to 54MB.
Using

zipinfo -1 YOUR_APK_FILE.apk | grep \.so$

I found out there are lots of platforms that I don't want, including mips.
So I decided to use abiFilters as below

ndk {
    abiFilters "armeabi-v7a","arm64-v8a"
}

then app size reduced to 22MB.
if you want to support emulators you can add "x86", "x86_64" to filters. And it will cost you with extra app size.

Hi,
Created a new app using
react-native init AwesomeProject (react-native version is 0.60.4)
cd AwesomeProject
generated the aab file using ./gradlew bundleRelease
App size is 22mb
set the enableSeparateBuildPerCPUArchitecture and enableProguardInReleaseBuilds to true in android/app/build.gradle.
generated the aab file using ./gradlew bundleRelease
App size is 22mb

I followed this document

App size is huge. This is not acceptable.
My actual app on which I have worked upon is around 46 mb after optimization.
Not acceptable. My business team with throw me away.

We have developed a True Native App on android which has lots of features and the app size is 7mb.

@arjun-livquik what do you measure? File size of generated aab file? Wrong.

Only right measurement is looking for final size after shrining in Google Play.
You can check it through Release Management -> Artifact Library and Explore build you want.

@radeno,
Sorry, I am new to .aab. I generated APKs and file size looks reasonable. That's a huge relief.

➜ du -h app/build/outputs/apk/release/app-arm64-v8a-release.apk
6.2M    app/build/outputs/apk/release/app-arm64-v8a-release.apk
➜ du -h app/build/outputs/apk/release/app-armeabi-v7a-release.apk
5.9M    app/build/outputs/apk/release/app-armeabi-v7a-release.apk
➜ du -h app/build/outputs/apk/release/app-x86-release.apk
5.9M    app/build/outputs/apk/release/app-x86-release.apk
➜ du -h app/build/outputs/apk/release/app-x86_64-release.apk
6.5M    app/build/outputs/apk/release/app-x86_64-release.apk

Thanks @radeno

You can also give hermes engine a try.

@sijad

After enabling Hermes. This is great. I checked my new apk of actual app. It's reduced significantly.

➜ du -h app/build/outputs/apk/release/app-arm64-v8a-release.apk
3.8M    app/build/outputs/apk/release/app-arm64-v8a-release.apk
➜ du -h app/build/outputs/apk/release/app-armeabi-v7a-release.apk
3.6M    app/build/outputs/apk/release/app-armeabi-v7a-release.apk
➜ du -h app/build/outputs/apk/release/app-x86-release.apk
4.0M    app/build/outputs/apk/release/app-x86-release.apk
➜ du -h app/build/outputs/apk/release/app-x86_64-release.apk
4.0M    app/build/outputs/apk/release/app-x86_64-release.apk

Publishing to other stores

By default, the generated APK has the native code for both x86 and ARMv7a CPU architectures. This makes it easier to share APKs that run on almost all Android devices. However, this has the downside that there will be some unused native code on any device, leading to unnecessarily bigger APKs.

You can create an APK for each CPU by changing the following line in android/app/build.gradle:

- def enableSeparateBuildPerCPUArchitecture = false
+ def enableSeparateBuildPerCPUArchitecture = true

Upload both these files to markets which support device targeting, such as Google Play and Amazon AppStore, and the users will automatically get the appropriate APK. If you want to upload to other markets, such as APKFiles, which do not support multiple APKs for a single app, change the following line as well to create the default universal APK with binaries for both CPUs.

- universalApk false  // If true, also generate a universal APK
+ universalApk true  // If true, also generate a universal APK

Enabling Proguard to reduce the size of the APK (optional)

Proguard is a tool that can slightly reduce the size of the APK. It does this by stripping parts of the React Native Java bytecode (and its dependencies) that your app is not using.

IMPORTANT: Make sure to thoroughly test your app if you've enabled Proguard. Proguard often requires configuration specific to each native library you're using. See app/proguard-rules.pro.

To enable Proguard, edit android/app/build.gradle:

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = true

All details see Official file

Hope it can help you.

Enabling Hermes will significantly reduce app bundle size.

Was this page helpful?
0 / 5 - 0 ratings