【问题标题】:cannot find symbol import com.google.firebase.iid.FirebaseInstanceIdService error in ionic3在 ionic3 中找不到符号导入 com.google.firebase.iid.FirebaseInstanceIdService 错误
【发布时间】:2019-10-07 10:20:45
【问题描述】:

我不知道为什么每当我尝试创建应用程序的构建时都会收到此错误。我尝试了很多解决方案,但我无法解决这个问题。我不知道是否有任何插件导致问题或是否有任何问题我的代码或 firebase 或 firebase 连接有问题。我已删除节点模块并运行 npm install 并删除并重新添加了 android 平台,但我仍然收到此错误。

import com.google.firebase.iid.FirebaseInstanceIdService;
                              ^
  symbol:   class FirebaseInstanceIdService
  location: package com.google.firebase.iid
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePluginInstanceIDService.java:9: error: cannot find symbol
public class FirebasePluginInstanceIDService extends FirebaseInstanceIdService {
                                                     ^
  symbol: class FirebaseInstanceIdService
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePlugin.java:508: error: method getByteArray in class FirebaseRemoteConfig cannot be applied to given types;
                            : FirebaseRemoteConfig.getInstance().getByteArray(key, namespace);
                                                                ^
  required: String
  found: String,String
  reason: actual and formal argument lists differ in length
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePlugin.java:525: error: method getValue in class
FirebaseRemoteConfig cannot be applied to given types;
                            : FirebaseRemoteConfig.getInstance().getValue(key, namespace);
                                                                ^
  required: String
  found: String,String
  reason: actual and formal argument lists differ in length
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePlugin.java:579: error: no suitable method found
for setDefaults(Map<String,Object>,String)
                        FirebaseRemoteConfig.getInstance().setDefaults(defaultsToMap(defaults), namespace);
                                                          ^
    method FirebaseRemoteConfig.setDefaults(Map<String,Object>) is not applicable
      (actual and formal argument lists differ in length)
    method FirebaseRemoteConfig.setDefaults(int) is not applicable
      (actual and formal argument lists differ in length)
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePluginInstanceIDService.java:18: error: method does not override or implement a method from a supertype
    @Override
    ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\io\card\cordova\sdk\CardIOCordovaPlugin.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
6 errors
:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
27 actionable tasks: 1 executed, 26 up-to-date
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 54s
cmd: Command failed with exit code 1 Error output:
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePluginInstanceIDService.java:6: error: cannot find symbol
import com.google.firebase.iid.FirebaseInstanceIdService;
                              ^
  symbol:   class FirebaseInstanceIdService
  location: package com.google.firebase.iid
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePluginInstanceIDService.java:9: error: cannot find symbol
public class FirebasePluginInstanceIDService extends FirebaseInstanceIdService {
                                                     ^
  symbol: class FirebaseInstanceIdService
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePlugin.java:508: error: method getByteArray in class FirebaseRemoteConfig cannot be applied to given types;
                            : FirebaseRemoteConfig.getInstance().getByteArray(key, namespace);
                                                                ^
  required: String
  found: String,String
  reason: actual and formal argument lists differ in length
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePlugin.java:525: error: method getValue in class
FirebaseRemoteConfig cannot be applied to given types;
                            : FirebaseRemoteConfig.getInstance().getValue(key, namespace);
                                                                ^
  required: String
  found: String,String
  reason: actual and formal argument lists differ in length
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePlugin.java:579: error: no suitable method found
for setDefaults(Map<String,Object>,String)
                        FirebaseRemoteConfig.getInstance().setDefaults(defaultsToMap(defaults), namespace);
                                                          ^
    method FirebaseRemoteConfig.setDefaults(Map<String,Object>) is not applicable
      (actual and formal argument lists differ in length)
    method FirebaseRemoteConfig.setDefaults(int) is not applicable
      (actual and formal argument lists differ in length)
C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\org\apache\cordova\firebase\FirebasePluginInstanceIDService.java:18: error: method does not override or implement a method from a supertype
    @Override
    ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\user\Downloads\mosque\platforms\android\app\src\main\java\io\card\cordova\sdk\CardIOCordovaPlugin.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
6 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 54s
[ERROR] An error occurred while running subprocess cordova.

        cordova build android exited with exit code 1.

【问题讨论】:

  • 好的。这是一个gradle错误,很多人都面临这个问题。我将继续帮助您寻找替代方法。

标签: angular firebase ionic-framework ionic3


【解决方案1】:

好的。这是一个梯度错误。这是由于 4 月份的 firebase 更新,FirebaseInstanceIdService 已被弃用,很多人都面临这个问题。

Check the reference documentation for FirebaseInstanceIdService

此类已被弃用。

赞成在 FirebaseMessagingService 中覆盖 onNewToken。一次 已实施,则可以安全地删除此服务。

【讨论】:

