Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to enable multidex in your react native project, let first understand why we need to enable multidex?

why we need to enable multidex?

When we added many dependencies in our project, it may bump you over the 64k method limit on the Android build system. Once this limit has been reached, you will start to see the following error whilst attempting to build your Android application:

Execution failed for task ':app:mergeDexDebug'.

When we face multidex issues?

your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need to enable the multidex.

However, if your minSdkVersion is set to 20 or lower, then you must enable the multidex and make the following modifications to your app project:

How to enable multidex in react native

To enable it’s very simple, follow below three steps and solve your multidex issue

Step 1

First, we have to tell gradle to turn on multidex with a directive.

Open the /android/app/build.gradle file and enable multidex on the defaultConfig.

build.gradle
android {
    defaultConfig {
        // ...
        multiDexEnabled true // <-- ADD THIS in the defaultConfig section
    }
    // ...
}

Step 2

Second, we have to add multidex dependencies, Open the /android/app/build.gradle file and find dependencies section and add implementation 'androidx.multidex:multidex:2.0.1' dependencies like below.

build.gradle
dependencies {
  implementation 'androidx.multidex:multidex:2.0.1'  // <-- ADD THIS DEPENDENCY
}

Step 3

The 3rd step is to alter your android/app/src/main/java/.../MainApplication.java file to extend MultiDexApplication like so:

MainApplication.java


// ... all your other imports here
import androidx.multidex.MultiDexApplication; // <-- ADD THIS IMPORT


// Your class definition needs `extends MultiDexApplication` like below
public class MainApplication extends MultiDexApplication implements ReactApplication {

Once added, rebuild your application: npx react-native run-android.

Step 4

Step Four is only for that dev who getting still issues after being done and test step 3.

Due to different versions, maybe you will be getting issues after doing all the above steps.

No worry, Change your multidex dependencies

build.gradle

dependencies {
  implementation 'com.android.support:multidex:1.0.3'  // <-- CHANGE TO THIS DEPENDENCY
}

And MainApplication.java file also

MainApplication.java


// ... all your other imports here
import android.support.multidex.MultiDexApplication; // <-- CHANGE TO THIS IMPORT

Once added, rebuild your application: npx react-native run-android.

Thanks for Reading…