我正在查阅谷歌文档,内容为“在Android Studio中添加Google Play服务”:
https://developer.android.com/google/play-services/setup.html
我需要使用这份文档修改一个新建的安卓项目的build.gradle文件。在第二步(将Google Play服务添加到您的项目中),指出需要添加以下代码行:
在“依赖项”下,添加以下内容:
保存时,会提示进行同步。我进行了同步,但是出现以下错误:
我是一个Android Studio 0.8.2用户。我并没有安装Gradle,而是使用了Android Studio自带的插件。
值得注意的是,当我创建了这个新项目时,生成的build.gradle文件上写着:
但是谷歌的文档说(与上述内容相矛盾):
我的build.gradle文件(或环境)有什么问题?
apply plugin: 'android'
在“依赖项”下,添加以下内容:
compile 'com.google.android.gms:play-services:5.0.77'
它还指出在更新Google Play服务后更新该版本,根据Android SDK管理器,现在已经更新到18。
这是我整个build.gradle文件的顶层(此文件的父文件夹为根文件夹)。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'android'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile 'com.google.android.gms:play-services:18'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
保存时,会提示进行同步。我进行了同步,但是出现以下错误:
Build script error, unsupported Gradle DSL method found: 'compile()'!
Error:(10, 0) Possible causes could be:
- you are using a Gradle version where the method is absent
- you didn't apply Gradle plugin which provides the method
- or there is a mistake in a build script
我是一个Android Studio 0.8.2用户。我并没有安装Gradle,而是使用了Android Studio自带的插件。
值得注意的是,当我创建了这个新项目时,生成的build.gradle文件上写着:
//NOTE: Do not place your application dependencies here
但是谷歌的文档说(与上述内容相矛盾):
Note: Android Studio projects contain a top-level build.gradle file and a build.gradle
file for each module. Be sure to edit the file for your application module.
我的build.gradle文件(或环境)有什么问题?
buildScript
部分移动到子模块中也不会有帮助,对吧?这是完全有效的评论,但不是一个答案。 - JBaruchbuildScript
块。此外,如果apply plugin
语句误留在顶层构建文件中,它将导致非常奇怪的错误。 - Scott Bartabuildscript
上下文中使用compile
配置,因为buildscript
阶段只能有一个配置——classpath
,它与你的项目依赖项无关。它配置的是构建脚本本身(即添加带有“android”插件的jar来编译gradle内容)。所以,这只是一个错误的答案 :) - JBaruch