android - 错误 : Gradle 'HelloWorld' project refresh failed: Build script error, 找到不支持的 Gradle DSL 方法 : 'setRoot()' !

标签 android gradle android-studio robolectric

您好,我正在使用 AndroidStudio 中的 gradle 测试我的 android 应用程序。我正在使用这个框架 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()
    }
}

最佳答案

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

它对我有用。

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')
    }
}

关于android - 错误 : Gradle 'HelloWorld' project refresh failed: Build script error, 找到不支持的 Gradle DSL 方法 : 'setRoot()' !,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22297283/

相关文章:

android - 在简历上重新加载 Assets

android - 导入项目(Eclipse ADT,Gradle等)选项在Android Studio 2.3.2中不会退出

android - 无法同步Gradle Android Studio 4.0

android - 使用 Gpt 声明依赖项时,Gpt 和 Gradle 中的实现有什么区别?

java - 我的 Android 应用程序上有 34 个按钮。有什么方法可以避免为每个人编写 onclicklistener 吗?

android - 在 Canvas 上垂直绘制文本

java - 从 OpenAPI 3 生成 Java Spring API

java - 从 Android 联系人列表中的联系人获取家庭位置?

android - 带有实验性 gradle 0.2.0 的 Android studio 中的 NDK

android - 在 Android 中发送和接收短信和彩信(Kit Kat Android 4.4 之前)