Note that there are some explanatory texts on larger screens.

plurals
  1. PORobolectric with Gradle: Resources not found
    primarykey
    data
    text
    <p>I'm trying to run my Robolectric tests together with the new Gradle Android build system, but I'm stuck at accessing the resources of my main project.</p> <p>I split the build into two separate projects to avoid conflicts between the <code>java</code> and the <code>android</code> gradle plugins, so the directory structure looks roughly like this:</p> <pre><code>. ├── build.gradle ├── settings.gradle ├── mainproject │   ├── build │   │   ├── classes │   │   │   └── debug │   ├── build.gradle │   └── src │   └── main │   ├── AndroidManifest.xml │   └── ... └── test ├── build.gradle └── src    └── test    └── java    └── ...    └── test    ├── MainActivityTest.java    ├── Runner.java    ├── ServerTestCase.java    └── StatusFetcherTest.java </code></pre> <p>My <code>build.gradle</code> in <code>test/</code> currently looks like this:</p> <pre><code>buildscript { repositories { mavenCentral() } dependencies { classpath 'com.stanfy.android:gradle-plugin-java-robolectric:2.0' } } apply plugin: 'java-robolectric' repositories {...} javarob { packageName = 'com.example.mainproject' } test { dependsOn ':mainproject:build' scanForTestClasses = false include "**/*Test.class" // Oh, the humanity! def srcDir = project(':mainproject').android.sourceSets.main.java.srcDirs.toArray()[0].getAbsolutePath() workingDir srcDir.substring(0, srcDir.lastIndexOf('/')) } project(':mainproject').android.sourceSets.main.java.srcDirs.each {dir -&gt; def buildDir = dir.getAbsolutePath().split('/') buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/') sourceSets.test.compileClasspath += files(buildDir) sourceSets.test.runtimeClasspath += files(buildDir) } dependencies { testCompile group: 'com.google.android', name: 'android', version: '4.1.1.4' testCompile group: 'org.robolectric', name: 'robolectric', version: '2.0-alpha-3' ... } </code></pre> <p>The evil classpath hackery allows me to access all classes of my main project, except for <code>R</code>, which exists as <code>.class</code> file in the build directory, but raises this error during the <code>compileTestJava</code> task:</p> <pre><code>/.../MainActivityTest.java:16: error: cannot find symbol final String appName = activity.getResources().getString(R.string.app_name); ^ symbol: variable string location: class R 1 error :test:compileTestJava FAILED </code></pre> <p>There must be a better way to execute Robolectric tests with the new build system, right?</p> <p>(<a href="https://github.com/passy/simstatus/tree/gradle">Full source of the app</a>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload