带有 Google Play 服务的 Android Studio [英] Android Studio with Google Play Services

查看:31
本文介绍了带有 Google Play 服务的 Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 Android Studio 测试 Google Play 服务.我有一个依赖于 google_play_services.jar 的项目.但是当我尝试重建项目时出现以下错误:

I'm trying to test Google Play Services with the new Android Studio. I have a project with a dependency to the google_play_services.jar. But when I try to Rebuild the project I get the following errors:

Information:[TstGP3-TstGP3] Crunching PNG Files in source dir: C:\Users\ans\AndroidStudioProjects\TstGP3\TstGP3\src\main\res
Information:[TstGP3-TstGP3] To destination dir: C:\Users\ans\AndroidStudioProjects\TstGP3\build\classes\res-cache\TstGP3-TstGP3
Information:Compilation completed with 2 errors and 0 warnings in 2 sec
Information:2 errors
Information:0 warnings
C:\Users\ans\.AndroidStudioPreview\system\compiler\tstgp3.3f17bd41\.generated\Android_BuildConfig_Generator\TstGP3-TstGP3.74fc5b25\production\com\example\tstgp3\BuildConfig.java
    Error:Error:line (4)error: duplicate class: com.example.tstgp3.BuildConfig
C:\Users\ans\.AndroidStudioPreview\system\compiler\tstgp3.3f17bd41\.generated\aapt\TstGP3-TstGP3.74fc5b25\production\com\example\tstgp3\R.java
    Error:Error:line (10)error: duplicate class: com.example.tstgp3.R

它似乎有两个 BuildConfig 文件和两个 R 类.我该如何解决这个问题?

It seems that it has two BuildConfig files and also two R classes. How can I resolve the issue?

我注意到编译器编译了两个 R.java 文件:一个位于我的项目文件夹中,另一个位于文件夹 %USERPROFILE%.AndroidStudioPreview所以,我试图在编译器设置中排除这个预览"文件夹,现在它可以工作了.只有在我开始在我的项目中使用 Google Play 服务类后才会出现此问题.如果有人能解释这个问题背后的原因,我将不胜感激.

I have noticed that the compiler compiles two R.java files: the one that is in my project folder and another one that is located in the folder %USERPROFILE%.AndroidStudioPreview So, I tried to exclude this "Preview" folder in the compiler settings and now it's working. This issue only occurs after I have started to use Google Play Services classes in my project. I will appreciate if someone can explain the reason behind this problem.

推荐答案

所有这些答案都是错误的,自从 gradle plugin v0.4.2 发布以来,android studio 下 google play 服务的设置是直接的.您不需要导入任何 jar 或添加任何项目库,也不需要在 android studio 下添加任何新模块.您需要做的是将正确的依赖项添加到 build.gradle 文件中.请查看这些链接:Gradle 插件 v0.4.2 更新新构建系统这个样本

All those answers are wrong, since the release of gradle plugin v0.4.2 the setup of google play services under android studio is straight forward. You don't need to import any jar or add any project library nor add any new module under android studio. What you have to do is to add the correct dependencies into the build.gradle file. Please take a look to those links: Gradle plugin v0.4.2 update, New Build System, and this sample

正确的方法如下:

首先,您必须启动 sdk 管理器并下载并安装位于extras"下的以下文件:Android 支持存储库Google Play 服务Google 代码库.

First of all you have to launch the sdk manager and download and install the following files located under "extras": Android support repository, Google play services, Google repository.

重启android studio并打开构建gradle文件.您必须修改 build.gradle 文件,使其在依赖项下看起来像这样:

Restart android studio and open the build gradle file. You must modify your build.gradle file to look like this under dependencies:

dependencies {
    compile 'com.google.android.gms:play-services:6.5.87' 
 }

最后同步您的项目(AVD 管理器左侧的按钮).

And finally syncronise your project (the button to the left of the AVD manager).

从 6.5 版开始,您可以包含完整的库(非常大)或仅包含您需要的模块(最佳选项).即,如果您只需要 Google 地图和分析,您可以用以下示例替换上一个示例:

Since version 6.5 you can include the complete library (very large) or just the modules that you need (Best Option). I.e if you only need Google Maps and Analytics you can replace the previous example with the following one:

dependencies {  
    compile 'com.google.android.gms:play-services-base:6.5.87'    
    compile 'com.google.android.gms:play-services-maps:6.5.87'  
}

您可以在此处

一些旁注:

  • 使用最新的播放服务库版本.如果是旧版本,android studio 会高亮显示.截至今天(2 月 5 日是 6.5.87),但您可以在 Gradle Please
  • 查看最新版本
  • 在 Android Studio 进行重大更新后,按照@user123321 评论中的建议按照以下说明清理重建项目

  • Use the latest play services library version. If it's an old version, android studio will highlight it. As of today (February 5th is 6.5.87) but you can check the latest version at Gradle Please
  • After a major update of Android Studio, clean an rebuild your project by following the next instructions as suggested in the comments by @user123321

cd 到您的项目文件夹
./gradlew 干净
./gradlew 构建

cd to your project folder
./gradlew clean
./gradlew build

这篇关于带有 Google Play 服务的 Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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