带有 Google Play 服务的 Android Studio

标签 android google-play-services android-studio

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

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 类。我该如何解决这个问题?

编辑:

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

最佳答案

所有这些答案都是错误的,因为 gradle plugin v0.4.2 的发布在 android studio 下设置 google play 服务是直截了当的。您无需导入任何 jar 或添加任何项目库,也无需在 android studio 下添加任何新模块。您要做的就是将正确的依赖项添加到 build.gradle 文件中。请查看这些链接:Gradle plugin v0.4.2 update , New Build System , 和 this sample

正确的方法如下:

首先,您必须启动 sdk 管理器并下载并安装位于“extras”下的以下文件:Android 支持库Google play services 谷歌存储库

重启 android studio 并打开 build gradle 文件。您必须修改您的 build.gradle 文件,使其在依赖项下如下所示:

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

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

从 6.5 版开始,您可以包含完整的库(非常大)或仅包含您需要的模块(最佳选项)。也就是说,如果您只需要 Google Maps and Analytics,您可以将前面的示例替换为以下示例:

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

你可以找到完整的依赖列表here

一些旁注:

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

    cd 到你的项目文件夹
    ./gradlew clean
    ./gradlew build

关于带有 Google Play 服务的 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624827/

相关文章:

android-studio - Android Studio 3.0 运行速度非常非常慢

android-studio - 无法正确编译 libgdx 的播放服务

android - 无法在启用混淆器的情况下构建发布版本

android - Windows 在 cmd 中看不到 adb 设备

android - 应用程序更新的推送通知

android - 在gradle中包含播放服务时,所有com.android.libraries支持都必须使用相同版本

java - 关于 ActionBarSherlock 将项目导入 Android Studio 的问题

android - 在没有授权 token 的情况下获取 Facebook 页面的公开帖子

android - 在android中自定义操作栏首先显示默认操作栏

android - 谷歌cast MediaNotificationService 中的大量RemoteServiceExceptions