skip to Main Content

I’m upgrading my targetSdkVersion to 34

buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 26
        compileSdkVersion = 33
        targetSdkVersion = 34
        supportLibVersion = 34
    }

and I’ve run into this error when running the react-native app.

I’ve seen a lot of solutions online that suggest adding

context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED);

But they don’t mention where it needs to be added. I’ve only worked on the react-native side so it would help if I had more details about the Android specific updates.

2

Answers


  1. Chosen as BEST ANSWER

    I added the following to MainApplication.java file.

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

    and before public void onCreate() in the same file,

      @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);
        }
      }
    

    In android/build.gradle under dependencies

    classpath("com.android.tools.build:gradle:7.3.1")
    

    In android/gradle/wrapper/gradle-wrapper.properties

    distributionUrl=https://services.gradle.org/distributions/gradle-7.5.1-all.zip
    

    I also kept getting the error

    couldn't find DSO to load: libhermes.so

    For which I had to upgrade react-native-reanimated to 3.5.4.

    I also upgraded my react-native-screens to 3.22.0.

    My other main library versions

        "react-native": "0.71.4",
        "react": "18.2.0",
        "@react-navigation/native": "^6.1.6",
        "@react-navigation/stack": "^6.3.16",
    

    This fixed things for me


  2. What I did to solve this is that I opened up the node_modules for my React Native project (code node_modules) and I searched for all the occurences of registerReceiver. Then I looked at the libraries in the search findings and like that I knew which libraries I have to upgrade. Most libs already made a release to support SDK 34, but I also found libs that did not: for these libs I used a patch-package to create a patch file where I added the corresponding flag myself to the registerReceiver call(s).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search