Ben Weiss
Ben Weiss

Reputation: 17922

NoClassDefFoundError at Google Play Services V2 library

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?

Upvotes: 46

Views: 42712

Answers (8)

Dennis Djan
Dennis Djan

Reputation: 9

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

Upvotes: 1

Mansukh Ahir
Mansukh Ahir

Reputation: 3543

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

Upvotes: 0

klimat
klimat

Reputation: 24991

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.

Upvotes: 0

Ramz
Ramz

Reputation: 7164

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

Upvotes: 34

Damian
Damian

Reputation: 550

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.

Upvotes: 2

Denis Dolgikh
Denis Dolgikh

Reputation: 161

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

Upvotes: 11

Umberto
Umberto

Reputation: 2060

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

Upvotes: 1

jham
jham

Reputation: 496

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.

Upvotes: 32

Related Questions