Site icon Mobile App Development Services

How to reduce apk size in React Native

When we develop an app using React Native, we want to make sure that our app can run on as many different devices as possible, by providing resources that are optimized for different CPU architectures, screens as well as many other factors. Unluckily, this also means that every single device, which downloads your app, will receive a bunch of code and resources that are not going to be used at all.

Android App Bundle

The size of an APK can be reduced by 35% by publishing the app in AAB (Android App Bundle) format – official Android Developers blog.

Only compiled code and resources are contained in the AAB bundle. Google play itself generates an optimized APK for every device type based on its specifications and CPU structure. 

Old APK at 26.8MB compared to the new AAB at 14.4 to 17.7 MB 

Using Android Studio or Command-line tools you can simply build your project as a signed app bundle. To build a signed app bundle from Android studio first, select Build > Generate Signed Bundle/APK from the Android Studio toolbar. The next step is to select the Android App Bundle checkbox and then click the Next button. Then, open the module dropdown, and choose the app as our base module from the list.

Follow the instructions provided on-screen to complete the build process. On completion, Android Studio will generate a .aab file and store it in our computer’s AndroidAppBundle/app/release directory. An Android App Bundle is a file (with the .aab file extension) that you upload to Google Play.

We can also generate signed .aab using the terminal by running the following command in the android folder:

cd android
./gradlew bundleRelease

Enable Resource Shrinking 

Resource shrinking can be used to identify and eliminate unnecessary resources. It can be enabled using the following command:

buildTypes {
       release {
        //Add the following//
    	shrinkResources true
        minifyEnabled true
       }
   }

Enable Proguard

Proguard is used to shrink, optimize, obfuscate, and preverify class files. it detects and eliminates unused classes, methods and attributes and rename the remaining ones using short and meaningless names. It compresses the Java Bytecode and reduces the size of the app a little bit.

To enable it use the following command:

def enableProguardInReleaseBuilds = true

Enable Separate Builds

This is the alternative of Android App Bundle (.aab). If we do not want to generate aab and stick with apk then this is the way to reduce sizes. Basically 2 major architectures, armeabi & x86, are supported by Android devices. When APK is generated via React Native, it contains the native libraries of both the architectures. In order to generate 2 different APKs use the following command:

Set def enableSeparateBuildPerCPUArchitecture = true

Both of the APKs should be uploaded to Google Play, where each one can be distributed to the correct architecture. This can reduce the size of APK around 7mb to 3.5mb for armeabi and 5mb for x86.

Do you know any other tips for reducing APK size? Do let us know in the comments.

Exit mobile version