Gradle DSL method not found: 'compile()'

Android

Android Problem Overview


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.

https://stackoverflow.com/questions/26851230/android-gradle-build-error9-0-gradle-dsl-method-not-found-compile

https://stackoverflow.com/questions/27156428/getting-error-gradle-dsl-method-not-found-compile-when-syncing-build-grad

https://stackoverflow.com/questions/24730117/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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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!

Solution 5 - Android

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)

Solution 6 - Android

Check your project. there is 2 build.gradle.

> Move your compile line to another build.gradle

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionT.AkashiView Question on Stackoverflow
Solution 1 - AndroidSimasView Answer on Stackoverflow
Solution 2 - AndroidBato-Bair TsyrenovView Answer on Stackoverflow
Solution 3 - AndroidPratik ButaniView Answer on Stackoverflow
Solution 4 - AndroidMohammadRezaView Answer on Stackoverflow
Solution 5 - AndroidAlexeev ValeriyView Answer on Stackoverflow
Solution 6 - AndroidGrey WolfView Answer on Stackoverflow
Solution 7 - AndroidTharangaView Answer on Stackoverflow
Solution 8 - AndroidEge KuzubasiogluView Answer on Stackoverflow
Solution 9 - AndroidTaslim OseniView Answer on Stackoverflow