Java 将 Google Play 服务包含到 Android Studio 项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18198719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:14:04  来源:igfitidea点击:

Including Google Play Services to Android Studio project

javaandroidandroid-studio

提问by Jivay

I've been trying to include the latest Google Play Services library to my Android Studio project in order to use push messages and Maps API.

我一直在尝试将最新的 Google Play 服务库包含到我的 Android Studio 项目中,以便使用推送消息和 Maps API。

Since there are plenty of tutorials on how to include this library on Eclipse and CLI, there are no instructions on how to include the latest library on Android Studio.

由于有大量关于如何在 Eclipse 和 CLI 上包含此库的教程,因此没有关于如何在 Android Studio 上包含最新库的说明。

I've been searching on many sites and one of the answers which looked to be the most fitting looked to be this one, since other ones appear to document an older version, but it still appears like I'm missing something.

我一直在许多网站上搜索,其中一个看起来最合适的答案似乎是这个,因为其他网站似乎记录了旧版本,但看起来我仍然遗漏了一些东西。

I've tried to include this lib the same way I included the Facebook library to my project (which is oddly better documented for Android Studio than GooglePlay is) but it still looks like i'm missing something.

我试图以与将 Facebook 库包含到我的项目中的方式相同的方式包含这个库(Android Studio 的文档记录比 GooglePlay 好得多),但看起来我仍然缺少一些东西。

To do so, I've copied the whole folder <android-sdk>\extras\google\google_play_services\libproject\google-play-services_libto my <project-path>\libraries\google-play-services_lib

为此,我已将整个文件夹复制<android-sdk>\extras\google\google_play_services\libproject\google-play-services_lib到我的<project-path>\libraries\google-play-services_lib

Then in Studio, i tried to add the copied folder in Module > add > Import module, like said in the Facebook documentation or in the link provided. I must be forgetting something like a gradle file, checking a module property, i don't really know anymore what i'm doing with this lib.

然后在 Studio 中,我尝试在模块 > 添加 > 导入模块中添加复制的文件夹,如 Facebook 文档或提供的链接中所述。我一定是忘记了 gradle 文件之类的东西,检查模块属性,我真的不知道我在用这个库做什么。

EDIT: I AM alreay working on Android Studio.

编辑:我已经在 Android Studio 上工作了。

采纳答案by Jivay

Using Android Studio, the only thing you need is to edit the build.gradle file and make sure that there are noandroid or google jar libraries, which means that you will need to remove jar libraries from your project (unless you're using libs like ActionbarSherlock which may need jar libs).

使用 Android Studio,您唯一需要的是编辑 build.gradle 文件并确保没有android 或 google jar 库,这意味着您需要从项目中删除 jar 库(除非您使用的是 libs像 ActionbarSherlock 可能需要 jar 库)。

If you plan using Google Play Services, your build.gradle file should look like this :

如果您计划使用 Google Play 服务,您的 build.gradle 文件应如下所示:

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
    compile 'com.android.support:support-v13:13.0.+'
    compile 'com.google.android.gms:play-services:3.1.36'
}

Pretty straightforward indeed.

确实很简单。

Also, if you're using Facebook SDK, you will need to edit its build.gradlefile and change its dependencies in accordance to your main project's build.gradle.

此外,如果您使用的是Facebook SDK,则需要build.gradle根据主项目的build.gradle.

回答by IanB

I'm assuming you have encountered a ClassNotFound exception because of the question you have referenced. If you were working in Eclipse I would suggest making sure you have checked "is library" on the Google Play Services library project and then making sure this library project is referenced in your app project.

我假设您遇到了 ClassNotFound 异常,因为您引用了这个问题。如果您在 Eclipse 中工作,我建议您确保已在 Google Play 服务库项目中选中“是库”,然后确保在您的应用项目中引用了此库项目。

However for Android Studio I suggest you look at this: Android Studio with Google Play Services

但是对于 Android Studio 我建议你看看这个: Android Studio with Google Play Services

The accepted answer looks relevant and presents a more complete procedure than the question you referenced.

接受的答案看起来相关,并提供了比您引用的问题更完整的程序。

回答by Jasper Hughes

I have a quick tutorial on how to get this working using Eclipse and Android Studio, its a bit lengthy but ive had 100% success rate so far and no errors.

我有一个关于如何使用 Eclipse 和 Android Studio 使其工作的快速教程,它有点冗长,但到目前为止我有 100% 的成功率并且没有错误。

Gradle Library Tutorial: http://www.aetherstudios.net/pages/gradle.html

Gradle 库教程:http://www.aetherstudios.net/pages/gradle.html

回答by Dan J

There are two steps to setting this up in Android Studio with Gradle.

在带有 Gradle 的 Android Studio 中进行设置有两个步骤。

1)Install the required parts of the SDK.

1)安装 SDK 所需的部分。

Android Studio uses a different Android SDK location to Eclipse, so you will need to do this in Android Studio even if you previously did it for Eclipse (unless you update them to share the same SDK location).

Android Studio 使用与 Eclipse 不同的 Android SDK 位置,因此您需要在 Android Studio 中执行此操作,即使您之前为 Eclipse 执行过此操作(除非您更新它们以共享相同的 SDK 位置)。

From the Android Studio menu bar, open Tools -> Android -> SDK Manager

从 Android Studio 菜单栏中,打开 Tools -> Android -> SDK Manager

Under the Extras section, install these:

在 Extras 部分下,安装这些:

  • Android Support Repository
  • Android Support Library
  • Google Play services
  • Google Repository
  • Android 支持库
  • 安卓支持库
  • 谷歌播放服务
  • 谷歌存储库

2)Add the Gradle dependencies.

2)添加 Gradle 依赖项。

Do this in the build.gradlefor your module (i.e. notthe top level one for the overall Android Studio project):

build.gradle您的模块中执行此操作(即不是整个 Android Studio 项目的顶级模块):

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.android.support:support-v13:13.0.+'
    compile 'com.google.android.gms:play-services:3.1.36'
}

This might not be the minimum set of instructions required, but it worked for me. :-)

这可能不是所需的最少指令集,但它对我有用。:-)