1

I am new to android programming, when I try to build my first project, I get the following error -

Gradle sync failed: Could not find method android() for arguments [build_ed74sxwq3zd69j7x0p9x4y5fb$_run_closure3@3d54d64f] on root project 'MyApplication' of type org.gradle.api.Project.

// 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:2.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

android {
    compileSdkVersion 19
    buildToolsVersion '25.0.2'
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
}
dependencies {compile files('app/libs/junit-4.12-JavaDoc.jar')
}
apply plugin: 'maven'

The app/build/gradle file is -

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.surabhi.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
    compile files('app/libs/junit-4.12-JavaDoc.jar') }
    apply plugin: mavin
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
surabhi gupta
  • 65
  • 1
  • 1
  • 9

3 Answers3

3

You are using the wrong build.gradle file.

You can't define the android block in the top-level file.

You have to remove this part in your

android {
    compileSdkVersion 19
    buildToolsVersion '25.0.2'
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
}
dependencies {compile files('app/libs/junit-4.12-JavaDoc.jar')
}
apply plugin: 'maven'
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

i had the same problem in android studio 2.3.3, what i was doing wrong is that my dependencies were in the the wrong build.gradle (this is wrong)enter image description here

The build.gradle that we need is the one with the android option as in the following `apply plugin: 'com.android.application'

android { compileSdkVersion 23 buildToolsVersion '23.0.0'

defaultConfig {
    applicationId "com.movas.mservice12"
    minSdkVersion 9
    targetSdkVersion 23
    versionCode 11
    versionName "2.0"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
lintOptions {
    abortOnError false
}
dexOptions {
   // incremental = true
    preDexLibraries = false
    javaMaxHeapSize "4g" // 2g should be also OK
}

}` Ensue where you are adding the dexOptions is inside the android curly braces.

shadrack Mwangi
  • 785
  • 6
  • 14
0

I got the same error when I added this to the project level gradle file. Move this to the app level gradle file and you should be good.

Roehit Kadam
  • 101
  • 1
  • 11