Search code examples
androidreact-nativemetro-bundlertargetsdkversion

Updated compileSdkVersion to 34 in React Native project, but unable to connect to Metro server


I recently updated my React Native project's compileSdkVersion from 33 to 34 to comply with Google's new policy. After making this change, I am unable to connect to the Metro server. Here are the details of my android/app/build.gradle configuration:

ext {
    buildToolsVersion = "33.0.0"
    minSdkVersion = 28
    compileSdkVersion = 34
    targetSdkVersion = 34
    ndkVersion = "25.0.8775105"
}

android {
    compileSdkVersion = 34

    defaultConfig {
        minSdkVersion = 28
        targetSdkVersion = 34
    }
}


My project setup includes:

  • React version: 18.2.0
  • React Native version: 0.72.3

Steps I've taken so far:

  1. Updated compileSdkVersion and targetSdkVersion to 34 in android/app/build.gradle.
  2. Cleaned the project using ./gradlew clean.
  3. Reinstalled node modules rm -rf node_modules && npm install or yarn install
  4. Rebuilt the project with npx react-native run-android
  5. Started the Metro server manually using npx react-native start Despite these steps, the Metro server fails to connect when running the app. I have also verified that all my dependencies are up to date.

Solution

  • First update the android/build.gradle

     compileSdkVersion = 34
    targetSdkVersion = 34
    

    if your code work file then ok In my case my app crash after these changes so I reslved app crash my changes these

    MainApplication.java

        // Add imports
       import android.content.BroadcastReceiver;
       import android.content.Intent;
       import android.content.IntentFilter;
       import android.os.Build;
       import org.jetbrains.annotations.Nullable;
    
    
       // ...
       // Put this above  "public void onCreate()":
      @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);
        }
      }
    // ....
    

    app/build.gradle

        dependencies {
         //  ...
        implementation 'org.jetbrains:annotations:16.0.2'
        // ...
    }