如何在使用 Google Maps Api v2 的 Android Studio 中创建 Android 应用程序? [英] How can I create an Android application in Android Studio that uses the Google Maps Api v2?

查看:25
本文介绍了如何在使用 Google Maps Api v2 的 Android Studio 中创建 Android 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android Studio 中尝试使用 GoogleMap 创建应用时遇到了很多麻烦.

我之前遵循以下指南,使用 Eclipse 作为我的 IDE(几乎)没有问题:

https://developers.google.com/maps/documentation/android/start

我以前从未使用过 Android Studio,并且在整个项目/模块范例中遇到了困难.

我一直无法成功配置 Google Play Services SDKhttp://developer.android.com/google/play-services/setup.html

这是我遇到的奇怪错误之一:

<前>摇篮:FAILURE:构建失败,出现异常.* 什么地方出了错:任务:MyMapApp:compileDebug"的执行失败.> 编译失败;有关详细信息,请参阅编译器错误输出.* 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪.使用 --info 或 --debug 选项运行以获得更多日志输出.

解决方案

最后我成功地使用 Android Studio 运行了 GoogleMapsAPIv2 项目.

正如 Xavier 所提到的,此方法仅适用于非基于 gradle 的项目.本教程中使用的 UI 部分将从 AndroidStudio 中排除.因此,如果您有自己的项目使用 Gradle 构建系统,您需要手动修改 build.gradle 配置文件,因为 Android Studio UI 不影响它.

在AndroidStudio v0.1.1 版本中,负责模块依赖的UI 部分已被删除,因此现在我们需要通过build.gradle 文件手动更新依赖项.用于更改 gradle 依赖项的 UI 将在下一版本中发布

对于那些仍然尝试使用这种方法的人 - 请注意它已经过时并且不再适用

这是我所做的:

1) 我从 Google Play 服务示例中获取了 maps 项目并将其复制到单独的目录中.这将是我们将尝试运行的 MapsApiV2 项目.在我的 Mac 上,它位于 /extras/google/google_play_services/samples我把它放到了 ~/Work/stack/

2) 将 google-play-services_lib 项目目录复制到同一个地方 (~/Work/stack),所以我的工作目录是这样的.顺便说一句,lib 项目位于 /extras/google/google_play_services/libproject:

3) 现在让我们打开 Android Studio.在欢迎屏幕上按 Import Project 并从 ~/Work/stack/maps 导入我们的 maps 项目.现在我们看到很多关于未知引用 GMS 库的抱怨:

4) 现在我们需要添加 Google Play Service 作为参考库.转到 View ->打开模块设置

5) 在 Modules 选项卡上,单击 + 按钮并选择 Import Module 并导入您的 GooglePlayServices 库.我没有更改向导中的任何内容,因此一直单击 Next 到最后:

6) 现在您需要引用这个导入的库.再次打开此屏幕(转到 View -> Module Settings).确保您选择了 maps 项目和 Dependency 选项卡.点击+添加依赖,选择Library.在那里选择您导入的库:

7) 现在我们可以看到它不是在抱怨 GMS 库,而是在抱怨支持库:

8) 让我们修复它.我的支持库位于 /extras/android/support/v13/android-support-v13.jar.所以让我们尝试将它添加到我们的工作区.转到 <代码> 查看 ->打开 Module Settings 并选择 Libraries 选项卡.选择 + -> Java 并选择支持库:

9) 现在它会询问您要将这个库添加到哪个项目,因此请确保您已选择您的 maps 项目:

10) 此时代码应该可以编译而没有问题.只需确保您在 Manifest 中使用正确的 SDK 版本即可.

玩得开心

I've been having a lot of trouble in Android Studio trying to create an app with GoogleMap.

I have followed the following guide before with (almost) no issues using Eclipse as my IDE:

https://developers.google.com/maps/documentation/android/start

I have never used Android Studio before and I'm having difficulty with the whole project/module paradigm.

I haven't been able to successfully configure the Google Play Services SDK http://developer.android.com/google/play-services/setup.html

Here is one of the weird errors I'm getting:

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':MyMapApp:compileDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

解决方案

Finally I managed to run GoogleMapsAPIv2 project using Android Studio.

EDIT: As mentioned by Xavier, this method is going to work for non-gradle based projects only. And UI part which was used in this tutorial will be excluded from AndroidStudio. So if you have your own project which uses Gradle build system, you need to manually modify build.gradle configuration file since Android Studio UI doesn't affect it.

EDIT2: With AndroidStudio v0.1.1 release, UI part responsible for modules dependencies has been eliminated, so for now we need to update dependencies manually through build.gradle file. UI for changing gradle dependencies is going to be released in next releases

EDIT3: For those who still tries to use this approach - please note that it is obsolete and doesn't work anymore

Here is what I did:

1) I took maps project from the Google Play Services samples and copied that to the separate directory. That is going to be our MapsApiV2 project we will be trying to run. On my Mac it was located at <sdk_location>/extras/google/google_play_services/samples I placed it to the ~/Work/stack/

2) Copied google-play-services_lib project directory to the same place (~/Work/stack), so my working directory looks like this. Btw, lib project is located at <sdk_location>/extras/google/google_play_services/libproject:

3) Now let's open Android Studio. On welcome screen press Import Project and import our maps project from ~/Work/stack/maps. Now we see a lot of complaints about unknown reference to GMS library:

4) Now we need to add Google Play Service as a reference library. Going to View -> Open Module Settings

5) On the Modules tab, click + button and select Import Module and import your GooglePlayServices lib. I didn't change anything in the wizards, so clicked Next all the way to the end:

6) Now you need to reference this imported library. Open this screen again (go to View -> Module Settings). Make sure you have your maps project and Dependency tab selected. Click + to add a dependency and select Library. Choose your imported library there:

7) Now we can see that it is not complaining about GMS library, but still complaining about support library:

8) Let's fix it. I have my support library located at <sdk location>/extras/android/support/v13/android-support-v13.jar. So let's try to add it to our workspace. Go to View -> Open Module Settings and select Libraries tab. Select + -> Java and select support library:

9) Now it is going to ask you which project to add this lib to, so make sure you have selected your maps project:

10) At this point code should compile w/o problems. Just make sure you are targeting the right SDK version in Manifest.

Have fun

这篇关于如何在使用 Google Maps Api v2 的 Android Studio 中创建 Android 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