【问题标题】:How to fix " :react-native-firebase:compileDebugJavaWithJavac FAILED" + cannot find import com.google.firebase.iid.FirebaseInstanceIdService;如何修复“ :react-native-firebase:compileDebugJavaWithJavac FAILED” + 找不到导入 com.google.firebase.iid.FirebaseInstanceIdService;
【发布时间】:2019-06-09 11:04:06
【问题描述】:

最近我克隆了一个新项目并尝试react-native run-android 不幸的是,由于某些原因,我无法运行我的项目并且终端向我显示此错误:> Task :react-native-firebase:compileDebugJavaWithJavac FAILED,在此错误之上,我详细看到了有关此的更多信息 => '找不到符号导入 com.google.firebase。 iid.FirebaseInstanceIdService;'。

我对这个问题进行了很多搜索,发现“FirebaseInstanceIdService”已被弃用,因此我应该在位于 => \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java 的 InstanceIdService.java 中评论 import com.google.firebase.iid.FirebaseInstanceIdService;,此外我还应该进行一些其他更改。但我又收到了同样的错误。

InstanceIdService.java 的内容(修改前):

package com.evollu.react.fcm;

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.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class InstanceIdService extends FirebaseInstanceIdService {

    private static final String TAG = "InstanceIdService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. This call is initiated by the
     * InstanceID provider.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // Broadcast refreshed token
        Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken");
        Bundle bundle = new Bundle();
        bundle.putString("token", refreshedToken);
        i.putExtras(bundle);

        final Intent message = i;

        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                // Construct and load our normal React JS code bundle
                ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
                ReactContext context = mReactInstanceManager.getCurrentReactContext();
                // If it's constructed, send a notification
                if (context != null) {
                    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
                } else {
                    // Otherwise wait for construction, then send the notification
                    mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
                        public void onReactContextInitialized(ReactContext context) {
                            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
                        }
                    });
                    if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                        // Construct it in the background
                        mReactInstanceManager.createReactContextInBackground();
                    }
                }
            }
        });
    }
}

编辑的InstanceIdService.java的内容:

package com.evollu.react.fcm;

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.iid.FirebaseInstanceId;
//import com.google.firebase.iid.FirebaseInstanceIdService; //Commented FirebaseInstanceIdService
import com.google.firebase.messaging.FirebaseMessagingService;  //ADD FirebaseMessagingService

// public class InstanceIdService extends FirebaseMessagingService {
public class MyFireBaseInstanceIDService  extends FirebaseMessagingService {

    private static final String TAG = "InstanceIdService";
    // private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName();;

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. This call is initiated by the
     * InstanceID provider.
     */
    // [START refresh_token]
    @Override
    public void onNewToken(String token) { //Added onNewToken method
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // Broadcast refreshed token
        Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken");
        Bundle bundle = new Bundle();
        bundle.putString("token", refreshedToken);
        i.putExtras(bundle);

        final Intent message = i;

        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                // Construct and load our normal React JS code bundle
                ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
                ReactContext context = mReactInstanceManager.getCurrentReactContext();
                // If it's constructed, send a notification
                if (context != null) {
                    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
                } else {
                    // Otherwise wait for construction, then send the notification
                    mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
                        public void onReactContextInitialized(ReactContext context) {
                            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
                        }
                    });
                    if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                        // Construct it in the background
                        mReactInstanceManager.createReactContextInBackground();
                    }
                }
            }
        });
    }
}

请注意:我在react-native-fcmreact-native-firebase 都有同样的问题,看来我应该对'RNFirebaseInstanceIdService.java'和'RNFirebasePerformance.java'进行一些更改

我自己解决了这个问题后才添加了这个注释:: 这根本不是关于 react-native-fcm 的!正如我之前所说的,这完全是关于 react-native-firebase 的,“尽管在 react-native-fcm 中确实存在已弃用的 'instanceIdService',一旦我尝试进行一些更改。

谢谢

