Robolectric needs access to multiple Android SDK jars in order to perform its magic, which means it needs special configuration beyond just setting up dependencies in your build. By default, it tries to download Android SDK jars from Maven Central.
But what if you have a hermetic build environment? You just need to do a little more configuration.
Here’s a Gradle build script that’ll help.
- Create an empty Gradle project (either
gradle initor use Android Studio or IntelliJ).
- Paste the script into your
- Change the first line (
robolectricVersion) to the version of Robolectric you want.
- Change the list of
androidSdkVersionsif you like.
- Run the
Gradle will download all the dependencies you need to run Robolectric and place them in
build/output/libs. Place the
.jar files in your project’s libs directory.
.jar files listed in
build/output/README.txt as compile-time dependencies.
Place the file called
build/output/robolectric-deps.properties in your test resources directory. Change the paths as indicated in the comment in that file.
You’re all set! Robolectric will now load Android SDKs from the filesystem instead of attempting to download them from Maven Central.