20

Im working on simple map app, I added google-play-services.jar and its reference to gradle

dependencies {
    compile files('libs/android-support-v4.jar', 'libs/google-play-services.jar')
}

The main activity:

setContentView(R.layout.activity_mapui);

GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

layout file

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

But the app crashes on the device

E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)

I tried several ways, same issue, I just want to add google services to Android Studio 0.3 with Gradle 0.6+

A7madev
  • 728
  • 3
  • 10
  • 18

9 Answers9

34

Add this line in the dependencies section of your build.gradle file:

compile 'com.google.android.gms:play-services:3.2.+'

You don't need to add a .jar to your libs folder.

Edit

As Varun pointed out, you also need to update the SDK to contain the most recent version of the play services.

You can do this by opening up the Android SDK Manager (under the Window tab) from Eclipse+ADT, checking the box next to Google Play services in the Extras folder, and clicking install.

Matt Logan
  • 5,886
  • 5
  • 31
  • 48
  • 3
    To add. Also update the SDK to contain the needed version of the play-services, and have the support/google repository installed from the extras section of the SDK manager. – Varun Nov 01 '13 at 17:55
  • 8
    If you don't need 2.2(1.7% user base?) support and are fine with 2.3 you should use a newer version `compile 'com.google.android.gms:play-services:4.0.30'` – Maciej Swic Dec 06 '13 at 13:42
  • My gradle says `Relying on packaging to define the extension of the main artifact has been deprecated` because of this rule. I haven't find any not-deprecated method yet. – Reza Mohammadi Dec 25 '13 at 11:22
  • I think the latest play services right now is 4.1. So you should add 'com.google.android.gms:play-services:4.0.+' – IgorGanapolsky Jan 15 '14 at 22:13
  • 5
    compile 'com.google.android.gms:play-services:4.+' will give you 4.whatever. – slott Jan 30 '14 at 20:22
  • I'm not going to update this answer every time a new version of google play services is released. The specific version number is mostly irrelevant to the intent of the answer. – Matt Logan Dec 08 '14 at 22:45
27

enter image description here

enter image description here

Hit check on all tools, to be safe

enter image description here

Hit check on API you are working with

enter image description here

Hit check on all extras to be safe

enter image description here

Go to file -> project structure

enter image description here

enter image description here

enter image description here

Finally go to build at the top of the screen and hit rebuild project

AndyRoid
  • 5,062
  • 8
  • 38
  • 73
22

As @Matt said, it is correct, I am here covered also which I did not find in the answer.

To add the Google Play Service library in your application project, follow below steps :-

1) Open the build.gradle file of your application module directory. See screenshot

Screenshot

2) Add new in your dependecies, here you to update the version number whenever you update the Google Play Service.

dependencies {
    ...
    compile 'com.google.android.gms:play-services:4.2.42'
    compile files('src/main/libs/gson-2.2.4.jar')

}

3) Now Lastly important save your file and click on Sync with Gradle Files Iconin the toolbar.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • 1
    how do I know the latest version of the dependency it probably changed already or is it still 4.2.42? – Leo Apr 01 '16 at 00:54
  • @Leo https://mvnrepository.com/artifact/com.google.android.gms/play-services-location https://developers.google.com/android/guides/setup – AturSams Feb 05 '20 at 11:17
14

This didn't work for me, I had to download the Goople Repository in the Android SDK Manager - Extras, to fix it.

Javier Roberto
  • 1,293
  • 11
  • 17
3

Try to avoid including entire google play services, when you use more library then you get Multidex exception:

The number of method references in a .dex file cannot exceed 64k

So best way to use:

compile 'com.google.android.gms:play-services-maps:9.8.0'

if you wish to include other services, please refer here:

https://developers.google.com/android/guides/setup (scroll down)

Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
  • Imran i'm using compile 'com.google.android.gms:play-services-maps:9.8.0' but getting error Please fix the version conflict either by updating the version of the google-services. plz help – Thripthi Haridas Dec 03 '16 at 05:02
1

Go to Android SDK Manager, go to Extras, select Google Play Services and Google Repository and then click on 'Install Packages...' button.

Click on 'build.gradle' file, in the code window in editor, then click on 'try again' link button...

Wait until gradle build finish.

luchopintado
  • 899
  • 11
  • 15
0

Go to the setting -> apps in your android testing phone and search for google play services and click on it, there you will find the version number installed on your device.

For me it was 9.6.83

then just add your version number to this command below :-

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

  • Add this to you build.gradle(app)

  • Gradle sync

Later you may get a gradle error saying that the particular repository needs to be installed, Go ahead and download it ! Voila your next gradle sync is successfull.

Yash Gupta
  • 331
  • 2
  • 8
0

Add this line to your app gradle in dependency block

implementation 'com.google.android.gms:play-services-maps:15.0.1'

Makvin
  • 3,475
  • 27
  • 26
0

Add Google Play Service ads and their feature in your android Project

  • Open your Android Studio build.gradle file
  • In Dependency add Google Play Service
  • implementation 'com.google.android.gms:play-services-ads:20.5.0'
  • sync your build.gradle file