Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build script error, unsupported Gradle DSL method found: 'android()'!

I'm using Android Studio 0.4.5 and having troubles syncing gradle.

When I try to do that I get this error:

Gradle 'MyApp' project refresh failed: Build script error, unsupported Gradle DSL method found: 'android()'!

My solution contains 4 modules. Here is my root build.graddle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

android {    
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

And the others (I removed dependencies for simplicity)

Module 1

apply plugin: 'android'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

Module 2

apply plugin: 'android-library'

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
    }

    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

Module 3 apply plugin: 'android-library'

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
    }

    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

Module 4

apply plugin: 'android-library'

android {
    compileSdkVersion 17
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17
    }

    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

Sorry for making this so long, but I'm getting desperate here :(

like image 711
Jacek Kwiecień Avatar asked Feb 19 '14 12:02

Jacek Kwiecień


2 Answers

The main reason was having this:

android {    
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
}

in the root build.gradle.

like image 112
Jacek Kwiecień Avatar answered Oct 16 '22 20:10

Jacek Kwiecień


Remove following lines of code from Module1 build.gradle file :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

As you are using the same configuration across all your modules, so it is fine to have it in root gradle file only.

Even if you want it in module's build.gradle file this code should be before applying android plugin.

Final Module1 build.gralde file :

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

Also make sure below mentioned configuration should be same across the modules

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }
 }

You can use whatever you want but should be same.

like image 24
Piyush Agarwal Avatar answered Oct 16 '22 20:10

Piyush Agarwal