Gradle DSL method not found: 'compile()'
Asked Answered
W

9

80

I had this gradle error.

Error:(9, 0) Gradle DSL method not found: 'compile()'

I have tried refering to similar questions but it did not work.

Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.

Getting Error "Gradle DSL method not found: 'compile()'" when Syncing Build.Gradle

Unsupported Gradle DSL method found: 'compile()'!

My build.gradle code is here

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        compile 'com.android.support:appcompat-v7:20.+'
        compile 'com.google.android.gms:play-services:6.5.+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

build.gradle(Module.app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.example.simplemaker.pushtest"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

What's wrong with my code?

Waterlogged answered 23/12, 2014 at 9:39 Comment(1)
Possible duplicate of Unsupported Gradle DSL method found: 'compile()'!Potential
A
111

As the note of your project's build.gradle file suggests:

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

Remove the 2 compile statements in that gradle file:

compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:6.5.+'

And then make your other (module's) build.gradle dependencies look like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.+'
}
Altamira answered 23/12, 2014 at 10:10 Comment(2)
What if my android project has no modules?Southing
i ws using com.android.support:support-compat:23.0.0 bt it was com.android.support:support-v4:23.0.0Terrorist
M
35

I am using Android studio based on IntelliJ Idea and I have changed Settings on when and how to wrap code. Also I had 'reformat automatically' options which lead to formatting Gradle files randomly. So it lead to something like this:

    compile 'de.greenrobot:eventbus:2.4.0' compile 'com.jakewharton:butterknife:7.0.1'

Gradle then fails to find compile() for the second compile. As you only allowed to write one dependency per line.

Melisandra answered 1/3, 2016 at 6:19 Comment(7)
This is another possibility why this error can occur @ExuberateMelisandra
Ok, it kinda reads like a question. Didn't want to get it deletedExuberate
Thank you, I just found IDEA really reformatted my Gradle file and made two line in one. You answer works for me.Lucinalucinda
Some extra detail would be handy hereLowe
@ChrisNevill what do you mean?Melisandra
Details about where the settings you mention are in IntelliJ / Android Studio. However I think this is actually a bug - rather than the settings - as I had not changed any settings in my setup.Lowe
That was it, in my case. I had some row containing both "compile" and "annotationProcessor" definitions, just put them in different rows.Takakotakakura
I
31

Its really silly problem and I got solution:

as compile statements goes in one line

compile "com.squareup.okhttp3:okhttp:$okHttpVersion"    compile "com.squareup.okhttp3:okhttp-urlconnection:$okHttpVersion"    compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"    compile "com.google.code.gson:gson:$gsonVersion"

I just changed next by next line and solved my problem:

compile "com.squareup.okhttp3:okhttp:$okHttpVersion"
compile "com.squareup.okhttp3:okhttp-urlconnection:$okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
compile "com.google.code.gson:gson:$gsonVersion"

Hope it will helps you. Thank you.

Ichang answered 3/6, 2016 at 15:59 Comment(6)
Exactly this... for some reason something had pulled my compile statements onto one lineLowe
See this : #36845072Lowe
I've had some success in changing the " to 'Lowe
Where I was using a string I've had some success in doing: compile myString + '' (that's two empty comma's)Lowe
Was facing the same problem and this is the solution!Archicarp
Thanks it solved my problem, what a silly mistake :(Unquestioning
J
8

Check your build.gradle files, sometimes when you add a module to your current module using the project settings, the build.gradle of current module is corrupted, and indents are broken, just check current build.gradle and check if all the compile statements are issued in a new line!

Jobe answered 23/4, 2016 at 7:18 Comment(1)
This is exactly what happened when I updated "signing" configuration in Module Settings.Themselves
S
7

in addition to proper replies already given above - a picture for more detailed explanation:

enter image description here

Project has 2 build.gradle. You need the highlighted one: build.gradle (Module: app)

Slop answered 6/8, 2016 at 9:50 Comment(0)
R
2

Check your project. there is 2 build.gradle.

Move your compile line to another build.gradle

Riesman answered 5/5, 2016 at 7:12 Comment(0)
R
0

Application dependencies must include in app -> build.gradle file. Not in Project -> build.gradle file.

Recalesce answered 28/5, 2017 at 6:55 Comment(0)
F
0

A simple mistake can cause this problem too, don't include classpath dependencies to build.gradle(Module:app)

Ferrite answered 8/7, 2017 at 8:15 Comment(0)
U
0

In my case, I was getting this error because I had a dependency implementation line outside the dependencies block. Something like this:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    ...
  }

implementation 'androidx.media:media:1.1.0'

Note that all implementation calls must be defined within the dependencies block. That's the simple fix.

I hope this helps someone out there.

Merry coding!

Unifilar answered 12/9, 2019 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.