Building framework JARs from platform sources - robolectric/robolectric GitHub Wiki

Robolectric uses a set of Android framework Jars that are compiled from Android platform sources. Note that these are not the same stub jars that are managed by Android Studio -- they contain the actual Android Java framework implementation that gets loaded onto system images. Robolectric itself is compiled against the latest released framework Jar. During runtime, tests can specify an SDK version to use, and Robolectric will create a sandbox and use the corresponding Jar to resolve Android framework classes.

You can see all of the framework Jars that have been used by Robolectric here: https://repo1.maven.org/maven2/org/robolectric/android-all/

The current set of framework Jars used by Robolectric is also contained in AndroidSdk.groovy.

The process to build these framework Jars usable by Robolectric is straightforward. The build rule for the JAR is the robolectric-host-android_all target in the robolectric-shadows Android.bp file. robolectric-shadows is the canonical location of Robolectric sources in the Android platform.

After executing this build rule, a file android-all-robolectric.jar is generated which contains the implementation of the framework jar.

In more details, to check out AOSP and build this Jar, install the repo program, and run:

mkdir aosp-master
cd aosp-master
repo init -u https://android.googlesource.com/platform/manifest -b master
repo sync -cq -j 64
export ANDROID_ROOT=/path/to/aosp-master
export LUNCH=sdk_phone_armv7
export ALLOW_MISSING_DEPENDENCIES=true
export DIST_DIR=$ANDROID_ROOT/out/dist
. build/envsetup.sh
lunch $LUNCH-eng
m -j robolectric-host-android_all
# the framework JAR is now contained in `out/host/linux-x86/framework/robolectric-host-android_all.jar`

Alternatively, the Robolectric framework Jar is generated as a dist artifact for the sdk target, so you can also run m -j sdk dist, and locate the Robolectric JAR in out/dist/android-all-robolectric.jar.

The Jar files that get uploaded to Robolectric's maven package (org.robolectric.android-all) originate from an internal Google continuous integration tool for Android. This is done for simplicity and integrity. These Jars equivalent to the AOSP version after the AOSP branch is synced with the internal Google branch, which happens as part of the Android release process.

Note also that Robolectric by default uses preinstrumented framework Jars. The preinstrumented jars the original framework JARs that have undergone Robolectric instrumentation (i.e. bytecode transformation to support the JVM and shadowing, see ClassInstrumentor. This reduces the amount of runtime overhead required to load Android classes.