我正在浏览 Google 在 Android Studio 中关于“将 Google Play 服务添加到您的项目”的文档:
https://developer.android.com/google/play-services/setup.html
我正在使用该文档来修改新创建的 Android 项目的 build.gradle 文件。在第 2 步(将 Google Play 服务添加到您的项目)中,它指出:
添加这一行:
apply plugin: 'android'
在依赖项下,添加:
compile 'com.google.android.gms:play-services:5.0.77'
它还表示要在更新 Google Play 服务后更新该版本,根据 Android SDK Manager,该版本现在为 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 文件(或环境)有什么问题?
最佳答案
您引用的 Google 文档是正确的,并且不冲突。有不止一个 build.gradle 文件。与其将依赖项放在顶层,不如将它们放在模块目录中的构建文件中。
另外,不要输入 apply plugin: 'android'
该顶级构建文件中的语句;它会导致错误。
您还可以通过 Project Structure UI 添加依赖项,这样做是正确的。
关于gradle - 发现不支持的 Gradle DSL 方法 : 'compile()' !,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24730117/