2

I am trying to implement the android push notifications for an app. I have followed the steps here: link

However, when I try and run the app on my device, I receive the error that the GcmBroadcastReceiver is not found in my apk... although it is there.

I have the google-play-services-lib in my workspace, as a library. I have my project linked to this library.

Below is the error I am getting when the crash occurs.

04-15 20:59:28.722: E/AndroidRuntime(10970): FATAL EXCEPTION: main
04-15 20:59:28.722: E/AndroidRuntime(10970): java.lang.RuntimeException: Unable to instantiate receiver sg.ignitedigital.AllAccess.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "sg.ignitedigital.AllAccess.GcmBroadcastReceiver" on path: /data/app/sg.ignitedigital.AllAccess-1.apk
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2493)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.access$1600(ActivityThread.java:159)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1392)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.os.Looper.loop(Looper.java:137)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.reflect.Method.invoke(Method.java:525)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at dalvik.system.NativeStart.main(Native Method)
04-15 20:59:28.722: E/AndroidRuntime(10970): Caused by: java.lang.ClassNotFoundException: Didn't find class "sg.ignitedigital.AllAccess.GcmBroadcastReceiver" on path: /data/app/sg.ignitedigital.AllAccess-1.apk
04-15 20:59:28.722: E/AndroidRuntime(10970):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-15 20:59:28.722: E/AndroidRuntime(10970):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2488)
04-15 20:59:28.722: E/AndroidRuntime(10970):    ... 10 more

Here is how I added the service and receiver in the manifest file:

    <permission android:name="sg.ignitedigital.AllAccess.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="sg.ignitedigital.AllAccess.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <receiver android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="sg.ignitedigital.AllAccess" />
        </intent-filter>
    </receiver>        

    <service android:name="sg.ignitedigital.AllAccess.GcmIntentService" />

I have tried fixing this so many times. Why is it not finding my GcmBroadcastReceiver file if it is there in the package... .

Fixed:

Eventually I found the problem. In my case, it was that I had a library project used by my project, that had a version of the support v4 jar and my project had a different version of the support v4 jar. Because those did not match, I was getting this error. Once I synced the same version of the support library, all worked well.

George Mincila
  • 529
  • 5
  • 17

1 Answers1

2

I have also been facing the same problem for the whole morning. I have checked around both of the StackOverflow and Google itself.

The problem is when you extends WakefulBroadcastReceiver to create GcmBroadcastReceiver, the v4 support library has to be exported for initialization of the class. Otherwise, you will have the ClassNotFoundException issue on runtime ,in my case, because the support library is in the libs folder and not exactly exported in Java Build Path.

So, the thorough steps are <br />
1. get the proper android-support-v4-libs from your android base folder (android-sdk-XXX/extras/android/support/v4/android-support-v4.jar). <br />
2. put under **libs** folder of your main project. (override other android-support-v4-libs.jar if you have other project referencing from your main project). <br />
3. Right Click on android-support-v4-libs.jar > Build > Add to Build Path.

Other points to check on AndroidManifest.xml are

<receiver
 android:name=".receiver.GcmBroadcastReceiver"
 android:permission="com.google.android.c2dm.permission.SEND" 
 android:exported="true"
 android:enabled="true"
 >
 <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
      <category android:name="yourApplicationPackageName" />
 </intent-filter>
</receiver>

<service
    android:name=".GcmIntentService"
    />
Aung Pyae
  • 1,590
  • 2
  • 16
  • 25
  • 2
    Actually the problem, in my case, was that I had a library project used by my project, that had a version of the support v4 jar and my project had a different version of the support v4 jar. Because those did not match, I was getting this error. Once I synced the same version of the support library, all worked well. – George Mincila Jul 16 '14 at 06:17
  • Yes. I also had that experience. But, even if your v4 libs are syncing, your main project is not exporting the v4 libs it is being using, you will also have that issue. – Aung Pyae Jul 18 '14 at 10:31