'dependencies' 不能应用于“(groovy.lang.Closure)”

'dependencies' cannot be applied to '(groovy.lang.Closure)'

我无法修复这个错误:

dependencies cannot be applied to '(groovy.lang.Closure)

这是我的 gradle 文件:

buildscript {
     repositories {
         maven { url 'http://download.crashlytics.com/maven' }
     }

     dependencies {
         classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
     }
 }
apply plugin: 'android'
apply plugin: 'crashlytics'

repositories {
   maven { url 'http://download.crashlytics.com/maven' }
}

dependencies {
    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':FRNDzTER_core')
    compile project(':cropper')
    compile project(':stickyListHeaders')
    compile "com.nostra13.universalimageloader:universal-image-loader:${rootProject.universalImageLoaderVersion}"
    compile "com.google.android.gms:play-    services:${rootProject.googlePlayServicesVersion}"
    compile "de.keyboardsurfer.android.widget:crouton:${rootProject.croutonVersion}"
    compile "com.nineoldandroids:library:${rootProject.nineoldandroidsVersion}"
    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+'
    compile 'com.crashlytics.android:crashlytics:1.+'
}

android{
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion
    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode rootProject.versionCode
        versionName rootProject.versionName
    } 
    buildTypes {
        release {
            debuggable rootProject.prodDebug
            proguardFile 'proguard.cfg'
        }
    }

    dependencies {
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    lintOptions {
        abortOnError false
    }
 }

根据 Android Studio 生成的内容,您需要有一个顶级项目文件 build.gradle,另一个用于您的应用程序 build.gradle

顶级:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url 'http://download.crashlytics.com/maven' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'http://download.crashlytics.com/maven' }
    }
}

应用级别:

apply plugin: 'com.android.application'
apply plugin: 'crashlytics'

android{
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion
    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode rootProject.versionCode
        versionName rootProject.versionName
    } 
    buildTypes {
        release {
            debuggable rootProject.prodDebug
            proguardFile 'proguard.cfg'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    lintOptions {
        abortOnError false
    }
 }     `

dependencies {
    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':FRNDzTER_core')
    compile project(':cropper')
    compile project(':stickyListHeaders')
    compile "com.nostra13.universalimageloader:universal-image-          l                        loader:${rootProject.universalImageLoaderVersion}"
    compile "com.google.android.gms:play-    services:${rootProject.googlePlayServicesVersion}"
    compile "   "de.keyboardsurfer.android.widget:crouton:${rootProject.croutonVersion}"
    compile "com.nineoldandroids:library:${rootProject.nineoldandroidsVersion}"
    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+'
    compile 'com.crashlytics.android:crashlytics:1.+'
}

但即使没有这些,你的问题是你的 android 插件配置中有一个 dependencies

android {
    dependencies {
    }
}

删除那个空的 dependencies 块。

编辑:我也开始在最新的 Android Studio 中遇到这个错误,我所要做的就是添加更新版本的 Gradle 插件和 compileSdkVersion 22。

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

我敢打赌,问题与发出警告的代码完全无关,很可能是您的混淆文件。我的代码中有以下内容并收到相同的警告:

buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

通过注释掉 runProguard false,我所有的问题都消失了,看图:)

我的问题是整个 build.setting 文件都被 cannot be applied to '(groovy.lang.Closure)' 警告消息占用,而不是发生在任何特定变量上。

我已经尝试了其他人提供的所有解决方案,但 none 对我有用。 我结束了这些步骤,然后它就像一个魅力。如果您遇到同样的问题,请尝试一下。

  1. 打开并编辑文件:yourproject/gradle/wrapper/gradle-wrapper.properties。编辑内容以更新 gradle 分发版本,如下图所示,然后保存。
  2. 删除此文件夹:yourproject/.gradle
  3. 点击Sync project with gradle files,就可以开始了。

您可以转到首选项和 select "use default gradle wrapper" 然后重建项目。它对我很有效:

转到

Windows

文件 -> 设置 -> 构建、执行、部署 -> 构建工具 -> Gradle

Mac

首选项 -> 构建、执行、部署 -> 构建工具 -> Gradle

和select使用默认的gradle包装器

当我注意到 gradle 主目录错误的警告时,我进入了首选项以尝试其他答案之一。所以我打开文件浏览器并选择较新的 gradle 版本,所有错误都消失了。图片如下所示。

我用 gradle 2.10 解决了这个问题。

在这里下载:http://gradle.org/gradle-download/

并像这样设置本地 gradle 发行版:

  1. 关闭项目(文件/关闭项目),
  2. Select“打开现有的 Android Studio 项目”在开始菜单和 select 包含您的 Android 项目的文件夹。

这对我有用。

如果您已经在使用 "default gradle wrapper" 但它没有帮助:

In Menu click: File -> Invalidate Caches / Restart...

如果还是不行,请尝试 2​​ 个步骤:

1) Delete ".gradle" folder (and "build" folder if you have it already)

2) In Menu click: File -> Invalidate Caches / Restart...

重新启动后警告应该消失。

(对我来说它有效,对 Android Studio 和 IntelliJ Idea 有效)

我在 android studio 2.2 中遇到了同样的问题,它对我有用:

原文:

android {
...
}

dependencies {
...
}

android中移动dependencies

android {
...
   dependencies {
   ...
   }
}

Gradle 文件可以在几个地方显式化以避免此类错误。例如。变化

configurations {

project.configurations {

task {
    description 'foo'

task {
    setDescription 'foo'

要解决此问题,只需关闭项目,然后在开始菜单上 select“打开现有的 Android Studio 项目”,然后 select 包含您的 [=14] 的文件夹=] 项目。请注意,select 文件夹,而不是 .iml 项目文件。

我通过使缓存无效并重新启动解决了 Android Studio 中的问题。

File-> Invalidate Caches / Restart..

将 "buildTypes" 剪切并粘贴到 "android" 部分和重新同步(刷新)项目的相同位置

我 运行 在 IntelliJ 中遇到了与 Kotlin 项目类似的问题。看起来问题是在我的项目中,即使它被设置为使用 JDK 8,语言和 API 版本不知何故被设置为 1.3。请参阅项目首选项 > 构面。在我的例子中,我勾选了 "use project settings,",但每个方面手动设置它们也可能有效。

对于使用 M1 Intellij IDEA 的人只需添加 build.gradle(:app)

//noinspection GroovyAssignabilityCheck

buildFeatures {
        viewBinding
    }