【问题标题】:Unsupported Gradle DSL method found: 'compile()'!发现不支持的 Gradle DSL 方法:'compile()'!
【发布时间】:2014-09-03 23:46:45
【问题描述】:

我正在浏览 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 管理器,该版本现在为 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 文件(或环境)有什么问题?

【问题讨论】:

    标签: gradle android-studio


    【解决方案1】:

    您引用的 Google 文档是正确的,并且没有冲突。 build.gradle 文件不止一个。与其将依赖项放在顶层,不如将它们放在模块目录中的构建文件中。

    另外,不要在顶级构建文件中添加apply plugin: 'android' 语句;这会导致错误。

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

    【讨论】:

    • Scott,所有人中的你,应该知道你的答案不是答案,它没有解释如何修复错误。将整个 buildScript 部分移动到子模块不会有帮助,不是吗?这是完全有效的评论,但不是答案。
    • 我不明白你的评论。编辑是在错误的构建文件中进行的。他们进入了顶层而不是模块层。我没有说移动buildScript 块。另外,如果将apply plugin 语句错误地留在顶层构建文件中,会导致非常奇怪的错误。
    • 啊,我明白了,所以你没有发现问题。问题不是构建文件(它在顶级文件中也可以正常工作),问题是错误的关闭。您不能在buildscript 上下文中使用compile 配置,因为buildscript 阶段只能有一个配置-classpath,它与您的项目依赖关系无关。它自己配置构建脚本(即添加带有android 插件的jar 来编译gradle 的东西)。所以,这只是一个错误的答案:)
    • Scott Barta,我将依赖项放在模块 build.gradle 文件中,它起作用了。现在,在那个 Google Play 服务设置页面上,我是否还需要将“应用插件:'android'”行放入模块的 build.gradle 文件中?
    【解决方案2】:

    不要通过编辑其最“外部”的 build.gradle (YourProject/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'
    }
    

    然后同步您的项目和依赖项将被下载。希望对您有所帮助!

    【讨论】:

      【解决方案3】:

      编译时依赖项应位于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 块中,除非您真的打算将其应用于项目中的每个模块,而您几乎肯定不会这样做。将其放入单独的模块构建文件中。
      • 不确定这个 play-services jar 是否对所有模块都有意义作为公共依赖项。如果不是,你是对的,它应该去子模块。
      【解决方案4】:

      将“Gradle DSL 方法”视为一种 Java 方法。所以在 Gradle 中,方法可以用 {} 或“.”来区分。所以

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

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

      “依赖项”和“编译”都是方法。

      因此,您在 build.gradle 文件的某处包含了您的程序不支持的方法。例如,使您的依赖项如下:

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

      这和写的一样:

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

      您会看到一条错误消息“找到不支持的 Gradle DSL 方法:‘nothing()’!” 显然“无”不是真正的方法。我只是编的。

      所以你在 build.gradle 中的“编译”方法之一是错误的。

      【讨论】:

        【解决方案5】:

        我不确定为什么对前面提到的解决方案(Scott Barta 和 JBaruch)投了反对票。如果我们通过上面提到的解决方案和 cmets,它完全可以工作。

        但是,当我遇到这个问题时,我使用 android 开发者 UI 来导入依赖项,如下所示:-

        1 转到查看 ---> 打开模块设置

        1. 选择依赖选项卡。单击 + 添加依赖项并选择库依赖项。在此处选择下载的库。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-21
          • 2015-11-19
          • 2015-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-18
          相关资源
          最近更新 更多