aboutsummaryrefslogtreecommitdiff
path: root/preinstrumented/build.gradle
blob: 8ffb5bbf2135e11514fca5a3a378c8ead6c2a188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
plugins {
    id "application"
}
apply plugin: 'java'

ext {
    javaMainClass = "org.robolectric.preinstrumented.JarInstrumentor"
}

application {
    mainClassName = javaMainClass
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    implementation libs.guava
    implementation project(":sandbox")
    implementation project(":shadows:versioning")
}

tasks.register('instrumentAll') {
    dependsOn ':prefetchSdks'
    dependsOn 'build'

    doLast {
        def androidAllMavenLocal = "${System.getProperty('user.home')}/.m2/repository/org/robolectric/android-all"

        sdksToInstrument().each { androidSdk ->
            println("Instrumenting ${androidSdk.coordinates}")
            def inputPath = "${androidAllMavenLocal}/${androidSdk.version}/${androidSdk.jarFileName}"
            def outputPath = "${buildDir}/${androidSdk.preinstrumentedJarFileName}"

            javaexec {
                classpath = sourceSets.main.runtimeClasspath
                main = javaMainClass
                args = [inputPath, outputPath]
            }
        }
    }
}

tasks.register('sourcesJar', Jar) {
    archiveClassifier = "sources"
}

tasks.register('javadocJar', Jar) {
    archiveClassifier = "javadoc"
}

// Avoid publishing the preinstrumented jars by default. They are published
// manually when the instrumentation configuration changes to maximize gradle
// and maven caching.
if (System.getenv('PUBLISH_PREINSTRUMENTED_JARS') == "true") {
    apply plugin: 'maven-publish'
    apply plugin: "signing"


    publishing {
        publications {
            sdksToInstrument().each { androidSdk ->
                "sdk${androidSdk.apiLevel}"(MavenPublication) {
                    artifact "${buildDir}/${androidSdk.preinstrumentedJarFileName}"
                    artifactId 'android-all-instrumented'
                    artifact sourcesJar
                    artifact javadocJar
                    version androidSdk.preinstrumentedVersion

                    pom {
                        name = "Google Android ${androidSdk.androidVersion} instrumented android-all library"
                        description = "Google Android ${androidSdk.androidVersion} framework jars transformed with Robolectric instrumentation."
                        url = "https://source.android.com/"
                        inceptionYear = "2008"

                        licenses {
                            license {
                                name = "Apache 2.0"
                                url = "http://www.apache.org/licenses/LICENSE-2.0"
                                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."
                                distribution = "repo"
                            }
                        }

                        scm {
                            url = "https://android.googlesource.com/platform/manifest.git"
                            connection = "https://android.googlesource.com/platform/manifest.git"
                        }

                        developers {
                            developer {
                                name = "The Android Open Source Projects"
                            }
                        }
                    }
                }
            }
        }
        repositories {
            maven {
                url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"

                credentials {
                    username = System.properties["sonatype-login"] ?: System.env['SONATYPE_LOGIN']
                    password = System.properties["sonatype-password"] ?: System.env['SONATYPE_PASSWORD']
                }
            }
        }
    }

    signing {
        sdksToInstrument().each { androidSdk ->
            sign publishing.publications."sdk${androidSdk.apiLevel}"
        }
    }
}

static def sdksToInstrument() {
  var result = AndroidSdk.ALL_SDKS
  var preInstrumentedSdkVersions = (System.getenv('PREINSTRUMENTED_SDK_VERSIONS') ?: "")
  if (preInstrumentedSdkVersions.length() > 0) {
    var sdkFilter = preInstrumentedSdkVersions.split(",").collect { it as Integer }
    if (sdkFilter.size > 0) {
      result = result.findAll { sdkFilter.contains(it.apiLevel) }
    }
  }
  return result
}

clean.doFirst {
    AndroidSdk.ALL_SDKS.each { androidSdk ->
        delete "${buildDir}/${androidSdk.preinstrumentedJarFileName}"
    }
}