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

10
我正在查阅谷歌文档,内容为“在Android Studio中添加Google Play服务”: https://developer.android.com/google/play-services/setup.html 我需要使用这份文档修改一个新建的安卓项目的build.gradle文件。在第二步(将Google Play服务添加到您的项目中),指出需要添加以下代码行:
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文件(或环境)有什么问题?
5个回答

17

您引用的Google文档是正确的,没有冲突。有不止一个 build.gradle 文件。将依赖项放在顶级文件中可能会导致错误,因此请将它们放在模块目录中的构建文件中。

另外,请勿在顶层构建文件中放置 apply plugin: 'android' 语句,否则会发生错误。

您还可以通过“项目结构”UI添加依赖项,这样做是正确的。


Scott,作为你,你应该知道你的回答并不是一个真正的回答,它没有解释如何修复错误。将整个buildScript部分移动到子模块中也不会有帮助,对吧?这是完全有效的评论,但不是一个答案。 - JBaruch
我不理解你的评论。错误的编辑是在错误的构建文件中进行的,它们被放置在顶级而不是模块级别的文件中。我没有说过要移动 buildScript 块。此外,如果 apply plugin 语句误留在顶层构建文件中,它将导致非常奇怪的错误。 - Scott Barta
啊,我明白了,所以你没有发现问题。问题不在构建文件(它在顶层文件中也完美地工作),问题在于错误的闭包。你不能在buildscript上下文中使用compile配置,因为buildscript阶段只能有一个配置——classpath,它与你的项目依赖项无关。它配置的是构建脚本本身(即添加带有“android”插件的jar来编译gradle内容)。所以,这只是一个错误的答案 :) - JBaruch
1
Scott Barta,我把依赖项放在了模块的build.gradle文件中,它起作用了。现在,在那个Google Play服务设置页面上,我是否仍然需要将“apply plugin:'android'”行放入我的模块的build.gradle文件中? - Gerard

8
请勿通过编辑最“外部”的build.gradle文件(YourProject/build.gradle)为您的项目添加依赖项。请改为编辑位于“app”模块下的build.gradle文件(YourProject/app/build.gradle)。
在该文件中,您会找到一个依赖声明,例如:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

这个块将会在android { ... }配置块的下方。 在我的情况下,我只是添加了leeloo依赖包,所以它变成了:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'net.smartam.leeloo:oauth2-client:0.1'
    compile 'net.smartam.leeloo:oauth2-common:0.1'
}

然后同步您的项目,依赖项将被下载。希望能帮到您!

1
编译时依赖项应该在allprojects下的dependencies块中,而不是在buildscript下。
apply plugin: 'android'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

allprojects {
    repositories {
        jcenter()
    }
    dependencies {
        compile 'com.google.android.gms:play-services:18'
    }
}

这应该可以正常工作。

请不要将依赖项放在allprojects块中,除非您确实打算将其应用于项目中的每个模块,而这几乎肯定不是您想要的。请将其放在各个模块的构建文件中。 - Scott Barta
不确定这个play-services jar是否适用于所有模块作为公共依赖项。如果不是,你是对的,它应该放到子模块中。 - JBaruch

1

将“Gradle DSL方法”视为Java方法。因此,在Gradle中,方法可以通过{}或“.”来区分。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

的意思相同。
dependencies.compile fileTree(dir: 'libs', include: ['*.jar'])

这里的“dependencies”和“compile”都是方法。

因此,在您的build.gradle文件中包含了一个程序不支持的方法。例如,将您的依赖项设置为如下:

dependencies {
    nothing 'this.does.nothing.build:gradle:0.7.+'
}

这与写成以下内容相同:

dependencies.nothing 'this.does.nothing.build:gradle:0.7.+'

你会看到一个错误提示,说“找不到支持的Gradle DSL方法:'nothing()'!”显然,“nothing”不是真正的方法。我只是编造了它。
因此,在build.gradle中,你的一个“compile”方法是错误的。

0
当我遇到这个问题时,我使用了Android开发者UI来导入依赖项,具体步骤如下:
1. 进入"View"菜单,选择"Open Module Settings"。 2. 在"Dependency"选项卡中,点击"+"号添加依赖项,并选择"Library dependency"。在这里选择已下载的库文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接