Add Google Play Services to Android Studio using G

2019-01-31 18:32发布

问题:

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+

回答1:

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

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 in the toolbar.



回答2:

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.



回答3:

Hit check on all tools, to be safe

Hit check on API you are working with

Hit check on all extras to be safe

Go to file -> project structure

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



回答4:

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



回答5:

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)



回答6:

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.



回答7:

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.



回答8:

Add this line to your app gradle in dependency block

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