Android Studio Robolectric缺少android.R

时间:2014-09-13 15:12:46

标签: android android-studio

我很难在Android工作室解决这个问题。

我已经制作了一个应用程序,并试图用这个Robolectric测试工具测试它,但似乎robolectric无法找到android jar文件虽然android jar运行完美,当我自己运行应用程序。我的朋友在他的计算机上有一个测试和应用程序的克隆,在这里我完美地运行。我错了什么?

请帮帮我。

这是失败:

java.lang.NoClassDefFoundError:android / R.     在org.robolectric.bytecode.Setup。(Setup.java:39)     在org.robolectric.RobolectricTestRunner.createSetup(RobolectricTestRunner.java:137)     在org.robolectric.RobolectricTestRunner.createSdkEnvironment(RobolectricTestRunner.java:114)     在org.robolectric.RobolectricTestRunner $ 3.create(RobolectricTestRunner.java:307)     在org.robolectric.EnvHolder.getSdkEnvironment(EnvHolder.java:21)     在org.robolectric.RobolectricTestRunner.getEnvironment(RobolectricTestRunner.java:305)     在org.robolectric.RobolectricTestRunner.access $ 300(RobolectricTestRunner.java:61)

这是app gradle文件:

    apply plugin: 'com.android.application'
    apply plugin: 'robolectric'
    apply plugin: 'idea'

    android {
        compileSdkVersion 18
        buildToolsVersion '19.1.0'

    defaultConfig {
        applicationId "com.kea.project.wheeloffortune"
        minSdkVersion 16
        targetSdkVersion 18

        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }

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

    sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }
}
buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
    }
}

    dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile files('libs/commons-lang3-3.3.2.jar')
    compile files('libs/picasso-2.2.0.jar')
    compile files('libs/volley.jar')
    compile files('libs/espresso-1.1.jar')
    compile files('libs/testrunner-1.1.jar')
    compile files('libs/testrunner-runtime-1.1.jar')
    androidTestCompile files('lib/espresso-1.1.jar')
    androidTestCompile files('lib/testrunner-1.1.jar')
    androidTestCompile files('lib/testrunner-runtime-1.1.jar')
    androidTestCompile 'com.google.guava:guava:14.0.1'
    androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile('org.robolectric:robolectric:2.3') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }

    androidTestCompile 'com.squareup:fest-android:1.0.+'
}

idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}

task addTest {
    def src = ['src/test/java']
    def file = file("app.iml")

    doLast {
        try {
            def parsedXml = (new XmlParser()).parse(file)
            def node = parsedXml.component[1].content[0]
            src.each {
                def path = 'file://$MODULE_DIR$/' + "${it}"
                def set = node.find { it.@url == path }
                if (set == null) {
                    new Node(node, 'sourceFolder', ['url': 'file://$MODULE_DIR$/' + "${it}", 'isTestSource': "true"])
                    def writer = new StringWriter()
                    new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)
                    file.text = writer.toString()
                }
            }
        } catch (FileNotFoundException e) {
            // nop, iml not found
        }
    }
}

gradle.projectsEvaluated {
    preBuild.dependsOn(addTest)
}

robolectric {
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'
}

0 个答案:

没有答案