gradle - 发现不支持的 Gradle DSL 方法 : 'compile()' !

标签 gradle android-studio

我正在浏览 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 文档是正确的,并且不冲突。有不止一个 b​​uild.gradle 文件。与其将依赖项放在顶层,不如将它们放在模块目录中的构建文件中。

另外,不要输入 apply plugin: 'android'该顶级构建文件中的语句;它会导致错误。

您还可以通过 Project Structure UI 添加依赖项,这样做是正确的。

关于gradle - 发现不支持的 Gradle DSL 方法 : 'compile()' !,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24730117/

相关文章:

java - 使用 Gradle 6 构建避免拆分包的附加工件

java - 如何将 android 项目作为库导入而不是将其编译为 apk (Android studio 1.0)

java - Gradle 和雅各科 : Get jacoco reports for Test-type task other than "test"

gradle - 如何通过GradleBuild任务类型从另一个Gradle子项目运行Gradle子项目任务

java - 系统健康 OpenJDK 显示间歇性性能和 UI 问题。我们推荐使用 Oracle JRE/JDK

android - 为什么activity_main.xml屏幕只是白色?

git - 无法使用 SSH(密码、MAC、KexAlgorithmsc)从版本控制 Git 中 check out

java - 如何在 Intellij Idea 中使用 Gradle 正确地将依赖项添加到同一级别的模块?

android-studio - 错误消息 : can not resolve method Create(anonymous android. view.View.OnClickListener,int)

java - 在android studio中使用firestore将新集合添加到同一文档