React Native Low Version(<0.74.0) Upgrade targetSdkVersion to 34

1. Make Sure the compileSdkVersion is correct.

Cause if you don’t have the correct version of compileSdkVersion, the Context.RECEIVER_EXPORTED will not found.

build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
buildscript {
// ...
ext {
buildToolsVersion = "***"
minSdkVersion = ***
compileSdkVersion = 33
targetSdkVersion = 34
ndkVersion = "***"
googlePlayServicesAuthVersion = "***"
}
// ...
}

2. Rewrite the registerReceiver in MainApplication.java

Make sure the registerReceiver is override before the onCreate function.

MainApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ...
@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);
}
}

@Override
public void onCreate() {
// some code here
}
// ...

3. Clean Project and Rebuild the apk package

Reference here: React Native App Crashes — On upgrading to targetSdkVersion 34(Android 14)