130

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
    }
 }
Sufian
  • 6,405
  • 16
  • 66
  • 120
Ashwani Kottapalli
  • 1,322
  • 2
  • 8
  • 7
  • 1
    I'm pretty sure these are very much related and as my answer over there suggests, I think the error marking is a bug hence the disparity of solutions. http://stackoverflow.com/questions/29133601/buildtypes-cannot-be-applied-to-groovy-lang-closure/30823995#30823995 That's not to say the top answer here doesn't make some very valid general points about your "current" gradle buildscript – Seb Andraos Jun 13 '15 at 22:10
  • see this http://stackoverflow.com/questions/29133601/buildtypes-cannot-be-applied-to-groovy-lang-closure – myNameCoad Aug 26 '15 at 06:50

16 Answers16

249

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

Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20
  • 5
    Does anybody have an explanation why this happens? – racs Dec 07 '15 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. – seeming.amusing Jan 11 '16 at 09:33
  • 3
    As of 143.2532994 version this problem seems to happen again...anyone same with me...? So annoying.. – Arst Jan 20 '16 at 02:26
  • Despite the selected answer being very elaborate and honest, there is a reason why this answer has order of magnitude more upvotes – Pavel Zdenek Feb 15 '16 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 Mar 16 '16 at 15:34
  • Thanks, this gave me the hint to see that the path to my local gradle was wrong! – cherry-wave Jun 05 '16 at 19:56
  • yeah, it actually was set to this always, but someway it as changed to another option, so I had to get back to " "use default gradle wrapper" again. should be the right answer – user25 Aug 26 '16 at 16:37
  • Worked! But I also had to restart Android Studio after too. – Eric Aug 31 '16 at 11:00
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

Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
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()
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 12
    I did that still same problem :( – Ashwani Kottapalli Apr 21 '15 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 http://developer.android.com/google/play-services/setup.html – EpicPandaForce Apr 21 '15 at 11:54
  • its not working...can you give me fb id or anyother...so we can workout ? please? – Ashwani Kottapalli Apr 21 '15 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 – EpicPandaForce Apr 21 '15 at 12:36
  • 1
    Depends on whether you are using Command Line or IDE. And if IDE, then what IDE. – EpicPandaForce Apr 21 '15 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)

Andrew
  • 36,676
  • 11
  • 141
  • 113
  • 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. –  Feb 26 '16 at 09:57
  • 2
    Just doing the 2) instruction worked for me. Thanks!! – manolodewiner May 16 '16 at 10:20
  • 3
    worked ... for 5 seconds. then I got that error again. – Yar Jun 17 '16 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 Mar 27 '17 at 04: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

John Cummings
  • 1,949
  • 3
  • 22
  • 38
Arst
  • 3,098
  • 1
  • 35
  • 42
  • 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. – Mark McClelland Aug 05 '15 at 02:13
  • Updating the gradle version in `gradle-wrapper.properties` solved my issue. Thanks! – BVB Aug 07 '15 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

craned
  • 2,991
  • 2
  • 34
  • 38
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 :)

Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
  • Daniel Wilson does it help if you change `runProguard` to `minifyEnabled` instead? More info is provided [here](http://tools.android.com/tech-docs/new-build-system/migrating-to-1-0-0) – Sergii N. Jul 06 '15 at 07:44
1

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

original:

android {
...
}

dependencies {
...
}

move dependencies in android:

android {
...
   dependencies {
   ...
   }
}
Yakami
  • 91
  • 1
  • 4
1

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

File-> Invalidate Caches / Restart..

enter image description here

ahmadalibaloch
  • 5,851
  • 2
  • 50
  • 59
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.

Tim Keating
  • 6,443
  • 4
  • 47
  • 53
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.

zerob13
  • 11
  • 2
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'
tkruse
  • 10,222
  • 7
  • 53
  • 80
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.

Webdma
  • 714
  • 6
  • 16
0

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

Andrew
  • 36,676
  • 11
  • 141
  • 113
0

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

//noinspection GroovyAssignabilityCheck

buildFeatures {
        viewBinding
    }
Felipe Franco
  • 171
  • 3
  • 15
-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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245