Skip to content

Instantly share code, notes, and snippets.

@simtel12
Created January 22, 2019 22:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save simtel12/13ff3e57c37e78e468502b51ebb0f4f2 to your computer and use it in GitHub Desktop.
Save simtel12/13ff3e57c37e78e468502b51ebb0f4f2 to your computer and use it in GitHub Desktop.
Robolectric manual download
/**
* Downloads all android-all dependencies and copies them to the mavenLocal() repository
*
* Once applied to your gradle project, can be executed with ./gradlew robolectricSdkDownload
*/
import java.nio.file.Files
// The general idea of this was borrowed from https://gist.github.com/xian/05c4f27da6d4156b9827842217c2cd5c
// I then modified it heavily to allow easier addition of new SDK versions
// List from: https://github.com/robolectric/robolectric/blob/master/robolectric/src/main/java/org/robolectric/internal/SdkConfig.java
// This list will need to be updated for new Android SDK versions that come out.
def robolectricAndroidSdkVersions = [
[androidVersion: "4.4_r1", frameworkSdkBuildVersion: "r2"],
[androidVersion: "5.0.2_r3", frameworkSdkBuildVersion: "r0"],
[androidVersion: "5.1.1_r9", frameworkSdkBuildVersion: "r2"],
[androidVersion: "6.0.1_r3", frameworkSdkBuildVersion: "r1"],
[androidVersion: "7.0.0_r1", frameworkSdkBuildVersion: "r1"],
[androidVersion: "7.1.0_r7", frameworkSdkBuildVersion: "r1"],
[androidVersion: "8.0.0_r4", frameworkSdkBuildVersion: "r1"],
[androidVersion: "8.1.0", frameworkSdkBuildVersion: "4611349"],
[androidVersion: "9", frameworkSdkBuildVersion: "4913185-2"],
]
// Base, public task - will be displayed in ./gradlew robolectricDownloader:tasks
task robolectricSdkDownload {
group = "Dependencies"
description = "Downloads all robolectric SDK dependencies into mavenLocal, for use with offline robolectric"
}
// Generate the configuration and actual copy tasks.
robolectricAndroidSdkVersions.forEach { robolectricSdkVersion ->
def version = "${robolectricSdkVersion['androidVersion']}-robolectric-${robolectricSdkVersion['frameworkSdkBuildVersion']}"
// Creating a configuration with a dependency allows Gradle to manage the actual resolution of
// the jar file
def sdkConfig = configurations.create(version)
dependencies.add(version, "org.robolectric:android-all:${version}")
repositories.add(Repos.jcenter(this.repositories))
def mavenLocalFile = new File(this.repositories.mavenLocal().url)
def mavenRobolectric = new File(mavenLocalFile, "org/robolectric/android-all/${version}")
// Copying all files downloaded for the created configuration into maven local.
task "robolectricSdkDownload-${version}"(type: Copy) {
from sdkConfig
into mavenRobolectric
doLast {
ArtifactResolutionResult result = dependencies.createArtifactResolutionQuery()
.forModule("org.robolectric", "android-all", version)
.withArtifacts(MavenModule, MavenPomArtifact)
.execute()
for(component in result.resolvedComponents) {
def componentId = component.id
if(componentId instanceof ModuleComponentIdentifier) {
File pomFile = component.getArtifacts(MavenPomArtifact)[0].file
File dest = new File(mavenRobolectric, pomFile.name)
Files.copy(pomFile.toPath(), dest.toPath())
}
}
}
}
robolectricSdkDownload.dependsOn "robolectricSdkDownload-${version}"
}
@darakeon
Copy link

darakeon commented Apr 6, 2021

The Line #41 does not works, seems like it can't find Repos.

Pasted the code without this line at my project and seems to have fixed my problem.

Thanks!

@simtel12
Copy link
Author

simtel12 commented Apr 6, 2021

The Line #41 does not works, seems like it can't find Repos.

Pasted the code without this line at my project and seems to have fixed my problem.

Thanks!

Glad to hear it helped! I didn't realize that Repos snuck in here - it's a helper class that I have in my buildSrc to handle configuration-time switching of repositories (ex. using local jcenter cache when [condition], or the Internet version of it when [!condition]) as well as consolidating the logic of restricting what dependencies can be used from a given repository.

@darakeon
Copy link

darakeon commented Apr 6, 2021

Thanks again for the help! I was fighting docker+robolectric combination for some days already... Now my tests are passing at circleCI!

And I ended up using robolectric to change SDK because changing SDK_INT static final does not work after java 11... You know when you discover a bug is actually a matrioska? Like that.

But it is solved, thanks!

@jairobjunior
Copy link

@simtel12 That works!

Since the method Repos.jcenter(this.repositories) is not being exposed, I created another version that can be helpful for future reference https://gist.github.com/jairobjunior/d739730a970c622d4634b0c84f0975c7

Thank you!

@n8yn8
Copy link

n8yn8 commented Mar 10, 2022

For anyone that may come to this problem again, it seems that Robolectric 4.5.1 to 4.6 transitioned from android-all artifacts to android-all-instrumented artifacts. These artifacts also have -i3 appended to their frameworkSdkBuildVersion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment