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.
Deckard also imports seamlessly 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 connectedInstrumentTest
. 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.
-
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. -
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 we can be solved in the future.
-
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 theSpecific instrumentation test runner
field. The easiest way is to click the elipsis button on the right and type inGITR
. This will findGoogleInstrumentationTestRunner
, which is what you want. The fully-qualified class name will appear. Now you can right click on the test method inDeckardEspressoTest
and choose the Android test runner.