Deckard is the simplest possible Android project that uses Robolectric for testing and Gradle to build. It has one Activity, a single Robolectric test of that Activity, and an Espresso test of that Activity.
With just a bit of manual intervention, Deckard also imports into IntelliJ and Android Studio, due to their support for gradle. See below for instructions.
Note: These instructions assume you have a Java 1.6 JDK installed.
To start a new Android project:
-
Install the Android SDK. On Mac OS X with Homebrew just run
brew install android-sdk
-
Set your
ANDROID_HOME
environment variable to/usr/local/opt/android-sdk
. -
Run the Android SDK GUI and install API 18 and any other APIs you might need. You can start the GUI by invoking
android
-
Download Deckard from GitHub:
wget https://github.com/robolectric/deckard-gradle/archive/master.zip unzip master.zip mv deckard-master my-new-project
-
In the project directory you should be able to run the Robolectric tests:
cd my-new-project ./gradlew clean test
-
You should also be able to run the Espresso tests:
./gradlew clean connectedAndroidTest
. Note: Make sure to start an Emulator or connect a device first so the test has something to connect to. -
Change the names of things from 'Deckard' to whatever is appropriate for your project. Package name, classes, and the AndroidManifest are good places to start.
-
Build an app. Win.
Currently we believe only IntelliJ EAP build 135.666 or later and Android Studio 0.5.0 or later will work with the tooling that deckard-gradle depends on.
Import the project into IntelliJ or Android Studio by selecting 'Import Project' and selecting the project's build.gradle
. When prompted, you can just pick the default gradle wrapper.
For both IntelliJ IDEA and Android Studio, you will also need to change the classpath order for you dependencies. Otherwise you will see the dreaded Stub!
exception:
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
- For Intellij, go to Project Structure -> Modules -> deckard-gradle pane. In the Dependencies tab, move the Module SDK dependency (i.e. Android API 19 Platform) to be the last item in the list.
- For Android Studio, dependency ordering is currently not modifiable via any GUI. Therefore, you must modify the project iml file directly as such and reload the project:
<orderEntry type="library" exported="" scope="TEST" name="wagon-provider-api-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xercesMinimal-1.9.6.2" level="project" />
<orderEntry type="jdk" jdkName="Android API 19 Platform" jdkType="Android SDK" /> <---make sure this is the last orderEntry
</component>
</module>
NOTE: Android Studio aggressively re-writes your dependencies list (your .iml file) and subverts the technique used above to get the Android SDK to the bottom of the classpath. You will get the dreaded Stub! exception every time you re-open the project (and possibly more often). For this reason we currently recommend IntelliJ; we hope this can be solved in the future.
Gradle is now in charge of compilation, but IntelliJ still launches the test runner. So in order for IntelliJ to know where to find compiled classes, you have to tell it. This manual step will hopefully go away soon, but for now it's necessary:
- Go to Project Structure -> Modules -> deckard-gradle -> Paths.
- The value for 'Output path' should be filled in, but 'Test output path' will not be. Copy the text that's in 'Output path', paste into 'Test output path', but change the final 'build/intermediates/classes/debug' to 'build/test-classes'. This is because the gradle android test plugin currently dumps all compiled test output (for all variants) into the same directory. This means that currently variants are not fully supported.
The above trick doesn't work for Android Studio, since that part of the module configuration GUI has been ripped out of the IDE. According to this thread on the Robolectric google group, some people have managed to get Android Studio to find the classes compiled by gradle.
You should now be able to DeckardActivityRobolectricTest
. Run it as a normal JUnit test - make sure to choose the JUnit test runner and not the Android one.
To run the Espresso test, you need to set up a Run Configuration. Go to Edit Configurations -> Defaults -> Android Tests
and, after choosing the correct module (deckard-gradle), fill in the Specific instrumentation test runner
field. The easiest way is to click the ellipsis button on the right and type in GITR
. This will find GoogleInstrumentationTestRunner
, which is what you want. The fully-qualified class name will appear. Now you can right click on the test method in DeckardEspressoTest
and choose the Android test runner.