Build script error, unsupported Gradle DSL method

2019-01-26 15:06发布

问题:

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 :(

回答1:

The main reason was having this:

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

in the root build.gradle.



回答2:

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.