How to resolve Service not registered exception when using firebase in android app?

Environment

The android project’s build.gradle

buildscript {
    
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.gms:google-services:4.3.3'
        classpath 'io.fabric.tools:gradle:1.31.2'  // Crashlytics plugin
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.0.0-beta04'
    }
}

The android app module’s build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

...
dependencies {
  ...
	implementation 'com.google.firebase:firebase-analytics:17.3.0'
	implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta04'
	implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}

Problem

Fatal Exception: java.lang.IllegalArgumentException: Service not registered: [email protected]
       at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1832)
       at android.app.ContextImpl.unbindService(ContextImpl.java:1880)
       at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
       at com.google.android.gms.common.stats.a.a()
       at com.google.firebase.iid.z0.b(:50)
       at com.google.firebase.iid.z0.a(:78)
       at com.google.firebase.iid.c1.handleMessage(:2)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at c.a.a.a.b.c.e.dispatchMessage()
       at android.os.Looper.loop(Looper.java:219)
       at android.app.ActivityThread.main(ActivityThread.java:8347)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)

Solution

Solution 1: Override the code by your self

Check the source code of firebase and you would find that it is caused by com.google.android.gms:play-services-basement:17.0.0.

The com.google.android.gms.common.stats.ConnectionTracker#unbindService() in the library tries to unbind the unregistered Service, the latest ConnectionTracker in 17.2.1 adds an unbindServiceSafe(), which tries-catch the unbindService , But the strange thing is that other classes of Firebase have not called this method.

Firebase uses FirebaseInitProvider to obtain Context without intrusion, and this Context is an instance of Application, so we can rewrite Application#unbindService(), and the final unbindService() called will be captured here:

    override fun unbindService(conn: ServiceConnection) {
        try {
            super.unbindService(conn)
        } catch (e: IllegalArgumentException) {
            // do something, ignore or report...
        }
    }

Solution 2: Upgrade the firebase crashlytics library version

You can upgrade the firebase SDK versions to get the latest update of the SDK:

In the project’s build.gradle

buildscript {
    
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.gms:google-services:4.3.3'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
        classpath 'io.fabric.tools:gradle:1.31.2'
    }
}

And in the app module’s build.gradle:

dependencies {
    implementation 'com.google.firebase:firebase-analytics:17.4.4'
    implementation 'com.google.firebase:firebase-crashlytics:17.1.1'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}