How to Reduce React Native App Size: Unlock Faster Load Times

Reducing React Native App Size Strategies and Best Practices

When building mobile apps with React Native, performance and user experience go hand in hand. But one of the most overlooked factors that directly impacts both is app size. A bloated app not only takes longer to download but also eats up device storage and slows down performance.

If your React Native app feels too heavy or takes up more space than it should, you’re not alone. Many React Native app developers struggle with optimizing app size without sacrificing functionality or design.

In this blog, we’ll break down how to reduce React Native app size using proven methods. Whether you’re a beginner or seasoned dev, these tips will help you ship faster, lighter apps that users love.

What Contributes to React Native App Size?

To effectively reduce React Native app size, it’s important to understand the key factors that contribute to it. Identifying these areas allows you to target specific components of your app for optimization. Here’s a breakdown of what typically makes up the size of your React Native app:

  • JavaScript bundle: The JavaScript bundle is the core of your app’s functionality, containing all the JavaScript code that runs on both iOS and Android. This bundle can grow large if there are unnecessary libraries, unoptimized code, or unused features. The larger the bundle, the bigger the app size.
  • Images and assets: Media files like images, fonts, and other assets can take up a significant portion of your app’s size. Unoptimized images or large media files can unnecessarily inflate the app’s size.
  • Native dependencies: React Native relies on native modules (iOS and Android code) to access device features and functionality. These dependencies, while essential for performance, can increase the app size, especially if there are too many unnecessary modules included.
  • Unused code and libraries: Sometimes, developers include libraries or code that aren’t used in the app. These unused components can unnecessarily increase the app’s size, taking up space without adding value.

Understanding what contributes to the size of your app is the first step in finding effective ways to reduce the React Native app size. By addressing these areas, you can significantly shrink your app without sacrificing its core functionality.

Struggling With Bulky Apps? Let Us Handle it.

How to Reduce the React Native App Size? (Quick Methods)

Optimizing your build configuration is an effective way to reduce the React Native app size. By adjusting specific settings in your project’s build process, you can remove unnecessary code, optimize assets, and improve your app’s overall size and performance. Let’s explore a few key techniques for optimizing your build configuration.

Create Separate APKs for Each CPU Architecture

When you generate an APK for your React Native app, it typically includes native code for both x86 and ARMv7a CPU architectures. While this ensures that your app runs smoothly on a wide range of devices, it also increases the overall APK size since both CPU architectures are bundled together. However, not every device needs the code for both architectures–each device only requires the native code that corresponds to its own CPU type.

To address this issue and optimize your app’s size and performance, you can configure your project to generate separate APKs for each CPU architecture. By doing this, only the necessary native code for each device is included in the APK, reducing the app’s size for users.

To enable this feature, open the android/app/build.gradle file and add the following line:

def enableSeparateBuildPerCPUArchitecture = true

This ensures that separate APKs are created for each supported CPU architecture, leading to a more efficient and optimized app size.

Enable Hermes Engine

Hermes is a JavaScript engine specifically designed to optimize React Native apps. By using Hermes, you can significantly reduce the APK size and boost the overall performance of your app, leading to quicker launch times and more efficient memory usage. To enable Hermes in your React Native project, follow these steps:

1. In your android/app/build.gradle file, add the following configuration to enable Hermes:

project.ext.react = [
    enableHermes: true  // <- Enable Hermes
]

2. After making this change, clean the build and rebuild the app:

cd android
./gradlew clean
./gradlew assembleRelease

Enabling Hermes typically results in a 20-30% reduction in APK size and also contributes to faster app launch times, making it an excellent optimization for your React Native app.

Enable Proguard for Android

Proguard is a tool that helps shrink and optimize the size of your Android app by removing unused code and obfuscating the remaining code. Enabling Proguard is a simple yet powerful way to reduce React Native app size on Android.

To enable Proguard, you need to modify the android/app/build.gradle file and set the minifyEnabled property to true.

android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

The minifyEnabled property ensures that Proguard is enabled during the release build. The shrinkResources property removes unused resources (like images and strings) from the app. After enabling Proguard, your app will be smaller and more optimized.

ResConfig

With the resConfigs option, you can selectively include specific resources in your APK, which helps reduce its overall size. By limiting the number of resources to only the essential ones, you can significantly cut down on the size of the APK.

For example, if your app only needs resources for the English language and certain screen densities like hdpi and xhdpi, you can specify them in your android/app/build.gradle file as follows:

android {
    defaultConfig {
        // Other configurations...
        resConfigs "en", "hdpi", "xhdpi" // Include only English, hdpi, and xhdpi resources
    }
}

This will ensure that only the necessary resources are bundled in your APK, reducing its size and optimizing your app for specific locales and screen types.

Try R8 Code Shrinker

R8 is the default code shrinker and obfuscator in Android builds, replacing Proguard with improved performance and smaller output. It helps reduce the size of your APK by removing unused code, classes, methods, and attributes, and it also performs code obfuscation to further minimize the final app size.

