Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NoClassDefFoundError:。。。R$styleable在将Robolectric与Android库(AAR)结合使用时可用_Java_Android_Robolectric - Fatal编程技术网

Java NoClassDefFoundError:。。。R$styleable在将Robolectric与Android库(AAR)结合使用时可用

Java NoClassDefFoundError:。。。R$styleable在将Robolectric与Android库(AAR)结合使用时可用,java,android,robolectric,Java,Android,Robolectric,我们正在开发一个提供活动的库,我希望能够对该活动执行一些功能测试 我的build.gradle包括: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' testCompile 'org.mockito:mockito-core:1.+' testCompile 'junit:junit

我们正在开发一个提供活动的库,我希望能够对该活动执行一些功能测试

我的
build.gradle
包括:

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

    testCompile 'org.mockito:mockito-core:1.+'
    testCompile 'junit:junit:4.12'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    testCompile('org.robolectric:shadows-httpclient:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    testCompile('org.robolectric:shadows-support-v4:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
}
@RunWith(RobolectricGradleTestRunner.class)
public class LibraryActivityTest {

    @Test
    public void testOnCreate() throws Exception {
        LibraryActivity libraryActivity = Robolectric.buildActivity(LibraryActivity.class).create().get();


    }
}
我的
图书馆活动测试包括:

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

    testCompile 'org.mockito:mockito-core:1.+'
    testCompile 'junit:junit:4.12'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    testCompile('org.robolectric:shadows-httpclient:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    testCompile('org.robolectric:shadows-support-v4:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
}
@RunWith(RobolectricGradleTestRunner.class)
public class LibraryActivityTest {

    @Test
    public void testOnCreate() throws Exception {
        LibraryActivity libraryActivity = Robolectric.buildActivity(LibraryActivity.class).create().get();


    }
}
MyRobolectricGradleTestRunner
覆盖
getAppManifest
以恢复指定特定于测试的AndroidManifest.xml而不是使用应用程序版本的能力

当我试图从Android Studio或gradle命令行运行
testOnCreate
时,我得到了
Android/support/v7/appcompat/R$styleable的class def not found错误:

android/support/v7/appcompat/R$styleable
java.lang.NoClassDefFoundError: android/support/v7/appcompat/R$styleable
    at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:109)
    at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
    at com.moxiesoft.mylibrary.LibraryActivity.onCreate(LibraryActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:5133)
    at org.robolectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:144)
    at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:241)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:122)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:309)
    at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:47)
    at org.robolectric.util.ActivityController.create(ActivityController.java:118)
    at org.robolectric.util.ActivityController.create(ActivityController.java:129)
    at com.moxiesoft.mylibrary.LibraryActivityTest.testOnCreate(LibraryActivityTest.java:20)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:235)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
还值得注意的是,安装和配置似乎是合理的,因为不涉及
ActivityController.create的测试似乎工作正常


如何解决此错误?

因此问题的根源似乎是gradle如何(或不)处理为
com.android.library
模块创建
android/support/v7/appcompat/R.java

有了这些线索,我可以通过将这段代码放入我的
build.gradle

def package_namespace = "com.moxiesoft.netagent.androidchatcustomerclient"
def package_namespace_path = package_namespace.replaceAll("\\.", "/")

afterEvaluate { project ->
    android.libraryVariants.each { variant ->
        // workaround for missing R class for aar dependencies
        def copyTaskName = "copy${variant.name.capitalize()}AppCompat"
        task(copyTaskName, type:Copy) {
            dependsOn "process${variant.name.capitalize()}Resources"
            from "build/generated/source/r/${variant.name}/$package_namespace_path"
//            into "build/generated/source/r/${variant.name}/android/support/v7/appcompat"
            into "src/test/java/android/support/v7/appcompat"
            include 'R.java'
            filter { line -> line.contains("package ${package_namespace};") ? 'package android.support.v7.appcompat;' : line }
            outputs.upToDateWhen { false }
        }
        tasks.getByName("compile${variant.name.capitalize()}UnitTestJava") dependsOn copyTaskName
    }
}

基本上,它只是将生成的应用程序R.java文件复制到tests build文件夹中,以便在测试编译阶段获取它。我尝试将副本放入generated sources文件夹,但测试编译阶段似乎没有在那里找到它。

David,我认为最新的3.0-SNAPSHOT修复了库清单和Robolectric测试的问题。因此,您不需要自定义测试运行程序是的,一旦我更改为gradle 1.2,就不再需要自定义运行程序,但实际问题仍然存在。但是您尝试过robolectric 3.0-SNAPSHOT吗?gradle 1.2和3.0-rc2不需要自定义测试运行程序。3.0-SNAPSHOT仍然存在
java.lang.NoClassDefFoundError
。对于我来说,最好更新test.java.srcdir,因为也找不到其他第三方库。下面为我修复了一切:sourceSets{test.java.srcDirs+='../App/build/generated/source/r/debug'}