Q:

Robolectric R$color classDefNotFound

I am trying test an android library with the following set up. com.android.tools.build:gradle:1.2.0, org.robolectric:robolectric:3.0-rc2, android studio 1.2-rc-3
In the setup for my test I am making an indirect reference to the R file of another library. The test runs fine from within android studio but running from command line produces the following error.
java.lang.NoClassDefFoundError: robolectricdemo/com/baselibrary/R$color
            at robolectricdemo.com.baselibrary.DemoStaticClass.getDemoResource(DemoStaticClass.java:12)
            at LibraryResourceTest.setUp(LibraryResourceTest.java:25)

            Caused by:
            java.lang.ClassNotFoundException: robolectricdemo.com.baselibrary.R$color
                at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
                at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
                at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
                at org.robolectric.internal.bytecode.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:98)
                ... 2 more

I have created a simplified version of my code at http://github.com/tcw48/robolectricDemo with the important snippets bellow.
The test class lives in the library extendlibrary
RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class LibraryResourceTest {
    private int color;

    @Before
    public void setUp() {
        color = DemoStaticClass.getDemoResource(RuntimeEnvironment.application);
    }

    @Test
    public void testLibraryResource(){
        assertNotNull(color);
    }
}

This static class lives in the library baselibrary.
public class DemoStaticClass {

    public static int getDemoResource(Context context) {
        return context.getResources().getColor(R.color.robo_red);
    }
}

I noticed in the build directory that an R file is only being generated in the package extendlibrary but there is no package for baselibrary. Manually creating the R file in a package called baselibrary causes the test to pass. Any ideas on what I could be doing wrong.

A:

The R class is missing until you will provide one ;) Try if it works for you too when you copy a valid R class to the expected package.
You must change the paths at the copy task to match your package names
afterEvaluate { project ->
  android.libraryVariants.each { variant ->
    // workaround for missing R class for aar dependencies
    tasks.getByName("assemble${variant.name.capitalize()}").dependsOn copyAppcompat

    // workaround for missing compilation of main sources
    tasks.getByName("test${variant.name.capitalize()}") {
        dependsOn "assemble${variant.name.capitalize()}"
    }
  }
}

task copyAppcompat(type: Copy) {
  from 'build/generated/source/r/debug/com/example/core'
  into 'src/test/java/android/support/v7/appcompat'
  include 'R.java'
  filter { line -> line.contains('package com.example.core;') ? 'package android.support.v7.appcompat;' : line }
}

Here is an example project which depends on other libraries (aar) https://github.com/nenick/AndroidStudioAndRobolectric/blob/library-with-aar
And here the example build.gradle https://github.com/nenick/AndroidStudioAndRobolectric/blob/library-with-aar/core/build.gradle#L42

