解决React Native编译报错:找不到FirebaseInstanceIdService
I get it, that build failure when running react-native run-android is super frustrating—especially when you've already tried modifying files in react-native-fcm and still hit the same error. Let's cut to the chase: the real issue is with react-native-firebase, not react-native-fcm, because Firebase removed the FirebaseInstanceIdService class entirely in newer versions of their Messaging SDK.
What's Going Wrong?
com.google.firebase.iid.FirebaseInstanceIdService was deprecated in Firebase Messaging v20.0.0 and fully removed in v21.0.0. Your react-native-firebase dependency is still referencing this old class, which triggers the > Task :react-native-firebase:compileDebugJavaWithJavac FAILED error. The changes you made to react-native-fcm don't fix this because the build failure is originating from the react-native-firebase module.
Step-by-Step Solution
1. Update RNFirebaseInstanceIdService.java
Head to node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/messaging/RNFirebaseInstanceIdService.java and replace its content with this updated code that uses the current Firebase APIs:
package io.invertase.firebase.messaging; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; import com.facebook.react.bridge.ReactContext; import com.google.firebase.messaging.FirebaseMessagingService; public class RNFirebaseInstanceIdService extends FirebaseMessagingService { private static final String TAG = "RNFirebaseInstanceIdService"; @Override public void onNewToken(String token) { super.onNewToken(token); Log.d(TAG, "Refreshed Firebase token: " + token); // Broadcast the updated token to your React Native app Intent tokenRefreshIntent = new Intent("com.google.firebase.MESSAGING_EVENT"); Bundle bundle = new Bundle(); bundle.putString("token", token); tokenRefreshIntent.putExtras(bundle); final Intent finalIntent = tokenRefreshIntent; new Handler(Looper.getMainLooper()).post(() -> { ReactInstanceManager reactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager(); ReactContext currentContext = reactInstanceManager.getCurrentReactContext(); if (currentContext != null) { LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(finalIntent); } else { // Wait for React context to initialize before broadcasting reactInstanceManager.addReactInstanceEventListener(context -> LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(finalIntent) ); if (!reactInstanceManager.hasStartedCreatingInitialContext()) { reactInstanceManager.createReactContextInBackground(); } } }); } }
2. Fix RNFirebasePerformance.java (If You See Errors Here)
If your build also fails on RNFirebasePerformance.java (located at node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/perf/RNFirebasePerformance.java), check for any references to deprecated Firebase Performance classes. For example, replace calls to old metric creation methods with the latest SDK equivalents (like using FirebasePerformance.getInstance().newHttpMetric() instead of deprecated constructors).
3. Make Changes Permanent (Don't Lose Them!)
Editing files in node_modules is temporary—they'll get overwritten the next time you run npm install or yarn install. Use patch-package to save these fixes:
- Install the tool:
npm install patch-package --save-dev(oryarn add patch-package --dev) - After making your code changes, run:
npx patch-package react-native-firebase - This creates a
patches/folder in your project with the diffs, which will automatically apply on future dependency installs.
4. Clean and Rebuild
Finally, clean your Android build to clear any cached errors:
cd android && ./gradlew clean && cd ..
Then run react-native run-android again—it should compile successfully now.
内容的提问来源于stack exchange,提问作者CoderHana

