A7maDev A7maDev - 1 month ago 10
Android Question

Add Google Play Services to Android Studio using Gradle

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+

Answer

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.