NoClassDefFoundError:...使用Robolectric与Android库(AAR)时R $ styleable

时间:2015-05-11 20:36:17

标签: java android robolectric

我们正在开发一个提供Activity的库,我希望能够对Activity进行一些功能测试。

我的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'
    }
}

我的LibraryActivityTest包括:

@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的类def 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的测试似乎工作正常。

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

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

通过here提供的线索,我可以通过将这个块放在我的build.gradle

中,从命令行和Android中获得帮助。
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文件夹中,以便在测试编译阶段获取它。我尝试将副本放入生成的sources文件夹中,但是测试编译阶段似乎并没有把它复制到那里。