【问题讨论】:

  • 我的建议是使用 react-native-firebase lib 而不是 react-native-fcm,react-native-firebase 与 Google 的最新更改保持同步,因此易于设置,就像魅力。rnfirebase.io/docs/v5.x.x/getting-started
  • @Moein 我对 firebase 有同样的问题 => 我正在尝试在 node-modules/react-native-firebase/...[somewhere].../RNFireBaseInstanceIdService 中进行相同的更改.java
  • 您是否严格按照 firebase 文档进行操作?您不需要对 node_module 文件中的任何文件进行任何更改。因此,首先,从您的项目中卸载 firebase,然后清除您的 node_module 缓存以及项目上所有可能链接的依赖项,然后重新安装 firebase。如果还不能正常工作,它可能会回到你的 firebase 的一些依赖项。请分享你的 android/app/build.gradle 和 android/build.gradle 文件。可能忘记添加一些依赖项
  • 是的,我尝试更新所需的软件包,但它太意大利面了,并且在安装或更改不同版本的 gradle 后依赖于所有软件包的 classPath 或 distributionURL 或在其他地方进行其他更改并面临其他错误不断,我终于决定转移+删除整个节点模块并丢弃其他地方的所有更改并再次重新安装软件包npm install,我终于在节点模块内部进行了一些更改 -react-native-firebase 我要去在这里发布和分享

标签: android firebase react-native firebase-cloud-messaging


【解决方案1】:

所以每次我遇到错误时:> Task :react-native-firebase:compileDebugJavaWithJavac FAILED 会有一些关于RNFirebaseInstanceIdService.java 文件中已弃用的FirebaseInstanceIdService 以及位于RNFirebasePerformance.java 中的incrementCounter 的问题。因为,FirebaseInstanceIdService 已弃用我评论然后导入 FirebaseMessagingService

(1)我在RNFirebaseInstanceIdService.java中所做的更改:

package io.invertase.firebase.messaging;


import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

// import com.google.firebase.iid.FirebaseInstanceIdService;  //I commented this once because it's deprecated
import com.google.firebase.messaging.FirebaseMessagingService;  //I added this one

public class RNFirebaseInstanceIdService extends FirebaseMessagingService {
  private static final String TAG = "RNFInstanceIdService";
  public static final String TOKEN_REFRESH_EVENT = "messaging-token-refresh";

  // I commented the below codes
  // @Override  
  // public void onTokenRefresh() {
  //   Log.d(TAG, "onTokenRefresh event received");

  //   // Build an Intent to pass the token to the RN Application
  //   Intent tokenRefreshEvent = new Intent(TOKEN_REFRESH_EVENT);

  //   // Broadcast it so it is only available to the RN Application
  //   LocalBroadcastManager.getInstance(this).sendBroadcast(tokenRefreshEvent);
  // }
}

(2) 我在 RNFirebasePerformance.java 中所做的更改

...
.
.
  //somewhere in the file I searched for incrementCounter and commented it

  @ReactMethod
  public void incrementCounter(String identifier, String event) {
    // getOrCreateTrace(identifier).incrementCounter(event);  //I commented this line
  }

.
.
...

所以,我在上面进行了这些更改,之后它对我有用....就像一个魅力!

注意手动编辑 node-modules 文件并不专业,因此更推荐 fork 这个。

B-):如果对您不起作用,请随时问我任何问题

【讨论】:

    【解决方案2】:

    您使用的项目正在使用 react-native-fcm npm 模块。 FirebaseInstanceIdService 类是一个废弃的类,已被删除。 为了生成 fcm 的令牌,您可以使用类 MessagingInstance.java 中的函数 onNewToken 并删除项目中对类 FirebaseInstanceIdService 的任何引用。 onNewToken 方法代码:-

    @Override
        public void onNewToken(String s) {
            super.onNewToken(s);
            Log.d("NEW_TOKEN", s);
        }
    

    请按照以下链接中的说明来克服您的错误:-

    FirebaseInstanceIdService is deprecated.

    编辑:- 如果您没有在项目中使用推送通知,您可以从项目中删除整个 react-native-fcm 模块。

    【讨论】:

    • 我在 react-native-firebase 中遇到同样的问题,同样的错误,对于 RNFirebaseInstanceIdService.java 和 RNFirebasePerfomance.java
    • 您的 AndroidManifest.xml 文件是否包含此代码... ``` ```如果确实删除它
    • 最好是运行命令 npm i react-native-firebase@latest 来安装最新的 react-native-firebase 包以摆脱所有已弃用的文件。
    • 我尝试安装最新版本,但我又遇到了其他错误...最后我解决了我的问题,主要是我之前所说的 react-native-firebase 通过手动更改节点内部-模块
    猜你喜欢
    • 2020-02-23
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2022-07-30
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多