React Native App Crashes when targetSdkVersion upgraded to 34 (Android 14)

January 11, 20242 min read
React Native App crash
Photo by Agê Barros on Unsplash

After I upgraded targetSdkVersion to 34 for React Native app, it started crashing on launch with the following error:

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.

I used adb logcat command to find the what is the actual error that caused app to crash on launch. The error happens due to Android 14 requiring broadcast receivers. This ensures secure handling of broadcast intents and prevents unintended access by other apps. Learn more from the Android 14 behavior changes.

Resolving the Issue

Run following command to find all libraries that use registerReceiver.

1grep -R "\.registerReceiver" ./node_modules/

You should get output like following:

1./node_modules//react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java: Intent batteryStatus = getReactApplicationContext().registerReceiver(null, ifilter);
2./node_modules//react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java: Intent intent = getReactApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
3./node_modules//react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java: Intent intent = getReactApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
4./node_modules//react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java: context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED);
5./node_modules//react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java: context.registerReceiver(receiver, filter);
6./node_modules//react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java: mApplicationContext.registerReceiver(mReloadAppBroadcastReceiver, filter);
7./node_modules//react-native-navigation/lib/android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadHandler.java: activity.registerReceiver(reloadReceiver, new IntentFilter(RELOAD_BROADCAST), Context.RECEIVER_EXPORTED);
8./node_modules//react-native-navigation/lib/android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadHandler.java: activity.registerReceiver(reloadReceiver, new IntentFilter(RELOAD_BROADCAST));

Notice that few libraries are using registerReceiver.

Update Affected Packages

Check if the identified libraries have updates. In my case, the issue was resolved by updating:

  • react-native-device-info
  • react-native-navigation

Update dependencies:

1npm install react-native-device-info@latest
2npm install react-native-navigation@latest

Rebuild the app and ensure that it runs on Android 14 without crashing.