50

I am developing a small dictionary app by using react native with expo.

As I am compiling to Apk file. The size goes up to 30mb and after having installed on a device, it goes to 80mb.

Is this normal?

Are there any ways to reduce the size of the app?

Thank you guys.

pstanton
  • 35,033
  • 24
  • 126
  • 168
Sras
  • 1,686
  • 2
  • 19
  • 29

11 Answers11

55

For android APK bundle using expo, you can use the following command:

expo build:android -t app-bundle

The size will be much smaller once you upload it to the play store. This is relevant for React Native SDK 32 and earlier.

jakobinn
  • 1,832
  • 1
  • 20
  • 20
40

the expo is for development the app you should migrate to react-native for production

  • make a new react app "react-native init"

  • Copy the source files over from Expo project

  • Install all dependencies of the Expo project except Expo specific libraries.

  • Make necessary adjustments to app.json file

  • Download the signing key of your Android app from Expo using exp

  • fetch:android:keystore and set it up



This reduces your app dramatically you can also enable proguard and specific build for cpu architecture

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



for more info visit https://medium.com/@aswinmohanme/how-i-reduced-the-size-of-my-react-native-app-by-86-27be72bba640

using expo component

after you done and want publish with less size or just wan't to use a native library expo give you an option called ExpoKit this also can be used with already build with native code react projects

1-run command expo eject to add ExpoKit (choose the "ExpoKit" option)
(no need to do this if you have copied files manually or using native project)
2 -start expo packager with expo start.Leave this running and continue with the following steps.
3- link library for android and ios, this command mostly do this react-native link, sometime this will not work and you should do it manually for this means visit expokit
PS: I didn't test this so if this not work inform me

Community
  • 1
  • 1
nima moradi
  • 2,300
  • 21
  • 34
  • 5
    It works great, reducing from 32mb to 9mb. And, even 7mb. Awesome. – Sras Apr 24 '18 at 14:55
  • 2
    It works great, reducing from 32mb to 9mb. And, even 7mb. Awesome. However, another problem is when it is installed on device, it goes up to 22mb. How can I solve this problem? – Sras Apr 24 '18 at 15:31
  • i don't know how to reduce installed app size , and i will be glad to know, but in general app will be bigger cause apk is like compressed file – nima moradi Apr 25 '18 at 05:22
  • 7
    How can I use expo specific libraries in new project? – Vrishank Oct 09 '18 at 07:21
  • 5
    Please be more specific on how to do this: **Install all dependencies of the Expo project except Expo specific libraries** – Rishav Kumar Oct 22 '18 at 07:16
  • 1
    Please reply When I need to use expo Asset or ImagePicker, Apploading then how do we use? – Diptesh Atha Jun 19 '19 at 07:12
  • 9
    Wrong, Expo is not for app testing only. This answer doesn't work for those with Expo dependent libraries. – Caio Mar Dec 03 '19 at 16:08
  • 1
    Instead of copying the source code from the existing expo project to another project I think its best to eject from expo and then build the apk using react-native build command – mirsahib Dec 06 '21 at 06:36
13

Make following changes in build.gradle file located at:

./android/app/build.gradle

Remove x86 from abi filters.

splits {
        abi {
            reset()
            enable true
            universalApk false
            include "armeabi-v7a", "x86"
        }
    }

Generate different APK's for different architecture

def enableSeparateBuildPerCPUArchitecture = true

Enable ProGuard :

def enableProguardInReleaseBuilds = true

Also set minifyEnabled true and shrinkResources true

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

Also, you can have different build types for development and release builds(depends on your user base)

