Search code examples
javaandroidnotificationsgoogle-cloud-messagingclassnotfoundexception

android - classnotfoundexception - mysterious


I'm trying to understand how GCM works. To do so, I copy/paste the code that http://developer.android.com/ proposes in the section "implementing a GCM client".

Sending a message from a server works, but when my client app receives the message, it crashes, telling me there is a :

E/AndroidRuntime(5722): java.lang.RuntimeException: Unable to instantiate receiver
com.test.testnotification3.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't
find class "com.test.testnotification3.GcmBroadcastReceiver" on path:
/data/app/com.test.testnotification3-2.apk

I searched for similar problems on the Internet, lots of people seem to get this error because of a problem in their manifest (wrong package name in the manifest). But I checked my manifest and it seems to be OK:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testnotification3"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.test.testnotification3.permission.C2D_MESSAGE" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <receiver
        android:name="com.test.testnotification3.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="com.test.testnotification3" />
        </intent-filter>
    </receiver>

    <service android:name="com.test.testnotification3.GcmIntentService" />

    <activity
        android:name="com.test.testnotification3.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

If you have any idea... Thanks a lot!

Edit : Here is my GcmBroadcastReceiver :

package core;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
    }

}

Of course, since the package name changed, I replaced "com.test.testnotification3" by "core" for the receiver.


Solution

  • You have to put your Reciever in another package. The system won't be able to instantiate it if it's on the main package.