244

I'm getting this warning in my project gradle file:

Warning:(16, 5) 'buildTypes' cannot be applied to '(groovy.lang.Closure< com.android.build.gradle.internal.dsl.BuildType>)'

My buildTypes section is:

    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }

I'm currently using Android Studio 1.1.0, compileSdkVersion 22, buildToolsVersion 22.0.0, and targetSdkVersion 22. I tried backing down to 21 but was still getting the warning.

What causes this warning & how is it fixed?

Jim Vitek
  • 4,174
  • 3
  • 23
  • 32

19 Answers19

471

For me the problem was not solved by applying the above solution. Instead I had to go to the settings within Android Studio and select "Use gradle wrapper":

In Android Studio select: File\Settings\Build, Execution, Deployment\Build tools\Gradle

(Mac Users: Android Studio\Preferences...\Build, Execution, Deployment\Build tools\Gradle )

Mark: Use default gradle wrapper (default)

This removed all 'cannot be applied to '(groovy.lang.Closure') warnings in the build files.

Sam
  • 3,453
  • 1
  • 33
  • 57
Peter
  • 10,910
  • 3
  • 35
  • 68
  • 2
    This is the correct answer, the problem happened when I opened the project on Windows and then on Linux (via Dropbox) it changes the location of the gradle distribution, so it's just better to use the default gradle wrapper. – Rodrigo Direito Jun 18 '15 at 14:21
  • 1
    I had the same issue. My teammate works on a windows machine. Fetching updates are giving me this issue – chip Jul 01 '15 at 10:22
  • @Waterbear I've edit the original answer because of this (I've searched awhile for this too :D). After a peer review it will be available :) See: http://i.imgur.com/BtCijIW.png – StefMa Jul 31 '15 at 09:00
  • Somehow 1.3.1 AS on Mac reset this setting on Build -> Clean. Setting it back fixes build. Thanks! – Den Drobiazko Aug 14 '15 at 09:48
  • That worked our for me except that initially all my settings were giving me this warning. After following @Peter's suggestions, only buildTypes gave me the warning, so I cut and pasted it in place as suggested below and that also did the trick. – Roberto Aug 25 '15 at 15:34
  • In my case, this option was already selected, I resolved the issue doing moving the code like the Luca Barillà's answer – androidevil Aug 27 '15 at 20:33
  • I had changed my gradle-wrapper.properties to point distributionUrl to a local, relative file (for firewall reasons, I've since realized that answer was to use a local gradle install instead of gradlew). For some reason, Android Studio (version 1.4 at least) doesn't correctly recognize local relative gradle distribution this way. the app still built, but I got a bunch of warnings in the IDE. changing it to an absolute path or remote url made the warnings go away – hair raisin Oct 15 '15 at 20:32
  • The main problem is the version! So instead of marking "Use default gradle wrapper (default)" just hit the three dots behind the path and select the new Version! – r00tandy Nov 04 '15 at 18:09
  • This worked for me after I started getting this error after upgrading from Android Studio 1.4 to 1.5. I also had to close and re-open Android Studio for the error to go away. – Paul Dec 04 '15 at 19:44
  • 11
    Something stupid with Android Studio, it started showing me warning when i made by build target 22 and buildToolsVersion 22.0.1.... For me 'Use default gradle wrapper' was already marked as default (AS 2.0 Beta)... i just cut my code block and pasted at very same place (no code changes, no config changes).... warning has been removed! – Umair Mar 23 '16 at 06:42
  • This does not remove all 'cannot be applied to '(groovy.lang.Closure') – Raji A C Oct 05 '16 at 08:15
  • I too agree with Umair, cut and paste also worked for me. might be some bug in android studio. – RockandRoll Dec 14 '16 at 07:14
129

I had the same issue. Moving the buildTypes as the last entry in the android section worked for me.

android {
    ...
    buildTypes {
    }
}
dorr
  • 1,509
  • 1
  • 11
  • 8
  • 2
    Odd that its position should make a difference but apparently it does. – Jason Crosby Aug 22 '15 at 04:05
  • This works for me too. Do you have any ideas how this work? Thanks. – myNameCoad Aug 26 '15 at 07:17
  • 4
    This seemed to work for me too, but then I undid those changes and copy/pasted that block in the same place and the warning also went away. Then I tried to undo it all and close and re-open the project to replicate the warning, but it was gone. I think it's just one of _those_ kinds of warnings. – elliptic1 Sep 22 '15 at 14:02
  • For me, almost of warnings were cleared by Peter's answer, except for a warning about `buildTypes`. Then, this solution worked for the last one! – hata Jun 26 '16 at 14:53
76

1 Cut and Paste the "buildTypes" at the same position in "android" section

2 Re Sync Project

Luca Barillà
  • 969
  • 6
  • 4
15

To solve this issue, in your Android Studio go to File -> Settings. In settings choose Build, Execution, Deployment -> Build Tools -> Gradle and select "Use default gradle wrapper (recommended)" option.

After rebuild your build.gradle will be totally fine.

Ivan V
  • 3,024
  • 4
  • 26
  • 36
14

If none of the above options work, then do the Following

  1. Change the project to Project perspective in the "Project Explorer".
  2. Delete .gradle and build folders.
  3. Click File -> Invalidate Caches / Restart... -> Invalidate and Restart
  4. Now its working, happy coding.
Atif Farrukh
  • 2,219
  • 2
  • 25
  • 47
7

I managed to solve my issue by simply doing this: Click "File" then select "Invalidate Caches/ Restart

Fred
  • 332
  • 4
  • 5
6

The easiest way to fix this Gradle issues is recreating the project to let Android Studio reconfigure Gradle.

Simply select "Open an existing Android Studio project" and pick your project folder.

More details here.

AleFranz
  • 771
  • 1
  • 7
  • 13
4

this worked for me:

buildTypes.debug {
    ext.enableCrashlytics = false
}

buildTypes.release {
    debuggable false
    zipAlignEnabled true
    minifyEnabled false
    signingConfig signingConfigs.release
}
serggl
  • 306
  • 3
  • 8
3

The problem presents when you import a project into your environment and the Gradle plugin is installed in a different location than what it was in the original. You just need to point out where your Gradle installation is.

Moving buildTypes or defaultConfig or whatever between each other is not a complete solution

Chisko
  • 3,092
  • 6
  • 27
  • 45
  • This how i finally solved this problem... 1. Navigate in Android Studio to: File | Settings | File | Settings | Build, Execution, Deployment | Build Tools | Gradle 2. Ensure that this is unchecked, despite that it is recommended to keep ... Use Default Gradle Wrapper (recommended) 3. and that this one is checked ... Use local gradle distribution 4. and that Gradle Home is set to this, with possibly a later gradle-n.n ... C:/Program Files/Android/Android Studio1/gradle/gradle-2.4 You may have to restart Android Studio, rebuild the project, etc but the problem is solved. – Mick Oct 31 '15 at 04:16
3

The issue at hand is that your Android Studio settings that you probably imported at upgrade, was still pointing to gradle-2.4 or some other versions than Android Studio 1.5 expects.

To solve this problem, you should open up Settings and go to Build, Execution, Deployment > Build Tools > Gradle and change your local gradle distribution's home path or select the default gradle wrapper.

Apply and re-sync your project.

anthonymonori
  • 1,754
  • 14
  • 36
2

I had the same problem. I started a new project and it worked, so I just copied the build.gradle file. In my case the only difference was the lack of the compileOptions section. I removed it from my project, synced gradle, re-inserted the compileOptions section and then synced gradle again.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
2

In your Android Studio go to File -> Settings. Next choose Build, Execution, Deployment -> Build Tools -> Gradle and select "Use default gradle wrapper (recommended)".

Rebuild project.

2

In my case my buildTypes section was above my productFlavors section. Interchanging their positions got rid of the warning:

BEFORE:

buildTypes {

        debug {
            minifyEnabled false
            shrinkResources false
            ...//etc
        }
 }

 productFlavors {
    demo {
        ...//etc
    }
 }

AFTER:

 productFlavors {
    demo {
        ...//etc
    }
 }    

 buildTypes {

        debug {
            minifyEnabled false
            shrinkResources false
            ...//etc
        }
 }

I am using Android Studio 2.1.2 with the following config at the top of my android section:

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.xyz.abc"
        minSdkVersion 14
        targetSdkVersion 24
        //..etc
Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
1

Changing distributionUrl=https://services.gradle.org/distributions/gradle-2.1-bin.zip to gradle-2.1-all.zip in gradle-wrapper.properties solved problem for me.

kengura
  • 121
  • 1
  • 5
1

I used to have this warning, after I updated Android Studio.

When I went to Settings>>Build Tools>>Gradle I found Local Gradle path was invalid, since gradle version was updated as well from 2.6 to 2.8.

Fixed the new path and re-sync gradle removed the warning.

Another way is to use gradlewrapper.

IronBlossom
  • 3,898
  • 3
  • 35
  • 42
0

EDIT: This how i finally solved this problem...

  1. Navigate in Android Studio to:

    File | Settings | Build, Execution, Deployment | Build Tools | Gradle

  2. Ensure that this is unchecked, despite that it is recommended to keep ...

    Use Default Gradle Wrapper (recommended)

  3. and that this one is checked ...

    Use local gradle distribution

  4. and that Gradle Home is set to this, with possibly a later gradle-n.n ...

    C:/Program Files/Android/Android Studio1/gradle/gradle-2.4

You may have to restart Android Studio, rebuild the project, etc but the problem is solved.

End of EDIT.

I am posting this to advise anyone else new to this problem to ignore it. The project runs perfectly regardless. This is quite simply a bug in the build system, hopefully to be fixed soon. I posted a comment and voted here where it is again an active topic ...

https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=76719

After trying, unsuccessfully, the suggestions in the following three threads, i re-made the entire project from scratch. I started a new Android Studio project, copied all of the original libs and main files into it ignoring any files which are generated by the build system and rebuilt the project. Everything was fine except all those "cannot be applied to '(groovy.lang.Closure)'" warnings re-appeared with a vengence. Then i went thru these suggestions again just to be certain that i had not overlooked something.

1 buildTypes cannot be applied to groovy.lang.Closure go to the settings within Android Studio and select "Use gradle wrapper": In Android Studio select: File\Settings\Build, Execution, Deployment\Build tools\Gradle Mark: Use default gradle wrapper (default) This removed all 'cannot be applied to '(groovy.lang.Closure') warnings in the build files.

2 'dependencies' cannot be applied to '(groovy.lang.Closure)' Open and edit file: yourproject/gradle/wrapper/gradle-wrapper.propertie. Edit content as below image then save. Delete this folder: yourproject/.gradle. Click Sync project with gradle files, then you are good to go.

3 https://github.com/DrKLO/Telegram/issues/870

My configuration is: Android Studio 1.3, compileSdk Version 22, buildTools Version "22.0.1"

Community
  • 1
  • 1
Mick
  • 663
  • 1
  • 9
  • 18
0

delete gradle folder under the app folder and resync project

Alex Perevozchykov
  • 2,211
  • 22
  • 19
0

All I had to do to remove

productFlavors {
}

From

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

and the warning went away.

Final look was like this

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
Atiq
  • 14,435
  • 6
  • 54
  • 69
0

Changing the buildToolsVersion from

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2" 
.. 
}

to

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1" 
.. 
}

worked for me.

Draken
  • 3,134
  • 13
  • 34
  • 54