23

Error after resolutionproject is staymax2 build.gradle files that i am working on To add V4 support libraries to android studio, i followed this document:https://developer.android.com/tools/support-library/setup.html#libs-without-res but I get an error. Here is what i did

  1. SDK manager> Installed Android Support Library and Android Repository.
  2. Go to Build.Gradle and added the line as given in the dcoument. Build.Gradle now looks like this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.13.2'

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

allprojects {
repositories {
    jcenter()
    dependencies {

        compile "com.android.support:support-v4:18.0.+"
    }
}

}

Then, I get a popup that suggest that I sync gradle. When i sync Gradle, i get this error:

Error:(20, 0) Gradle DSL method not found: 'compile()' Possible causes:

  • The project 'staymax' may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
  • The build file may be missing a Gradle plugin. Apply Gradle plugin
  • Am i missing any step? Please suggest.

    Build.Gradle(app)

    apply plugin: 'com.android.application'
    
    android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"
    
    defaultConfig {
        applicationId "com.appt.shreyabisht.staymax"
        minSdkVersion 15
        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'])
    }
    
    shrbisht
    • 676
    • 3
    • 9
    • 23
    • Generally you declare dependencies on an individual module, rather than in the top level build file. Are you sure you want to add this to every module in your project? – ianhanniballake Nov 26 '14 at 19:13

    10 Answers10

    37

    In almost all cases, your dependencies should be put into the individual module's build.gradle files rather than at the top most level build.gradle file. In your case, that means the dependency should be added to the app module's build.gradle file:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile "com.android.support:support-v4:18.0.+"
    }
    

    And you should remove the entire allprojects part of the top level build.gradle.

    ianhanniballake
    • 191,609
    • 30
    • 470
    • 443
    • "In almost all cases, your dependencies should be put into the individual module's build.gradle files rather than at the top most level build.gradle file." – shrbisht Nov 26 '14 at 19:51
    • I have two build.gradlevfiles under Gradle Scripts. One is build.gradle(app) and other is build.gradle(staymax). Staymax is the project i am working on and I assumed that build.gradle was present by default. If you mean that I need to update a gradle file which is specific to the module that im working on ( I translate that as the test project i am working on ) , then i should be making changes to build.gradle(staymax) and not build.gradle(app). I will add a screenshot to make more sense of all this. Thanks. – shrbisht Nov 26 '14 at 19:51
    • staymax is the top most level build.gradle file (hence why the comment on the top of the file is `// Top-level build file where you can add configuration options common to all sub-projects/modules.` - `app` is the build.gradle for your app itself and is where you want to do everything in. – ianhanniballake Nov 26 '14 at 19:53
    • ok so what i understand now is . 1. build.grable(staymax) is the top most level and i should remove the entire allprojects{} from it. 2. I have another build.gradle(app) which is individual module's build.gradle and i should edit the dependencies{} to dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile "com.android.support:support-v4:18.0.+" } Did i get it right? – shrbisht Nov 26 '14 at 20:00
    • Good God!!! Thank you!!!!!!! So I did all that and it synched alright. Although its did say that "this support library should not use a lower version(18) than the targetversion(20) SDk." Is this still ok or do i need to make any changes? – shrbisht Nov 26 '14 at 20:05
    • Changing your `18` to `20` is definitely a good idea, although you might consider also updating your target SDK version and support library to the latest: `21` to take advantage of the Android 5.0 Material Design support added in [AppCompat v21](http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html) - if you are following tutorials though, they are probably still using 20 though. – ianhanniballake Nov 26 '14 at 20:08
    • changed to 20. synced. and yes on that. Following tutorials, so ill just keep it 20 for a few test projects. Thank you thank you thank you for not just throwing the solution but also doing a little bit of explaining. Thank you . I will move on to new world of errors now. – shrbisht Nov 26 '14 at 20:13
    • soon after we resolved this problem, I went on to create new java files but i found that my older xmls were not sort of corrupted. When i clicked on design, i got errors like missing styles. Whats more is that android wont recognize the string that i had already added in the string resource. I was wondering if it has got anything to do with the changes we made in the build.gradle. I ran the project few hours ago and i was running. I am attaching a snapshot too. – shrbisht Nov 26 '14 at 21:12
    • could it be because i changed to 20? In the screenshot i can see im working on material design now ( the buttons resemble the one in android L) – shrbisht Nov 26 '14 at 21:15
    • Some of the helper methods such as adding a new activity will automatically include the latest version of the support libraries (which require compiling against the latest SDKs) - there's no harm in always compiling against the latest, but look through your dependencies for any version 21s and change them to 20s as a short term solution – ianhanniballake Nov 26 '14 at 22:23
    12

    I have found that when I add an applicationSuffix or versionNameSuffix through the IDE menu (Build > Edit Build Types), it changes the dependencies section of my app build.gradle from this:

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        testCompile 'org.json:json:20140107'
    
        compile "com.android.support:appcompat-v7:${supportLibVersion}" 
        compile "com.android.support:design:${supportLibVersion}" 
        compile "com.android.support:support-vector-drawable:${supportLibVersion}" // VectorDrawableCompat
        compile "com.android.support:animated-vector-drawable:${supportLibVersion}" // AnimatedVectorDrawableCompat
    }
    

    to this:

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        testCompile 'org.json:json:20140107'
        compile "com.android.support:appcompat-v7:${supportLibVersion}" compile "com.android.support:design:${supportLibVersion}" compile "com.android.support:support-vector-drawable:${supportLibVersion}"
        // VectorDrawableCompat
        compile "com.android.support:animated-vector-drawable:${supportLibVersion}"
        // AnimatedVectorDrawableCompat
    }
    

    I don't know why, but it combines the first three "compile" lines into one line, and moves the two comments (to the next line in each case).

    I solved the problem by editing the app build.gradle and putting each "compile" statement onto its own line.

    C. Todd
    • 468
    • 1
    • 5
    • 11
    • Thank you so much, I resolved thanks to your tip. I don't know why AS decided to reformat the gradle file but it broke totally my project build. Now is ok :-) – Lisitso Jul 21 '16 at 10:17
    • Hey thank you that bloody IDE combined two lines in one in my case. – Mat Nov 03 '16 at 16:03
    • It literally did this with 9 consecutive lines in my build.gradle. Thanks a ton. – mDroidd Jan 18 '17 at 12:32
    • Thank you! After 2 hours trying to understand what was going on, found you post. AS... today was very annoying! – Lu Araujo Jun 02 '17 at 19:40
    2

    This worked for me:

    1. Exit Android Studio.
    2. Remove <home folder>/.gradle folder.
    3. Relaunch Android studio and let it load all gradle modules again.

    P.S.: Mine was a fresh project so I removed the project as well and created a new one, so there were no old gradle references from the project

    sschale
    • 5,168
    • 3
    • 29
    • 36
    2

    In my case, there are 2 build.gradle file in my project (it auto create by android studio when create new project)

    Just move the compile line to another one is solve my problem

    Wolf
    • 6,361
    • 2
    • 28
    • 25
    1

    You have 2 different build.gradle (sometimes more depending on you)

    Only one of them is your app's gradle, then the other one is project's gradle.

    If you put your dependencies project's gradle you can get error : Gradle DSL method not found: 'compile()'

    Put your dependencies in app's build.gradle then it will work.

    Yasin Kaçmaz
    • 6,573
    • 5
    • 40
    • 58
    1

    Hi everyone for me it was a "couple days consuming job" to make my app run in Android Studio . 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 .
    6. 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

    Just delete the method.

    //DELETE THIS LINES:
    android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
    }
    

    Try to compile again. It should work now.

    Source : https://medium.com/@marcuspereira/solving-the-gradle-dsl-method-not-found-android-in-android-studio-6e5ab499bd3#.u1lxecq41

    Kamaro
    • 955
    • 1
    • 10
    • 11
    1

    I have the same issue, and the bug is my build.gradle of application level has dependencies of my submodules. Means

    dependencies {
       compile project(':library')
      compile project(':MPChartLib')
    }
    

    I have just remove the two line inside the dependencies braces and it is able to start syncing.

    shailesh
    • 1,783
    • 2
    • 17
    • 26
    1

    Deleting .gradle in my project folder did the job for me.

    Samir
    • 3,139
    • 2
    • 17
    • 24
    1

    Make sure you have a settings.gradle file in your project. Adding that solved the problem of Error:Gradle DSL method not found: 'minSdkVersion()' for me.

    Ghanendra
    • 363
    • 6
    • 15