davips davips - 1 month ago 28
Android Question

Class Extending FirebaseMessagingService not working inside Android Library

I created an Android Library to implement FCM to use in my internal projects, where I implement a class to extend FirebaseMessagingService (

.services.MyFirebaseMessagingService
) and another one to extend FirebaseInstanceIdService (
.services.MyFirebaseInstanceIDService
).

I defined this in my AndroidManifest.xml inside the library as below:


<!-- [START firebase_service] -->
<service
android:name=".services.MyFirebaseMessagingService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_service] -->
<!-- [START firebase_iid_service] -->
<service android:name=".services.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->




When I included all the library source code as a module in my App, everything worked well, but when I included only the AAR generated, I received this error when I run my application:

java.lang.RuntimeException: Unable to instantiate service com.example.lib.services.MyFirebaseMessagingService: java.lang.ClassNotFoundException: com.example.lib.services.MyFirebaseMessagingService
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2380)
at android.app.ActivityThread.access$1600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4905)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.example.lib.services.MyFirebaseMessagingService
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2377)
at android.app.ActivityThread.access$1600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4905)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)


The service com.example.lib.services.MyFirebaseInstanceIDService works correctly.

What I need to do with AAR generated library to work?

Answer

For FCM follow this step :
1 - in main Build.gradle (not in app folder) add this line :

buildscript {
// ...
dependencies {
    // ...
    classpath 'com.google.gms:google-services:3.0.0'
}


2- in app/build.gradle add this line :

 apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:9.8.0'
  compile 'com.google.firebase:firebase-messaging:9.8.0'

  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'


Note : "Android Support Repository" in you SDk most be updated . Hope this help

Comments