NoClassDefFoundError: ... R $ style when using Robolectric with Android Library (AAR)

We are developing a library that provides an Activity and I would like to be able to do some functional testing in an Activity.

Mine build.gradle

includes:

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

      

And mine LibraryActivityTest

includes:

@RunWith(RobolectricGradleTestRunner.class)
public class LibraryActivityTest {

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


    }
}

      

MyRobolectricGradleTestRunner

overrides getAppManifest

to restore the ability to specify a specific AndroidManifest.xml for testing, instead of using the app version.

When I try to run testOnCreate

from the Android Studio or gradle command line, I get a def not found class error for android/support/v7/appcompat/R$styleable

:

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)

      

It's also worth noting that the installation and configuration seems to be reasonable as tests that don't include ActivityController.create

seem to work fine.

How can I resolve this error?

+3


source to share


1 answer


So, the root of the problem seems to be how gradle handles (or doesn't) create android/support/v7/appcompat/R.java

for com.android.library

modules.

With hints here I managed to get it working from both command line and Android by putting this piece in mybuild.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
    }
}

      

Basically it is just copying the generated R.java file into the test build folder so that it gets picked up by the test compilation phase. I tried putting a copy in the generated sources folder, but the test compilation phase doesn't seem to pick it up there.

+1


source







All Articles