Fixing App Crash on Launch After Upgrading targetSdkVersion to 34 (Android 14) in React Native

Problem Overview:

If you’ve recently upgraded your React Native app’s targetSdkVersion to 34 (Android 14), you might have encountered a crash when launching the app. This issue is often accompanied by errors like:

Caused by: java.lang.SecurityException: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn’t being registered exclusively for system broadcasts.

Caused by: java.lang.RuntimeException: Requested enabled DevSupportManager, but BridgeDevSupportManager class was not found or could not be created.

In my experience, packages like react-native-orientation and rn-fetch-blob are potential culprits behind this issue. Below, I will walk you through the steps to resolve this problem.

Step 1: Update MainApplication.java

The crash is related to changes in Android 14 that require explicit declaration of the receiver’s export state when registering a broadcast receiver. To fix this, update your MainApplication.java file:

Code Update:

import android.content.Context; // Ensure single import
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import org.jetbrains.annotations.Nullable;

@Override
public Intent registerReceiver(@Nullable BroadcastReceiver receiver, IntentFilter filter) {
    if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) {
        return super.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED);
    } else {
        return super.registerReceiver(receiver, filter);
    }
}

Note: Insert the above code before the onCreate() method in MainApplication.java.

Step 2: Add org.jetbrains Annotations Dependency

Add the following line to your android/app/build.gradle dependencies:

implementation 'org.jetbrains:annotations:16.0.2'

This resolves issues with @Nullable annotations and ensures compatibility.

Now, re-run your project:

npx react-native run-android

For many users, this should resolve the crash. However, if the issue persists, proceed to the next step.

Step 3: Fix Issues with react-native-orientation

If you are using react-native-orientation, it might still cause the app to crash due to incorrect broadcast receiver registration. You need to make some changes to the package files.

Update build.gradle in react-native-orientation:

Edit the file located at node_modules/react-native-orientation/android/build.gradle:

Before:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
    }
}

After:

android {
    compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : 33
    buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : "33.0.0"

    defaultConfig {
        minSdkVersion rootProject.hasProperty('minSdkVersion') ? rootProject.minSdkVersion : 21
        targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : 34
    }
}

Update OrientationModule.java in react-native-orientation:

Edit the file located at node_modules/react-native-orientation/android/src/main/java/com/github/yamill/orientation/OrientationModule.java:

Before:

activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));

After:

import android.os.Build;
        if(Build.VERSION.SDK_INT >= 34 && activity.getApplicationInfo().targetSdkVersion >= 34) {
            activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"), Context.RECEIVER_EXPORTED);
        } else {
            activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));
        }

These changes ensure that the broadcast receiver is correctly registered for Android 14.

Conclusion

With Android 14, certain behaviors related to broadcast receivers have changed. Explicitly declaring RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED is now mandatory when registering receivers programmatically. By updating your MainApplication.java and fixing issues in packages like react-native-orientation, you can resolve these crashes and ensure compatibility with Android 14.

If you still face issues, check the GitHub issue thread for react-native-orientation for more insights and updates.

Feel free to share your thoughts or additional solutions in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *