错误:Gradle 'HelloWorld'项目刷新失败:构建脚本错误,发现不支持的Gradle DSL方法:'setRoot()'!

4

大家好,我正在使用 gradleAndroidStudio 来测试我的安卓应用。我使用了这个框架 RoboLectric 来完成所有工作。

但是当我将其与 gradle 文件同步时,它给我报错了。各位大佬们,能否分享一下解决该问题的看法?

Gradle settings
3:35:14 PM Gradle 'HelloWorld' project refresh failed:
Build script error, unsupported Gradle DSL method found: 'setRoot()'!
Possible causes could be:  
- you are using Gradle version where the method is absent 
- you didn't apply Gradle plugin which provides the method
- or there is a mistake in a build script
Gradle settings

在app文件夹中,有一个build.gradle文件,这就是我现在拥有的文件。
apply plugin: 'android'
apply plugin: 'android-test'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

sourceSets {
    instrumentTest.setRoot('src/test')
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile 'junit:junit:4.10'
    testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
    testCompile 'com.squareup:fest-android:1.0.+'
    instrumentTestCompile 'junit:junit:4.10'
    instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
    instrumentTestCompile 'com.squareup:fest-android:1.0.+'
}

在 HelloWorld 中,还有一个 build.gradle 文件,我在其中有以下内容:
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
        classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

错误信息显示“找到不支持的Gradle DSL方法:'setRoot()'”,而您的构建脚本包含“instrumentTest.setRoot('src/test')”。这就是我开始查找的地方。 - Peter Niederwieser
我明白了,你能稍微解释一下如何修复吗? - N Sharma
我认为sourceSets声明需要放在android {...}内部,就像Gradle Android插件文档中所解释的那样。普通的Gradle源集(即在android块之外的源集)没有setRoot方法。 - Peter Niederwieser
@PeterNiederwieser 谢谢,这对我有用。 - N Sharma
你的Gradle代码在哪里指定了instrumentTest.setRoot? - IgorGanapolsky
@IgorGanapolsky 我已经得到Peter的帮助解决了,见下面我的回答。 - N Sharma
1个回答

7

正如Peter在评论中建议的那样,将sourceSets声明在android{...}内部。

这对我很管用。

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接