To use R8, simply ensure that minifyEnabled is set to true in your android/app/build.gradle file. Since R8 is enabled by default when you use Proguard configurations, no additional setup is needed if Proguard is already part of your build:

android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

R8 works together with resource shrinking (shrinkResources true) to aggressively reduce APK size by stripping out unused code and assets. It’s a powerful optimization tool that requires minimal configuration but can yield significant reductions in app size.

Enable Separate Builds

To reduce the size of your APK, you can create separate builds for different CPU architectures such as armeabi-v7a, arm64-v8a, and x86. This approach ensures that each device only receives the necessary native code, making the APK smaller and more optimized for the specific architecture.

To enable separate builds, add the following code to your android/app/build.gradle file:

android {
    splits {
        abi {
            enable true
            reset()
            include 'armeabi-v7a', 'arm64-v8a', 'x86'
            universalApk false
        }
    }
}

By doing this, your app will generate smaller APKs tailored to the specific CPU architectures, leading to a reduced file size for users and better performance.

Use Android App Bundles

Instead of generating a single APK for all devices, Android App Bundles allow you to create APKs that are optimized for each device configuration (such as screen size, architecture, and language). This can significantly reduce the size of the APK that users download.

To build an Android App Bundle, modify your android/app/build.gradle file and ensure the bundle task is set up correctly:

android {
    bundle {
        enableSplit = true
    }
}

When you generate the release build (./gradlew bundleRelease), it will create a .aab file instead of an APK. The Google Play Store then delivers optimized APKs to users, reducing the overall app size.

Optimize iOS Build

For iOS, there are several optimizations you can make to reduce the app size. One key strategy is enabling app thinning, which reduces the size of the app by only including the necessary resources for the target device. To enable app thinning, add the following settings in your ios/Podfile:

platform :ios, ‘10.0’

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'React-Core'
      target.build_configurations.each do |config|
        config.build_settings['ONLY_ACTIVE_ARCH'] = 'YES'
      end
    end
  end
end

This code ensures that the build is optimized for the active architecture (e.g., x86_64 or arm64) and reduces unnecessary resources for other architectures.

Remove Unused Architectures for iOS

By default, React Native includes all device architectures in the build. However, for most apps, you only need the architecture for the devices you are targeting. Removing unused architectures can help reduce React Native app size. To remove unused architectures, add the following code to your ios/Podfile:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'React-Core'
      target.build_configurations.each do |config|
        config.build_settings['VALID_ARCHS'] = 'arm64 x86_64'
      end
    end
  end
end

This code removes unnecessary architectures and ensures that only the essential ones are included in your final build.

By optimizing your build configuration, you can make significant strides in reducing the React Native app size. Enabling Proguard for Android, using Android App Bundles, and applying app thinning for iOS are all powerful techniques to make your app leaner and faster.

These optimizations, combined with other strategies like code splitting and asset compression, will help you create a more efficient and user-friendly React Native app.

Use Over-the-Air (OTA) Updates to Reduce React Native App Size

Another approach to managing app size is by using Over-the-Air (OTA) updates. Services like Microsoft CodePush allow you to push updates to the JavaScript code of your app without requiring a full app update. This helps reduce the need to re-download the entire app and lets you make small, incremental updates.

By using OTA updates, you can reduce the React Native app size over time by keeping updates small and focused on necessary changes.

Take Your React Native App to the Next Level!

FAQs on Reducing React Native App Size

How to minify React Native apps?

To minify React Native apps, enable Proguard for Android and Hermes engine for both Android and iOS. Proguard removes unused code and shrinks the APK size. Hermes compiles JavaScript into bytecode, reducing runtime size and improving performance. Also, remove unused libraries, compress assets, and use release builds (–variant=release) for production.

What is resize mode in React Native?

resizeMode in React Native is a property used with the Image component to control how the image fits into its container. Common values are:
1. cover: scales the image to fill the space, may crop
2. contain: fits the whole image without cropping
3. stretch: stretches the image to fit, may distort
4. center: centers the image without scaling
It helps keep your UI responsive and images clean on different screens.

How do I reduce RAM usage in React Native?

To reduce RAM usage in React Native, avoid unnecessary re-renders with React.memo, clean up unused listeners or timers, and use FlatList or SectionList for large lists instead of ScrollView. Also, reduce the use of heavy third-party libraries, keep state minimal, and use lazy loading for screens or components not needed right away. Monitoring with tools like Flipper can help spot memory issues early.

Let’s Summarize

Reducing app size isn’t just about saving storage, it’s about delivering a faster, smoother experience for your users. By using tools like Proguard, optimizing assets, and avoiding unnecessary dependencies, React Native app developers can significantly cut down app weight without losing functionality.

The key is to make optimization a habit, not an afterthought. Keep testing, monitor performance, and always look for areas to improve. A smaller app means quicker downloads, better retention, and happier users, and that’s a win for everyone.

If you need professional help with managing your React Native app, consult a React Native app development agency to ensure the best results!

author
Vish Shah is Technical Consultant at WPWeb Infotech since 2015. He has vast experience in working with various industries across the globe. He writes about the latest web development technologies and shares his thoughts regularly.

Leave a comment