129

I am unable to fix this error:

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

This is my gradle file:

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
    }
 }
2

16 Answers 16

248

You can go to Preferences and select "use default gradle wrapper" then rebuild the project. It worked well for me: enter image description here

8
  • 5
    Does anybody have an explanation why this happens?
    – racs
    Commented Dec 7, 2015 at 22:26
  • 1
    It seems like after having updated from 1.4 to 1.5, this setting got "reset" to use the local distribution instead on my machine. This answer definitely resolved the issue. Commented Jan 11, 2016 at 9:33
  • 3
    As of 143.2532994 version this problem seems to happen again...anyone same with me...? So annoying..
    – Arst
    Commented Jan 20, 2016 at 2:26
  • Despite the selected answer being very elaborate and honest, there is a reason why this answer has order of magnitude more upvotes Commented Feb 15, 2016 at 11:35
  • Oh, yes, there is a reason - mac users. On windows, the page is different. "Nothing to show". The marked answer is at least somehow useful.
    – Gangnus
    Commented Mar 16, 2016 at 15:34
37

Go to

Windows

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

Mac

Preference -> Build, Execution, Deployment -> Build Tools -> Gradle

and select Use default gradle wrapper

3
  • 1
    Windows. Nothing to select.
    – Gangnus
    Commented Mar 16, 2016 at 15:31
  • 2
    First you have to open the project first.
    – Raptor
    Commented May 4, 2016 at 12:01
  • And on Linux it's the same as on Windows: File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle. You can edit your answer and write "Windows/Linux".
    – lenooh
    Commented Aug 25, 2016 at 11:52
22

Based on what Android Studio generates, you need to have a top-level project file build.gradle, and another for your app build.gradle.

Top-level:

// 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' }
    }
}

Application level:

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.+'
}

But even without all that, your problem is that you have a dependencies within your android plugin config.

android {
    dependencies {
    }
}

remove that empty dependencies block.

EDIT: I also started getting this error with the latest Android Studio, all I had to do was add a newer version of the Gradle plugin, and 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()
    }
}
5
  • 12
    I did that still same problem :( Commented Apr 21, 2015 at 11:48
  • to be honest, I think you should fix the extra quotes and the spaces in your Crouton, Universal-Image-Loader and googleplayServices dependencies. You should use only part of the Google Play Services, too according to developer.android.com/google/play-services/setup.html Commented Apr 21, 2015 at 11:54
  • its not working...can you give me fb id or anyother...so we can workout ? please? Commented Apr 21, 2015 at 12:04
  • ...in that case, I don't know what your bug is. :( last time this happened to me, my android and gradle plugin were outdated, and I was using a config type that no longer existed Commented Apr 21, 2015 at 12:36
  • 1
    Depends on whether you are using Command Line or IDE. And if IDE, then what IDE. Commented Apr 21, 2015 at 13:58
20

If you already are using the "default gradle wrapper" and it doesn't help:

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

If it also doesn't help try in 2 steps:

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

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

After restarting the warning should disappear.

(For me it worked and for Android Studio and for IntelliJ Idea)

4
  • Worked for me. The only thing to note is: If you are using your own gradle instead of the bundled one, you have to delete .gradle in your home directory too.
    – user2936450
    Commented Feb 26, 2016 at 9:57
  • 2
    Just doing the 2) instruction worked for me. Thanks!! Commented May 16, 2016 at 10:20
  • 3
    worked ... for 5 seconds. then I got that error again.
    – Yar
    Commented Jun 17, 2016 at 16:55
  • Its a bit frustrating how bad IntelliJ has gotten of recent with parsing projects properly. This used to be its forte...now it feels like it stumbles at the slightest thing.
    – smaudet
    Commented Mar 27, 2017 at 4:11
18

My problem is that the whole build.setting file were occupied with cannot be applied to '(groovy.lang.Closure)' warning messages instead of happening on any particular variable.

I have tried all solutions provided by others but none of them works for me. I ended out doing these steps then it works like a charm. If you are encountering the same issue then give it a try.

  1. Open and edit file: yourproject/gradle/wrapper/gradle-wrapper.properties. Edit content to update the gradle distribution version as shown in the image below then save.
  2. Delete this folder: yourproject/.gradle.
  3. Click Sync project with gradle files, then you are good to go.

enter image description here

2
  • 1
    There is no need to delete the yourproject/.gradle directory. If you choose to do so, be sure to save any customizations you've made there, such as adding a gradle.properties file with org.gradle.daemon=true (a common optimization). However, you can simply rebuild, and the gradle wrapper will download the new version specified in the distributionUrl, alongside the older versions. It's fine for older version directories to remain in your project's gradle directory. Commented Aug 5, 2015 at 2:13
  • Updating the gradle version in gradle-wrapper.properties solved my issue. Thanks!
    – BVB
    Commented Aug 7, 2015 at 15:24
4

I went into the preferences to try one of the other answers when I noticed a warning that my gradle home directory was wrong. So I opened up the file browser and chose the newer gradle version, and all the errors went away. Image shown below.

enter image description here

1

I bet you the problem is totally unrelated to the piece of code that is giving you warnings, most likely it's your proguard file. I had the following in my code and got the same warning:

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

By commenting out runProguard false, all of my problems go away, go figure :)

1
  • Daniel Wilson does it help if you change runProguard to minifyEnabled instead? More info is provided here
    – Sergii N.
    Commented Jul 6, 2015 at 7:44
1

I have same problem in android studio 2.2,it work for me:

original:

android {
...
}

dependencies {
...
}

move dependencies in android:

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

I solved the problem in Android Studio by Invalidating the cache and restart.

File-> Invalidate Caches / Restart..

enter image description here

1

I ran into a similar problem to this in IntelliJ for a Kotlin project. It looks like the issue was that in my project, even though it was set to use JDK 8, the language and API versions somehow got set to 1.3. See Project Preferences > Facets. In my case, I ticked "use project settings," but manually setting them per facet may work as well.

0
  1. Close the project (File / Close project),
  2. Select “Open an existing Android Studio project” on the start menu and select the folder that contains your Android project.

This work for me.

0

Gradle files can be made explicit in several places to avoid such errors. E.g. Change

configurations {

to

project.configurations {

or

task {
    description 'foo'

to

task {
    setDescription 'foo'
0

To fix the issue simply close the project, then select “Open an existing Android Studio project” on the start menu and select the folder that contains your Android project. Beware, select the folder, not the .iml project file.

0

Cut and then paste the "buildTypes" at the same place in "android" section and Re-Sync (refresh) Project

0

For people with M1 Intellij IDEA just add in build.gradle(:app)

//noinspection GroovyAssignabilityCheck

buildFeatures {
        viewBinding
    }
-1

I fixed this issue with gradle 2.10.

Download it here : http://gradle.org/gradle-download/

And set your local gradle distribution like this :

as shown here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.