Java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging

I am developing a sample GCM application using http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/#comment-103785 .

I can not register the gcm server. I get the following error. I looked through it. But I could not find a solution, and all the solutions concerned jar files, I made all the changes, but I could not take it.

Error

FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging
    com.sanjeev.integrationapp.RegisterActivity.registerGCM(RegisterActivity.java:80)
    com.sanjeev.integrationapp.RegisterActivity$1.onClick(RegisterActivity.java:47)

    at android.view.View.performClick(View.java:4084)

    at android.view.View$PerformClick.run(View.java:16966)

    at android.os.Handler.handleCallback(Handler.java:615)

    at android.os.Handler.dispatchMessage(Handler.java:92)

    at android.os.Looper.loop(Looper.java:137)

    at android.app.ActivityThread.main(ActivityThread.java:4745)

    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:786)

    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    at dalvik.system.NativeStart.main(Native Method)

And my class code follows

public class RegisterActivity extends Activity {

    Button btnGCMRegister;
    Button btnAppShare;
    GoogleCloudMessaging gcm;
    Context context;
    String regId;

    public static final String REG_ID = "regId";
    private static final String APP_VERSION = "appVersion";

    static final String TAG = "Register Activity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    context = getApplicationContext();

    btnGCMRegister = (Button) findViewById(R.id.btnGCMRegister);
    btnGCMRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (TextUtils.isEmpty(regId)) {
                regId = registerGCM();
                Log.d("RegisterActivity", "GCM RegId: " + regId);
            } else {
        Toast.makeText(getApplicationContext(),
            "Already Registered with GCM Server!",
                        Toast.LENGTH_LONG).show();
            }
        }
    });

    btnAppShare = (Button) findViewById(R.id.btnAppShare);
    btnAppShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (TextUtils.isEmpty(regId)) {
        Toast.makeText(getApplicationContext(), "RegId is empty!",
                        Toast.LENGTH_LONG).show();
            } else {
                Intent i = new Intent(getApplicationContext(),
                        MainActivity.class);
                i.putExtra("regId", regId);
                Log.d("RegisterActivity",
            "onClick of Share: Before starting main activity.");
                startActivity(i);
                finish();
    Log.d("RegisterActivity", "onClick of Share: After finish.");
            }
        }
    });
}

public String registerGCM() {

 // below line I am getting error 

gcm = GoogleCloudMessaging.getInstance(this);




    regId = getRegistrationId(context);

    if (TextUtils.isEmpty(regId)) {

        registerInBackground();

    Log.d("RegisterActivity",
    "registerGCM - successfully registered with GCM server - regId: "
                        + regId);
    } else {
        Toast.makeText(getApplicationContext(),
                "RegId already available. RegId: " + regId,
                Toast.LENGTH_LONG).show();
    }
    return regId;
}

@SuppressLint("NewApi")
private String getRegistrationId(Context context) {
    final SharedPreferences prefs = getSharedPreferences(
            MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
    String registrationId = prefs.getString(REG_ID, "");
    if (registrationId.isEmpty()) {
        Log.i(TAG, "Registration not found.");
        return "";
    }
    int registeredVersion = prefs.getInt(APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion) {
        Log.i(TAG, "App version changed.");
        return "";
    }
    return registrationId;
}

private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
Log.d("RegisterActivity","I never expected this! Going down, going down!" + e);
        throw new RuntimeException(e);
    }
}

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                gcm = GoogleCloudMessaging.getInstance(context);
                }
        regId = gcm.register(Config.GOOGLE_PROJECT_ID);
        Log.d("RegisterActivity", "registerInBackground - regId: "
                        + regId);
        msg = "Device registered, registration ID=" + regId;

                storeRegistrationId(context, regId);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                Log.d("RegisterActivity", "Error: " + msg);
            }
            Log.d("RegisterActivity", "AsyncTask completed: " + msg);
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
             Toast.makeText(getApplicationContext(),
                    "Registered with GCM Server." + msg, 

                   Toast.LENGTH_LONG)
                    .show();
        }
    }.execute(null, null, null);
}

private void storeRegistrationId(Context context, String regId) {
    final SharedPreferences prefs = getSharedPreferences(
            MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
    int appVersion = getAppVersion(context);
    Log.i(TAG, "Saving regId on app version " + appVersion);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(REG_ID, regId);
    editor.putInt(APP_VERSION, appVersion);
    editor.commit();
} 

}

+4
source share
1 answer

google_play_services_lib , google_play_services_lib google-play-services.jar, GCM.

google-play-services.jar GCM , google_play_services_lib GCM Android. , , GCM google_play_services_lib.

, :
1. libs, libs, lib.
2. google-play-services.jar libs.
3. google-play-services.jar > .

Android, , .

: jar - sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs

+8

Source: https://habr.com/ru/post/1540674/


All Articles