58

I am getting the following build error when I try and sync my project:

Error:(9, 0) Gradle DSL method not found: 'compile()'
Possible causes:The project 'AlexTest' may be using a version of Gradle that does not contain the method.
The build file may be missing a Gradle plugin.
link: Apply Gradle plugin

I have tried applying every single gradle plugin they link me to in that link on the bottom, yet same issue, so I conclude that the first error is the cause.

Here is the build.gradle file for AlexTest (the project directory):

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

allprojects {
    repositories {
        jcenter()
    }
}

I think that was the gradle file it was having trouble with. But I'm not sure what method it is referring to.

Also here is the gradle-wrapper.properties which it also referred to:

#Mon Nov 10 01:06:12 PST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip

perhaps the gradle version in the distributionUrl needs to match the one in the dependency?

I also have a build.gradle file in the app directory itself - 1 level lower, though I don't think that is what it was referring to, but here it is:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.snappiesticker.alextest"
        minSdkVersion 16
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:6.1.+'
}
BigBoy1337
  • 4,735
  • 16
  • 70
  • 138

5 Answers5

78

I have tried applying every single gradle plugin they link me to in that link on the bottom, yet same issue, so I conclude that the first error is the cause.

Correct.

Here is the build.gradle file for AlexTest (the project directory):

You will notice that this file contains a code comment:

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

Remove the compile 'com.google.android.gms:play-services:6.1.11' line from that file. Leave the compile 'com.google.android.gms:play-services:6.1.+' that you have in the other build.gradle file.

The dependencies closure in buildscript is for Gradle plugins only. The top-level dependencies closure, found in the module's build.gradle file, is for application dependencies.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    @elron: If it makes you feel better, IIRC, the Gradle plugin system is being overhauled, and so I hope eventually the dual-`buildscript` system will be fixed. It's seriously confusing to explain to people, "yes, you have to modify `buildscript`, but not just *any* `buildscript`, but rather the *right* `buildscript`. – CommonsWare Feb 21 '15 at 01:12
  • 1
    The same issue happened to me when two compile commands ended up on the same line. When I split them up into two lines the problem disappeared. –  May 07 '16 at 21:21
13

Saw reports that the problem occurred for other reasons, but this solved for me.

If you carry out any changes in the Project Structure (Android Studio) and press OK, Gradle will be synchronized, but this type of synchronization, if the dependency block has something like:

This block will stay this way after synchronization:

One way to solve the problem is to insert a line break to stay as it was before the synchronization or put a semi-colon between the two statements.

I hope it helps.

  • The "multiple dependencies merging into one line" somehow happened between finishing dev one day, and starting the next. All the errors were vague and unhelpful. Putting them back onto one line each instantly fixed it all. Good job! – Jake Lee Jun 04 '17 at 10:31
3

Just add foolwoing statement in your dependencies

apply plugin: 'jetty'

androidwifi
  • 1,009
  • 10
  • 8
1

Hi everyone for me it was a "couple days consuming job" to make my app run in Android Studio (I migrated from Eclipse and had this problem too ) . Finally I found that very simple way of it .

  1. Create libs folder under src/main/java/ it is App/java/libs in left pane .
  2. Copy and paste all your external jars into here.
  3. Goto left pane and right click on your App then click Open Module Settings
  4. Then Project Structure window will appear .
  5. Then move to Dependencies tab . Final Step : Add all your jars located in App/java/libs (You will find them in src/main/java/libs) one by one .

That is all Enjoy it.

katmanco
  • 1,146
  • 12
  • 24
1

Declare dependencies in Module's build.gradle file, not in AlexTest's build.gradle file

enter image description here

Biswajit Karmakar
  • 9,799
  • 4
  • 39
  • 41