buildTypes {
        debug {
            ndk {
                abiFilters "armeabi-v7a", "x86"
            }
            ....
        }

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

Also don't forget to remove the unused FONT Files

akshay gore
  • 946
  • 8
  • 21
  • we must need to define different version codes though (e.g. def versionCodes = ["armeabi-v7a":18, "x86":19, "arm64-v8a": 20]). I failed multiple times to upload multiple apks due to this mistake. – Amit Bravo Apr 29 '19 at 13:13
  • Why should we remove `x86` form abi filters? is it not needed? – Vinil Prabhu Jun 21 '19 at 07:59
  • @VinilPrabhu did you get any reply? – Yossi Jul 07 '19 at 17:22
  • @akshaygore When I remove x86, I get only an arm apk. I tried replaced "armeabi-v7a" with x86 and I get only an x86 build. I tried to have the two configrutaions in a single build.gradle and I am getting an error. Do you have an example for how to do that? – Yossi Jul 07 '19 at 17:34
  • 2
    @Yossi i didn't find any answer to this, i have kept api filters as `"armeabi-v7a", "x86"` – Vinil Prabhu Jul 08 '19 at 04:34
  • @Yossi check this [answer](https://stackoverflow.com/a/56928500/5895297) – Vinil Prabhu Jul 10 '19 at 04:24
  • This helped me (I am not using expo): https://github.com/facebook/react-native/issues/23575#issuecomment-472726816 – Yossi Jul 10 '19 at 13:47
  • 1
    @Yossi If you don't want to support the Intel architectures or emulators then you can remove it. – akshay gore Jul 12 '19 at 21:00
8

As of 2021, there is now a much easier option to reduce Android binary size, since ejecting/migrating to pure react-native, switching to bare workflow and building your own binaries locally requires a lot of work and study if you don't know how to do it already or don't have your environment and tools all set up.

In app.json, make the following changes:

"expo": {
...
  "android": {
    "enableDangerousExperimentalLeanBuilds": true
  }
}

This alone should reduce app size in ~40-50% depending on your project.

There's a few things you should know about using this option, so you should read https://github.com/expo/fyi/blob/master/managed-app-size.md. It's still WAY easier than ejecting.

on top of that, you should still use app bundles (.aab) to get another ~50% size reduction after uploading to the play store:

expo build:android -t app-bundle

Pedro Affonso
  • 1,656
  • 15
  • 26
  • 3
    i have you this "enableDangerousExperimentalLeanBuilds": true my app was 80mb and now it is 59mb – Dafy Feb 08 '21 at 18:00
  • 1
    @SyedSadaifRizvi what version of expo SDK are you using? I tried the enableDangerousExperimentalLeanBuilds set to true in SDK40 but the build fails – Nate Mar 08 '21 at 23:05
  • 1
    This actually broke my app. After adding this it only works in expo but crashes after adding to play store. – Adarsh Khatri Jun 15 '21 at 12:02
  • 1
    @AdarshKhatri I guess that's why they put "dangerous" in the property name, probably it's a little unpredictable. I also believe it now is deprecated in favor of EAS builds, that allow a huge size reduction according to the documentation (haven't tried this yet) – Pedro Affonso Jun 16 '21 at 00:16
2

the other way to reduce your app size without manually copy-paste your code in another repo. In app.json or app.config.js just set it enableDangerousExperimentalLeanBuilds to true, by doing this expo won't install the unnecessary node modules when you are running expo build:android -t app-bundle.

If you run into any crashes compare the native node modules which are installed before enabling enableDangerousExperimentalLeanBuilds and install only the required one. You can find the native node modules installed in the expo.io build artefacts.

here are some node module that come along with managed flow which you may not require

expo-ads-admob:8.3.0 expo-analytics-amplitude:8.3.1 expo-ads-facebook:8.4.0 expo-analytics-segment:9.0.0 unimodules-constants-interface:5.3.0 expo-app-auth:9.2.0 expo-apple-authentication:2.2.1 unimodules-app-loader:1.3.0 unimodules-font-interface:5.3.0 unimodules-image-loader-interface:5.3.0 unimodules-permissions-interface:5.3.0 expo-application:2.3.0 expo-av:8.6.0 expo-background-fetch:8.5.0 expo-task-manager:8.5.0 unimodules-task-manager-interface:5.3.0 expo-barcode-scanner:9.0.0 unimodules-barcode-scanner-interface:5.3.0 expo-battery:3.0.0 expo-blur:8.2.0 expo-brightness:8.3.0 expo-calendar:8.5.0 expo-camera:9.0.0 unimodules-camera-interface:5.3.0 unimodules-face-detector-interface:5.3.0 unimodules-file-system-interface:5.3.0 expo-cellular:2.3.0 expo-constants:9.2.0 expo-contacts:8.5.0 expo-crypto:8.3.0 expo-device:2.3.0 expo-document-picker:8.4.0 expo-error-recovery:1.3.0 expo-face-detector:8.3.0 expo-facebook:9.0.0 expo-file-system:9.2.0 expo-firebase-analytics:2.5.0 expo-firebase-core:1.2.0 expo-font:8.3.0 expo-gl:9.1.1 expo-gl-cpp:9.1.2 expo-gl-cpp-legacy:8.4.0 expo-google-sign-in:8.3.0 expo-haptics:8.3.0 expo-image-loader:1.2.0 expo-image-manipulator:8.3.0 expo-image-picker:9.1.1 expo-permissions:9.3.0 expo-in-app-purchases:9.0.0 expo-intent-launcher:8.3.0 expo-keep-awake:8.3.0 expo-linear-gradient:8.3.0 expo-local-authentication:9.3.0 expo-localization:9.0.0 expo-location:9.0.0 expo-mail-composer:8.4.0 expo-media-library:9.2.1 expo-module-template:8.4.0 expo-network:2.3.0 expo-notifications:0.7.2 expo-payments-stripe:8.3.0 expo-print:9.1.0 expo-screen-capture:1.1.1 expo-screen-orientation:2.0.0 expo-secure-store:9.2.0 expo-sensors:9.1.0 unimodules-sensors-interface:5.3.0 expo-sharing:8.4.1 expo-sms:8.3.1 expo-speech:8.4.0 expo-splash-screen:0.6.2 expo-sqlite:8.4.0 expo-store-review:2.2.0 expo-updates:0.3.3 expo-video-thumbnails:4.3.0 expo-web-browser:8.5.0

Vijay
  • 169
  • 7
2

My suggestion will be for you to upgrade to EAS, with EAS your build size is reduced by more than 3/4, however, at this point, you have to pay $29 but I promise you it's worth it. An app I have been working on prompting into EAS reduced the bundle size from 80MB to 16MB. EAS also has EAS submit which enables you submit your app to the stores in a Flash

Novak254
  • 439
  • 6
  • 14
  • Not true, you can build through eas locally I described it it this "share the knowledge" Q&A - https://stackoverflow.com/questions/71592608/expo-build-eas Also I think there is (it may've been added later) a free tier - https://expo.dev/pricing – jave.web Mar 29 '22 at 12:56
2

You can avoid copying the source code from the existing expo project to another project as suggested in this comment

If you use eas build (not expo build) you can try to set up enableProguardInReleaseBuilds param in app.json.

Example:

{
  "expo": {
    "plugins": [
      [
        "expo-build-properties",
        {
          "android": {
            "enableProguardInReleaseBuilds": true,
          }
        }
      ]
    ]
  }
}

Reference: https://docs.expo.dev/versions/latest/sdk/build-properties/#pluginconfigtypeandroid

alx
  • 74
  • 3
1

Upgrade react-native to newer version, for different versions it is giving different size apk.

for 0.57 it was ≈12mb

for 0.59.3 it was ≈30mb

and for 0.59.9 it was ≈15mb

always try to use updated react-native version

Vinil Prabhu
  • 1,279
  • 1
  • 10
  • 22
0

There are basically two options for reducing size of apk file from 32mb(expo) to 7mb(react-native cli).

  1. Detach from expo
  2. create new react-native cli project and copy all source from expo to new react-native project.
Sras
  • 1,686
  • 2
  • 19
  • 29
0

Upgrade expo version to 31.exporecently launched single-SDK builds for Android. These builds contain only the SDK version the app uses when built and are both faster and slimmer. it helps for smaller standalone app build size. release note

Anjal Saneen
  • 3,109
  • 23
  • 38
0

Vote over here so that expo can look into the feature https://expo.canny.io/feature-requests/p/reducing-app-size Problem is react-native uses hermes so the packaging is similar to what native android app from android studio can do but since herme is like latest about 1-2year expo team has not made the support for hermes please vote.. And I also totally agree with @jakobinn

Rahul Shakya
  • 1,269
  • 15
  • 15