java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId on pusher api

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId on pusher api

我想问一下如何解决这个问题,因为我尝试研究它,但是 none 我在网上找到的答案对我有用。我仍在注册设备以在推送器网站上使用,当我复制此代码时,错误弹出。

PushNotifications.start(getApplicationContext(), "mykey"); PushNotifications.addDeviceInterest("hello");

这是我的项目Gradle

buildscript {
repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath "com.android.tools.build:gradle:7.0.3"
    classpath 'com.google.gms:google-services:4.3.10'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
} }

还有我的应用程序 Gradle

dependencies {

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'com.google.firebase:firebase-analytics:20.0.0'
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.google.firebase:firebase-database:20.0.2'
implementation 'com.google.firebase:firebase-storage:20.0.0'
implementation 'com.google.firebase:firebase-messaging:23.0.0'
implementation 'com.google.firebase:firebase-core:17.4.4'
implementation 'com.pusher:push-notifications-android:1.6.2'
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation "androidx.cardview:cardview:1.0.0"
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'} apply plugin: 'com.google.gms.google-services'

最后这是我的 java class

public class Sprinkler extends AppCompatActivity {

Button On;
Button Off;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sprinkler);

    PushNotifications.start(getApplicationContext(), "mykey");
    PushNotifications.addDeviceInterest("hello");

    On = (Button) findViewById(R.id.switch1);
    Off = (Button) findViewById(R.id.switch2);
    On.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // getting the default FirebaseDatabase instance
            FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();

            // getting a DatabaseReference for the database root node
            DatabaseReference databaseReference = firebaseDatabase.getReference("Relay_Status");

            databaseReference.setValue(1);

            Toast.makeText(getApplicationContext(), "Sprinkler Turn On!", Toast.LENGTH_SHORT).show(); } }); }

这是错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fire_app, PID: 24128
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;
    at com.pusher.pushnotifications.PushNotificationsInstance.start(PushNotificationsInstance.kt:180)
    at com.pusher.pushnotifications.PushNotifications.start(PushNotifications.java:31)
    at com.example.fire_app.Sprinkler.onCreate(Sprinkler.java:27)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access0(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceId" on path: DexPathList[[zip file "/data/app/com.example.fire_app-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at com.pusher.pushnotifications.PushNotificationsInstance.start(PushNotificationsInstance.kt:180) 
    at com.pusher.pushnotifications.PushNotifications.start(PushNotifications.java:31) 
    at com.example.fire_app.Sprinkler.onCreate(Sprinkler.java:27) 
    at android.app.Activity.performCreate(Activity.java:5990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access0(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Suppressed: java.lang.ClassNotFoundException: com.google.firebase.iid.FirebaseInstanceId
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 17 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

由于您使用的是 22.0 以上的 firebase:firebase-messaging 版本,因此您需要确保将这些行也添加到您的应用中 gradle

implementation "com.google.firebase:firebase-iid:21.1.0"
implementation 'com.pusher:push-notifications-android:1.9.0'

// Add this line to the end of the file
apply plugin: 'com.google.gms.google-services'

Source.