20

我正在开发简单的地图应用程序,我添加了 google-play-services.jar 及其对 gradle 的引用

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

主要活动:

setContentView(R.layout.activity_mapui);

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

布局文件

<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"/>

但是应用程序在设备上崩溃了

E/AndroidRuntime: 致命例外:主要 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(未知来源)

我尝试了几种方法,同样的问题,我只想用 Gradle 0.6+ 将谷歌服务添加到 Android Studio 0.3

4

9 回答 9

34

dependencies在文件的部分添加这一行build.gradle

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

您不需要将 a 添加.jar到您的libs文件夹中。

编辑

正如 Varun 所指出的,您还需要更新 SDK 以包含最新版本的播放服务。

您可以通过从 Eclipse+ADT 打开 Android SDK 管理器(在Window选项卡下),选中文件夹旁边的框Google Play servicesExtras然后单击安装来完成此操作。

于 2013-11-01T17:53:47.993 回答
27

在此处输入图像描述

在此处输入图像描述

点击检查所有工具,以确保安全

在此处输入图像描述

点击检查您正在使用的 API

在此处输入图像描述

点击检查所有额外内容以确保安全

在此处输入图像描述

转到文件-> 项目结构

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

最后转到build屏幕顶部并点击rebuild project

于 2014-06-19T02:10:21.320 回答
22

正如@Matt 所说,这是正确的,我在这里也涵盖了我在答案中没有找到的内容。

要在您的应用程序项目中添加 Google Play 服务库,请按照以下步骤操作:-

1) 打开build.gradle应用程序模块目录的文件。看截图

截屏

2)在您的依赖项中添加新的,在这里您可以在更新 Google Play 服务时更新版本号。

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

}

3)现在最后重要的是保存您的文件并单击工具栏中的与Gradle文件同步图标

于 2014-03-02T12:46:13.067 回答
14

这对我不起作用,我必须在 Android SDK Manager - Extras 中下载 Goople 存储库来修复它。

于 2014-02-12T08:59:18.893 回答
3

尽量避免包含整个 google play 服务,当您使用更多库时,您会得到Multidex异常:

.dex 文件中方法引用的数量不能超过 64k

所以最好的使用方法:

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

如果您希望包括其他服务,请参阅此处:

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

于 2016-10-26T03:22:33.337 回答
1

转到 Android SDK Manager,转到 Extras,选择 Google Play Services 和 Google Repository,然后单击“Install Packages...”按钮。

在编辑器的代码窗口中单击“build.gradle”文件,然后单击“重试”链接按钮...

等到 gradle 构建完成。

于 2014-06-19T01:15:55.947 回答
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.

于 2016-10-31T04:20:55.270 回答
0

Add this line to your app gradle in dependency block

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

于 2018-09-05T05:14:38.733 回答
0

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

  • Open your Android Studio build.gradle file
  • 在依赖项中添加 Google Play 服务
  • 实施 'com.google.android.gms:play-services-ads:20.5.0'
  • 同步你的 build.gradle 文件
于 2021-11-24T09:23:54.473 回答