1plugins { 2 id "application" 3} 4apply plugin: 'java' 5 6ext { 7 javaMainClass = "org.robolectric.preinstrumented.JarInstrumentor" 8} 9 10application { 11 mainClassName = javaMainClass 12} 13 14java { 15 sourceCompatibility = JavaVersion.VERSION_1_8 16 targetCompatibility = JavaVersion.VERSION_1_8 17} 18 19dependencies { 20 implementation libs.guava 21 implementation project(":sandbox") 22 23 testImplementation libs.junit4 24 testImplementation libs.mockito 25} 26 27tasks.register('instrumentAll') { 28 dependsOn ':prefetchSdks' 29 dependsOn 'build' 30 31 doLast { 32 def androidAllMavenLocal = "${System.getProperty('user.home')}/.m2/repository/org/robolectric/android-all" 33 34 sdksToInstrument().each { androidSdk -> 35 println("Instrumenting ${androidSdk.coordinates}") 36 def inputPath = "${androidAllMavenLocal}/${androidSdk.version}/${androidSdk.jarFileName}" 37 def outputPath = layout.buildDirectory.file(androidSdk.preinstrumentedJarFileName).get().asFile.path 38 39 javaexec { 40 classpath = sourceSets.main.runtimeClasspath 41 main = javaMainClass 42 args = [inputPath, outputPath] 43 } 44 } 45 } 46} 47 48tasks.register('emptySourcesJar', Jar) { 49 archiveClassifier = "sources" 50} 51 52tasks.register('emptyJavadocJar', Jar) { 53 archiveClassifier = "javadoc" 54} 55 56// Avoid publishing the preinstrumented jars by default. They are published 57// manually when the instrumentation configuration changes to maximize gradle 58// and maven caching. 59if (System.getenv('PUBLISH_PREINSTRUMENTED_JARS') == "true") { 60 apply plugin: 'maven-publish' 61 apply plugin: "signing" 62 63 64 publishing { 65 publications { 66 sdksToInstrument().each { androidSdk -> 67 "sdk${androidSdk.apiLevel}"(MavenPublication) { 68 artifact = layout.buildDirectory.file(androidSdk.preinstrumentedJarFileName).get().asFile.path 69 artifactId 'android-all-instrumented' 70 artifact emptySourcesJar 71 artifact emptyJavadocJar 72 version androidSdk.preinstrumentedVersion 73 74 pom { 75 name = "Google Android ${androidSdk.androidVersion} instrumented android-all library" 76 description = "Google Android ${androidSdk.androidVersion} framework jars transformed with Robolectric instrumentation." 77 url = "https://source.android.com/" 78 inceptionYear = "2008" 79 80 licenses { 81 license { 82 name = "Apache 2.0" 83 url = "http://www.apache.org/licenses/LICENSE-2.0" 84 comments = "While the EULA for the Android SDK restricts distribution of those binaries, the source code is licensed under Apache 2.0 which allows compiling binaries from source and then distributing those versions." 85 distribution = "repo" 86 } 87 } 88 89 scm { 90 url = "https://android.googlesource.com/platform/manifest.git" 91 connection = "https://android.googlesource.com/platform/manifest.git" 92 } 93 94 developers { 95 developer { 96 name = "The Android Open Source Projects" 97 } 98 } 99 } 100 } 101 } 102 } 103 repositories { 104 maven { 105 url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 106 107 credentials { 108 username = System.properties["sonatype-login"] ?: System.env['SONATYPE_LOGIN'] 109 password = System.properties["sonatype-password"] ?: System.env['SONATYPE_PASSWORD'] 110 } 111 } 112 } 113 } 114 115 signing { 116 sdksToInstrument().each { androidSdk -> 117 sign publishing.publications."sdk${androidSdk.apiLevel}" 118 } 119 } 120 121 122 // Workaround for https://github.com/gradle/gradle/issues/26132 123 // For some reason, Gradle has inferred that all publishing tasks depend on all signing tasks, 124 // so we must explicitly declare this here. 125 afterEvaluate { 126 tasks.configureEach { 127 if (name.startsWith("publishSdk")) { 128 sdksToInstrument().each { androidSdk -> 129 dependsOn(tasks.named("signSdk${androidSdk.apiLevel}Publication")) 130 } 131 } 132 } 133 } 134} 135 136static def sdksToInstrument() { 137 var result = AndroidSdk.ALL_SDKS 138 var preInstrumentedSdkVersions = (System.getenv('PREINSTRUMENTED_SDK_VERSIONS') ?: "") 139 if (preInstrumentedSdkVersions.length() > 0) { 140 var sdkFilter = preInstrumentedSdkVersions.split(",").collect { it as Integer } 141 if (sdkFilter.size > 0) { 142 result = result.findAll { sdkFilter.contains(it.apiLevel) } 143 } 144 } 145 return result 146} 147 148clean.doFirst { 149 AndroidSdk.ALL_SDKS.each { androidSdk -> 150 delete layout.buildDirectory.file(androidSdk.preinstrumentedJarFileName) 151 } 152} 153