Skip to content

Instantly share code, notes, and snippets.

@ephemient
Last active July 12, 2023 13:27
Show Gist options
  • Save ephemient/b27b5b4cca59ee3b1af14b0b75dd33e2 to your computer and use it in GitHub Desktop.
Save ephemient/b27b5b4cca59ee3b1af14b0b75dd33e2 to your computer and use it in GitHub Desktop.
Download Robolectric jars using Gradle
// see org.robolectric.plugins.DefaultSdkProvider
enum class Sdk(val apiLevel: Int, val androidVersion: String, val robolectricVersion: String) {
JELLY_BEAN(16, "4.1.2_r1", "r1"),
JELLY_BEAN_MR1(17, "4.2.2_r1.2", "r1"),
JELLY_BEAN_MR2(18, "4.3_r2", "r1"),
KITKAT(19, "4.4_r1", "r2"),
LOLLIPOP(21, "5.0.2_r3", "r0"),
LOLLIPOP_MR1(22, "5.1.1_r9", "r2"),
M(23, "6.0.1_r3", "r1"),
N(24, "7.0.0_r1", "r1"),
N_MR1(25, "7.1.0_r7", "r1"),
O(26, "8.0.0_r4", "r1"),
O_MR1(27, "8.1.0", "4611349"),
P(28, "9", "4913185-2"),
Q(29, "10", "5803371"),
R(30, "11", "6757853"),
S(31, "12", "7732740"),
S_V2(32, "12.1", "8229987"),
}
val robolectricJars = file("$buildDir/jars")
val downloadRobolectricJars by tasks.registering(Copy::class) {
group = "android"
description = "Installs Robolectric dependency jars to a local directory."
into(robolectricJars)
for (sdk in enumValues<Sdk>()) {
val configuration = configurations.create("robolectric-${sdk.name}-jar")
dependencies.add(
configuration.name,
"org.robolectric:android-all:${sdk.androidVersion}-robolectric-${sdk.robolectricVersion}"
)
from(configuration)
}
}
val jar by tasks.registering(Jar::class) {
group = BasePlugin.BUILD_GROUP
description = "Assembles an empty jar archive. " +
"Downloads Robolectric dependency jars as a side effect."
dependsOn(downloadRobolectricJars)
destinationDirectory.set(file("$buildDir/libs"))
archiveBaseName.set(project.name)
}
val main by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
}
}
artifacts.add(main.name, jar)
val clean by tasks.registering(Delete::class) {
group = BasePlugin.BUILD_GROUP
description = "Deletes the build directory."
delete(buildDir)
}
dependencies {
testImplementation(project(":robolectric-jars"))
}
// https://issuetracker.google.com/issues/122674820
// Switch to android.unitTests.all after AGP 4.1
tasks.withType<Test> {
systemProperty("robolectric.offline", "true")
systemProperty("robolectric.dependency.dir", project(":robolectric-jars").file("build/jars"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment