GCM : java.lang.noclassdeffounderror: com.google.a

2019-02-03 02:18发布

问题:

I am trying to register a device onto GCM using the following code:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    GCMRegistrar.checkDevice(this);
    // the following function can be removed when deploying the app
    GCMRegistrar.checkManifest(this);

    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
      GCMRegistrar.register(this, SENDER_ID);
    } else {
      Log.v(TAG, "Already registered");
    } 

I have imported import com.google.android.gcm.GCMRegistrar;
and also added GCM.jar to my buildpath in Eclipse which i got thru the sdk download

Please help !

回答1:

In the updated ADT(Above ADT 17) you have to put jar files inside libs folder and not lib. So make sure that you put gcm.jar file inside libs and not lib folder.



回答2:

Just go to project properties -> java build path -> "order and export " now giv a tick on the jar file "GCM.jar" that you have added



回答3:

My gcm.jar was already in libs, so it wasn't a folder naming issue for me..

I had to go to Right Click > Build Path > Configure Build path... for the project in question and I removed the following:

  • Any lingering files outside of a folder/bundle
  • Android Private Libraries
  • Any bundles with the word 'reference' in them (you may not have one)

You might get a lot of "errors" in your project so restart Eclipse and let it clean. Once it cleans you'll have no errors. Run your project and viola.

Thanks to DLew for this answer.



回答4:

Make a folder libs in your project folder and put the gcm.jar in it. Then add it to your class path using add external jars in configure build path option.



回答5:

You have to put gcm.jar file inside libs, then go to project Properties > Java Build Path > Libraries and add it to your path using Add External JARs..., after in Order and Export you check gcm.jar and press OK. That should work



回答6:

Make sure that the gcm jar file is in the right libs folder and added into the java build path at your project properties.



回答7:

I had same problem, after update SDK in my project. it helped for me: I just "add support library" in "Android tools". Hope this helps anyone else.



回答8:

Me also got same problem, after adding android-support-v4.jar and gcm.jar into my libs folder no issues found its work like a charm

Thanks



回答9:

The SENDER_ID field should be the project ID from when you created your google api project. Read the beginning of this: http://developer.android.com/guide/google/gcm/gs.html#libs

TAG should also be a string -> ""tag Name"



回答10:

Hello You are using old GCM implementations use new implementations available on https://developers.google.com/cloud-messaging/android/start