46

I get the following exception when trying to use the Google Play Services V2 library after following the official tutorial.

java.lang.NoClassDefFoundError: com.google.android.gms.R$string
at com.google.android.gms.common.GooglePlayServicesUtil.b(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.a(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.getErrorDialog(Unknown Source)

Where did I go wrong?

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
  • 1
    Did you provide Google android apiKey to your application? Please, provide info about your Manifest.xml and snippets of code where you got Exception – Maxim Shoustin Dec 03 '12 at 20:25
  • I didn't my ID to the application, yet. IMO failing with an unrelated error message for such common errors is usually not Google's style. – Ben Weiss Dec 03 '12 at 20:27
  • witch jar do you use for `GooglePlayServicesUtil`? – Maxim Shoustin Dec 03 '12 at 20:29
  • The V2 which was released today. – Ben Weiss Dec 03 '12 at 20:34
  • $Keyboardsurfer if you use Eclipse be sure that your project configured properly. Go to Project Properties -> Java Build path -> Order And Export tab -> set checkbox with your jar. After clean project and refresh to generate new gen. `NoClassDefFoundError` exception might be the reason of that – Maxim Shoustin Dec 03 '12 at 20:37
  • I'm building with maven and It's going to be _a lot of fun_ with apklibs again. – Ben Weiss Dec 03 '12 at 20:43

8 Answers8

34

I also have the same issues once ,I followed the step properly and able to solve this issue

First (Set up your project from Google Developer console) Go to API Console - Google Code

Create A project As shown in the images enter image description here

Click Create then you will Ask to add a project name as shown

enter image description here

once you create your project its time to select what service we need to use,In this case we need android v2 map so select the Google Maps Android API v2 from Service As shown, enter image description here

Now go to Api Access and create your OAuth 2.0 .By provide your package name and SHA1 fingerprint in the corresponding fields. enter image description here

once you finish with OAuth 2.0 we are ready to use your API Key enter image description here

Now Create An Android project with the same package name used while creating the OAuth 2.0. and Check whether you have the google play service in Android SDK Manager otherwise install google play service. enter image description here

After installing Google playservice you will find a Google play library in Your Android YourSdkpath\extras\google\google_play_services.Import that project to your workspace and give it as the refrence library to your project enter image description here

enter image description here

enter image description here

After that put the corresponding java and xml files to your project.

MainActivity.java

package yourpackage;//Package name used while creating the Api key


import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status==ConnectionResult.SUCCESS)
    {
        SupportMapFragment supportMapFragment = (SupportMapFragment) 
                getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting a reference to the map
        googleMap = supportMapFragment.getMap();
    }
    else{

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="wrap_content"
   android:layout_height="match_parent" />

AndroidManifest.xml

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
   <permission
    android:name="yourpackage.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

<uses-permission android:name="yourpackage.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="yourpackage.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>
      <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YourAPIkey"/>

Hope it will help you

Ramz
  • 7,116
  • 6
  • 63
  • 88
  • 2
    Add one more meta data tag To avoid error java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the element: – Ramz Dec 10 '13 at 07:21
32

Version 2 includes resources and must be imported as a library project. See the instructions at http://developer.android.com/google/play-services/setup.html for how to do that in various IDEs.

The resources are for GooglePlayServicesUtil.getErrorDialog(), which directs the user to install, update, enable, etc. Google Play services if it isn't available on the device.

jham
  • 396
  • 3
  • 4
  • 8
    Not exactly "various IDEs" - it covers Eclipse and the CLI, and that's it. – skaffman Dec 06 '12 at 08:09
  • If you've included Drive API using Google Plugin, don't forget to delete reference to Google Play Services in the libs folder. Otherwise you'll get duplicate class exception after adding Google Play Services as android library. – petrsyn Feb 03 '13 at 22:33
  • 1
    a direct link to the page that resolves the problem: http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject – averasko Jul 31 '13 at 19:19
  • I was importing this as a `jar` file under the /libs folder (copy paste basically) but what was probably needed is to import this as a library project in order for the R file of the `play-services` to be generated I guess. Thanks for this reply. – George Daramouskas May 24 '15 at 20:21
11

For Android Studio:

1) Open Module Settings:

2) Add (Import) module google_play_service_lib from SDK

Picture

3) Module google_play_service_lib check as Library

4) Add library google_play_service.jar from SDK

5) Finish

Picture

Community
  • 1
  • 1
Denis Dolgikh
  • 151
  • 1
  • 5
  • 1
    Following this finally solved the issues I was having in Android Studio. I was making the mistake of either only including the library or creating a module from the wrong directory under google_play_services. – Landstander Oct 03 '13 at 15:38
  • 1
    What about the build.gradle ? Coz, it says that, "Gradle: error: package com.google.android.gms.common does not exist" – alicanbatur Nov 14 '13 at 17:36
  • Not working. Followed the steps., But getting Runtime error. 'java.lang.NoClassDefFoundError: com.google.android.gms.R$string'. Please help. I am using Play services 8.4.0 – dreamdeveloper Jan 13 '16 at 06:20
2

If you already upgraded SDK and get such error remember to:

  • use new version of google-play-services.jar if you have copy this file in your eclipse project
  • set target=android-19 in project.properties
  • for newer API (I use API 19) you may need to add following code

AndroidManifest.xml

<application
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>

I also use ourSdkpath\extras\google\google_play_services as Eclipse library imported as described above.

Damian
  • 437
  • 5
  • 11
1

I solved this error by checking "Copy projects into workspace" while importing the google play services lib.
More info here: http://developer.android.com/google/play-services/setup.html

Umberto
  • 2,011
  • 1
  • 23
  • 27
1

if you are using

compile 'com.google.android.gms:play-services:9.0.2'

change it to individual ones such as

compile 'com.google.android.gms:play-services-location:9.0.2'

also add compile 'com.android.support:multidex:1.0.1' if you have multiDexEnabled=true

chrki
  • 6,143
  • 6
  • 35
  • 55
0

I have the same issue with Android Studio when build project without Gradle. It might be problem with the name of package in AndroidManifest.xml in GooglePlayService project. It should be package="com.google.android.gms".

Issue appears when I add Library Project as "New Module" instead "Import Module".

enter image description here

When you choose New Module, Android Studio shows dialog to specify "Module name" and "Package name" and by default "Package name" looks like "com.example.MODULE_NAME_YOU_SPECIFED" which is wrong. It's very easy to miss it, because project builds with success.

enter image description here

When you add Library Project by "Import Module" everything should be OK, because Android Studio just add project without doing any changes with source code of Library Project.

klimat
  • 24,711
  • 7
  • 63
  • 70
  • Not working. Followed the steps., But getting Runtime error. 'java.lang.NoClassDefFoundError: com.google.android.gms.R$string'. Please help. I am using Play services 8.4.0 – dreamdeveloper Jan 13 '16 at 06:20
0

I also face same problem. If you run your application in emulator then may be face this error because in most of emulator there is no google playService (Play store app) installed.

So try to run your app in android device which is playService app is installed.

Solution :- add error part code in try...catch and then try it will work in emulator also.

Note :- Google Messaging service require palyservice. otherwise throw error SERVICE_NOT_AVAILABLE

Mansukh Ahir
  • 3,373
  • 5
  • 40
  • 66