Robolectric fetches and caches it's own android jar package by itself during initialization, this mechanism is kinda unreliable, for example it can fail your CI builds from time to time.
If you have same problems, take a look at our approach.
We created gradle module, containing fetch logic as a custom task; see attached robolectric.build.gradle
;
All other modules, containing robolectric tests should dependens on it.
Custom task eagerly resolves jar's and store this as a typical dependency in gradle cache.
Now we need to tell robolectric to use our, already fetched jar's, instead of downloading them again;
To do so we have to register custom SdkProvider, via robolectric plugin api
Just place file, named: org.robolectric.pluginapi.SdkProvider
(attached to gist) to <our-custom-robolectric-module>/src/main/resources/META-INF.services/
File just points to class in out module.
AvitoSdkProvider
(attached in gist) overrides getSdks()
function and returns only relevant (downloaded in gradle) jar paths.
RobolectricSdk
(attached in gist) overrides getJarPath()
to point on gradle caches path, that we uses to cache jar's