Robojava is a Gradle plugin that allows simple integration of Robolectric into Android Studio. This plugin works by creating a stub java project. This allows extending it with powerful plugins that are compatible with gradle java projects, but not with gradle android projects, like Gradle Cobertura Plugin for example.
Assuming your project structure is a Multi Project Gradle Build, add the following to your root buildscript's dependencies section:
Then create a stub project at the same level as your android project with a
build.gradle like so:
ext.androidProject = 'myapp' // name of the android project
ext.variant = 'freeDebug' // add the test variant flavor (remove if you do not have use flavors). See example project.
apply plugin: 'cobertura' // add if you want code coverage. See example project.
apply plugin: 'com.kageiit.robojava'
settings.gradle in the project root:
include 'myapp', 'tdd'
You may need to also use a custom test runner (may not be required for library projects). Add the
generated-sources directory as source directory for the
androidTest source set:
sourceSets.androidTest.java.srcDirs += 'build/generated-sources'
This will allow you to configure your custom test runner easily, as the plugin generates code for obtaining the directory paths to the manifest, resources, assets etc. See the included example.
The final structure may look something like this:
This plugin was created mainly to address the problem of not being able to configure Android project tests easily for the IDE and TDD etc. You can now simply do the following:
- Run/debug all robolectric tests in the IDE without any other manual intervention/configuration.
- Run a set of tests via the commandline:
./gradlew test --tests com.exampe.MyActivityTest.testAwesomeMethod
- NO modifications required to Android Studio. Configuration lives with the project.
This plugin was tested against versions
- Android Studio - 1.1 Preview 1
- Android Gradle Build Plugin - 1.0.0
- Only one test variant can be chosen at a time to run, if you are running single tests via the commandline. This limitation will be removed in a future release. Although, you can run all tests of all variants/flavors by simply running `./gradlew test'.
- All tests in the main source set are fully integrated into Android Studio, but product flavor integration is currently in progress. You can run them via the commandline, however.
Copyright 2014 Gautam Korlam
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.