  • 这就是我们导入 firebase.iid 的方式
  • 好的。 FirebaseInstanceIdService 已弃用
【解决方案2】:

这是由于 4 月 5 日的 Firebase 更新。在这里https://firebase.google.com/support/release-notes/android#update_-_april_02_2019 96,我找到了最后的工作版本并相应地调整了我的project.properties,如下所示:

cordova.system.library.4=com.google.firebase:firebase-core:16.0.8

cordova.system.library.5=com.google.firebase:firebase-messaging:17.5.0

cordova.system.library.6=com.google.firebase:firebase-config:16.4.1

cordova.system.library.7=com.google.firebase:firebase-perf:16.2.4

这里所有依赖项都锁定到特定版本(更新 - 2019 年 4 月 2 日),而不是 +,这将使您获得最新版本,从而中断构建过程。

现在我可以成功构建了!希望对您有所帮助!

project.properties 可以在 platforms/android/project.properties

上找到

【讨论】:

    【解决方案3】:

    是的,它已经贬值了。修复它很简单,只需移动代码...

    来自onTokenRefresh() 中的FirebaseInstanceIdService 子类

    到您的FirebaseMessagingService 子类中的onNewToken()

    折旧信息:https://firebase.google.com/support/release-notes/android#update_-_april_02_2019

    我建议对使用旧版本的 firebase 进行这个小代码更改。迁移到 AndroidX [https://developer.android.com/jetpack/androidx/migrate] 可能是个好主意。

    【讨论】:

    • onNewToken() 方法不存在...你确定吗?
    • @AriWaisberg 只需在 FirebaseMessagingService 中创建 onNewToken() 方法即可。
    • @AriWaisberg 函数定义如下-> 覆盖 public void onNewToken(String refreshedToken),我认为你缺少参数
    • @kaitlyn: 我已经迁移 AndroidX 但又出现同样的错误>
    【解决方案4】:

    经过多次测试,我的解决方案是通过 cordova-plugin-firebasex 更改 cordova-plugin-firebase。 https://github.com/dpa99c/cordova-plugin-firebase#migrating-from-cordova-plugin-firebase

    rm -Rf platforms/android
    cordova plugin rm cordova-plugin-firebase
    rm -Rf plugins/ node_modules/
    npm install
    cordova plugin add cordova-plugin-firebasex
    cordova platform add android
    

    我希望这会有所帮助。

    https://github.com/dpa99c/cordova-plugin-firebase#androidx

    此插件已迁移到使用 AndroidX (Jetpack),它是 Android 支持库的继承者。这是通过添加对 cordova-plugin-androidx 的依赖来实现的,它在 Cordova 项目的 Android 平台中启用 AndroidX。

    这是因为 2019 年 6 月 17 日的主要版本 Firebase 和 Play 服务库已迁移到 AndroidX。

    因此,如果您的项目包含任何依赖于旧版 Android 支持库的插件,您应该将 cordova-plugin-androidx-adapter 添加到您的项目中。此插件会将任何插件代码从 Android 支持库动态迁移到 AndroidX 等效项。

    【讨论】:

    • 这对我来说非常有效。我认为这是正确的解决方案。没有降级。没有库代码操作。
    • 你救了我的命,真的!与这些错误斗争了 2 个月我给你买啤酒)))
    • 这是一个更好的解决方案。谢谢!
    【解决方案5】:

    如果这个问题发生在React Native框架中。有两种解决方案:

    解决方案 1 (推荐)

    在以下文件中更改 firebase 版本: android\app\build.gradle

    ...
    //implementation 'com.google.firebase:firebase-core'
    //implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-core:16.0.8'
    implementation 'com.google.firebase:firebase-messaging:17.6.0'
    ...
    

    并在文件底部的android\gradle.properties 中添加

    firebaseCoreVersion=16.0.8
    firebaseMessagingVersion=17.6.0
    

    解决方案 2:

    更改\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\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 {
    
        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 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();
                        }
                    }
                }
            });
        }
    }
    

    【讨论】:

      【解决方案6】:

      由于支持 AndroidX 的主要 Firebase 版本而出现此问题

      解决方案:

      1. 移除 android 平台(我按照此步骤操作。但应该有我可能不知道的解决方法) - cordova platform rm android

      2. 移除旧的 firebase 插件 - cordova plugin rm cordova-plugin-firebase

      3. 添加新的 firebase 插件 - cordova plugin add cordova-plugin-firebasex

      4. 再次添加平台 - cordova platform add android

      现在您的构建将成功。

      【讨论】:

      • 但我没有使用 cordova-plugin-firebase 插件在这种情况下我该怎么办?
      • @Kapilsoni 我想你用的是 OTP Auth。
      【解决方案7】:

      使用

      cordova plugin add cordova-plugin-androidx
      cordova plugin add cordova-plugin-androidx-adapter
      

      然后使用

      cordova plugin add cordova-plugin-firebasex
      

      【讨论】:

        【解决方案8】:

        要解决这个问题...使用 firebase 消息传递来获取令牌。 原因:FirebaseInstanceId 已弃用,因此无法使用。

        下面是一个如何去做的例子。

        FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
                    @Override
                    public void onComplete(@NonNull @NotNull Task<String> task) {
                        if (task.isSuccessful()){
                            updateToken(task.getResult());
                        }
                    }
                });
        

        希望这对某人有所帮助

        【讨论】:

          猜你喜欢
          • 2021-12-21
          • 2021-08-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-14
          • 2021-12-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多