Unity-Android-App Crashes when Run First Time but From Second Time it Works Fine

avatar
209    5 months ago

I have implemented Firebase Analytics and Firebase Messaging after download the plugin from Google we site and successfully running the Resolvers. The problem is after build when App is Run FIRST TIME it crashes with the following error but from 2nd time onwards the app does not crash Here is the log

FATAL EXCEPTION: main
    Process: com.lemontart.flying.robot.car.transformation, PID: 27861
    java.lang.RuntimeException: Unable to instantiate service com.google.firebase.messaging.cpp.ListenerService: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.messaging.cpp.ListenerService" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk"],nativeLibraryDirectories=[/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/lib/arm, /data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3654)
        at android.app.ActivityThread.access$1500(ActivityThread.java:207)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.messaging.cpp.ListenerService" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk"],nativeLibraryDirectories=[/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/lib/arm, /data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:169)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:103)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3649)
        at android.app.ActivityThread.access$1500(ActivityThread.java:207) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6863) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/messaging/FirebaseMessagingService;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
                ... 11 more
     Caused by: java.lang.ClassNotFoundException: com.google.firebase.messaging.FirebaseMessagingService
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 14 more
     Caused by: java.lang.IllegalAccessError: Class com.google.firebase.iid.zzb extended by class com.google.firebase.messaging.FirebaseMessagingService is inaccessible (declaration of 'com.google.firebase.messaging.FirebaseMessagingService' appears in base.apk)
                ... 14 more
2019-05-17 12:55:18.960 27861-27861/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.lemontart.flying.robot.car.transformation, PID: 27861
    java.lang.Error: FATAL EXCEPTION [main]
    Unity version     : 2018.3.14f1
    Device model      : OnePlus ONEPLUS A5000
    Device fingerprint: OnePlus/OnePlus5/OnePlus5:9/PKQ1.180716.001/1904191514:user/release-keys

    Caused by: java.lang.RuntimeException: Unable to instantiate service com.google.firebase.messaging.cpp.ListenerService: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.messaging.cpp.ListenerService" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk"],nativeLibraryDirectories=[/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/lib/arm, /data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3654)
        at android.app.ActivityThread.access$1500(ActivityThread.java:207)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.messaging.cpp.ListenerService" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk"],nativeLibraryDirectories=[/data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/lib/arm, /data/app/com.lemontart.flying.robot.car.transformation-EVwlnR7Mxf4TO-7_HvK44w==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:169)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:103)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3649)
        at android.app.ActivityThread.access$1500(ActivityThread.java:207) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6863) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/messaging/FirebaseMessagingService;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)

I an see there are all the jars and aars present it the project, still I am not able to figure our the problem. Here is my gradle file

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
**BUILD_SCRIPT_DEPS**}
}

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

// Android Resolver Repos Start
allprojects {
    repositories {
        maven {
            url "https://maven.google.com"
        }
        maven {
            url "https://maven.google.com/" // Assets/Consoliads/Editor/ConsoliAdsDependencies.xml:7
        }
        maven {
            url "file:////Volumes/JawadData/Unity%20Projects/Consoli%204.6.2%20Unity%202018/Assets/Firebase/m2repository" // Assets/Firebase/Editor/AnalyticsDependencies.xml:22, Assets/Firebase/Editor/AppDependencies.xml:22
        }
        mavenLocal()
        jcenter()
        mavenCentral()
    }
}
// Android Resolver Repos End
apply plugin: 'com.android.application'
**APPLY_PLUGINS**

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:multidex:1.0.1'

**DEPS**}

android {
    compileSdkVersion **APIVERSION**
    buildToolsVersion '**BUILDTOOLS**'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        multiDexEnabled true
        minSdkVersion **MINSDKVERSION**
        targetSdkVersion **TARGETSDKVERSION**
        applicationId '**APPLICATIONID**'
        ndk {
            abiFilters **ABIFILTERS**
        }
        versionCode **VERSIONCODE**
        versionName '**VERSIONNAME**'
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**]
    }**SIGN**

    buildTypes {
        debug {
            minifyEnabled **MINIFY_DEBUG**
            useProguard **PROGUARD_DEBUG**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
            jniDebuggable true
        }
        release {
            minifyEnabled **MINIFY_RELEASE**
            useProguard **PROGUARD_RELEASE**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
        }
    }**PACKAGING_OPTIONS****SPLITS**
**BUILT_APK_LOCATION**
    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}**SPLITS_VERSION_CODE****REPOSITORIES****SOURCE_BUILD_SETUP**

I need to fix this issue, please help it will be highly appreciated. Thanks

Answers { 1 }
avatar
5 months ago

Try adding this line to your gradle:

implementation 'com.google.firebase:firebase-messaging:+'

I think it should to the end of the document, if not try in other places like this:

    dependencies {
        implementation 'com.google.firebase:firebase-messaging:+'
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:multidex:1.0.1'

**DEPS**}

Or here:

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        implementation 'com.google.firebase:firebase-messaging:+'
**BUILD_SCRIPT_DEPS**}
}