OneSignal Android Studio Unknown error getting FCM Token

Viewed 2445

i have a problem. I cannot use OneSignal. I don't know why. I googled the error but nothing helped.

Currently i use Android Studio on Version: 4.2 The newest OneSignal Version: 4.3.4 And also the newest Firebase Version. I created a new Firebase Project and OneSignal Project.

This is the Error:

2021-05-25 13:06:37.370 3225-4181/de.kggaming.application E/OneSignal: Unknown error getting FCM Token
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;
        at com.onesignal.PushRegistratorFCM.getToken(PushRegistratorFCM.java:60)
        at com.onesignal.PushRegistratorAbstractGoogle.attemptRegistration(PushRegistratorAbstractGoogle.java:97)
        at com.onesignal.PushRegistratorAbstractGoogle.access$100(PushRegistratorAbstractGoogle.java:37)
        at com.onesignal.PushRegistratorAbstractGoogle$1.run(PushRegistratorAbstractGoogle.java:84)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceId" on path: DexPathList[[dex file "/data/data/de.kggaming.application/code_cache/.overlay/base.apk/classes3.dex", zip file "/data/app/~~37wP-zifceloViDoIaZiLQ==/de.kggaming.application-MT0rnPML9HHvlPgijekxXw==/base.apk"],nativeLibraryDirectories=[/data/app/~~37wP-zifceloViDoIaZiLQ==/de.kggaming.application-MT0rnPML9HHvlPgijekxXw==/lib/arm64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.onesignal.PushRegistratorFCM.getToken(PushRegistratorFCM.java:60) 
        at com.onesignal.PushRegistratorAbstractGoogle.attemptRegistration(PushRegistratorAbstractGoogle.java:97) 
        at com.onesignal.PushRegistratorAbstractGoogle.access$100(PushRegistratorAbstractGoogle.java:37) 
        at com.onesignal.PushRegistratorAbstractGoogle$1.run(PushRegistratorAbstractGoogle.java:84) 
        at java.lang.Thread.run(Thread.java:923) 

This is my build.gradle (Project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.0"
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And this my App build.gradle:

plugins {
    id 'com.onesignal.androidsdk.onesignal-gradle-plugin'
    id 'com.android.application'
    id 'com.google.gms.google-services'

}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "de.kggaming.application"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation platform('com.google.firebase:firebase-bom:28.0.1')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
}

Here the application class:

package de.kggaming.application;

import android.app.Application;

import com.onesignal.OneSignal;

public class ApplicationClass extends Application {
    private static final String ONESIGNAL_APP_ID = "XXXXXXXXXXXXXXXXXXXXXXXX";
    @Override
    public void onCreate() {
        super.onCreate();
        OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.DEBUG);

        // OneSignal Initialization
        OneSignal.initWithContext(this);
        OneSignal.setAppId(ONESIGNAL_APP_ID);
    }
}

The app don't have any features. Just a empty activity and this onesignal implementation.

Can you help me to get this thing work?

Thank you!

Best regards,

Gismo

2 Answers

Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId

faced a similar issue in my flutter project when trying to initialize the hypertrack_plugin which depends on firebase SDK.

enter image description here

After two day of search fixed with adding this implementation in android/app/build.gradle

hope this solution might solve your case too.

dependencies {
  implementation "com.google.firebase:firebase-iid"
}

found the solution from this Link

The class com.google.firebase.iid.FirebaseInstanceId has been deprecated and removed from the latest versions of the aar:

com.google.firebase.firebase-iid

See link.

The OneSignal version that you have in your project expects version 20.x.x of the aar file which still has that class. However, if you include v20.x.x of that aar file you will start getting duplicate class errors with the firebase-messaging:22.0.0 library that you are using.

So, the only solution that I can propose is to update the OneSignal SDK that you are using to the latest version.

Related