1 import com.android.tools.metalava.CREATE_ARCHIVE_TASK
2 import com.android.tools.metalava.CREATE_BUILD_INFO_TASK
3 import com.android.tools.metalava.configureBuildInfoTask
4 import com.android.tools.metalava.configurePublishingArchive
5 import org.gradle.api.tasks.testing.logging.TestLogEvent
6 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
7 import java.io.FileInputStream
8 import java.io.FileNotFoundException
9 import java.util.Properties
10
11 buildDir = getBuildDirectory()
12
13 defaultTasks = mutableListOf(
14 "installDist",
15 "test",
16 CREATE_ARCHIVE_TASK,
17 CREATE_BUILD_INFO_TASK,
18 "ktlint"
19 )
20
<lambda>null21 repositories {
22 google()
23 mavenCentral()
24 val lintRepo = project.findProperty("lintRepo") as String?
25 if (lintRepo != null) {
26 logger.warn("Building using custom $lintRepo maven repository")
27 maven {
28 url = uri(lintRepo)
29 }
30 }
31 }
32
<lambda>null33 plugins {
34 kotlin("jvm") version "1.5.0"
35 id("application")
36 id("java")
37 id("maven-publish")
38 }
39
40 group = "com.android.tools.metalava"
41 version = getMetalavaVersion()
42
<lambda>null43 application {
44 mainClass.set("com.android.tools.metalava.Driver")
45 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
46 }
47
<lambda>null48 java {
49 sourceCompatibility = JavaVersion.VERSION_1_8
50 targetCompatibility = JavaVersion.VERSION_1_8
51 }
52
<lambda>null53 tasks.withType(KotlinCompile::class.java) {
54 sourceCompatibility = "1.8"
55 targetCompatibility = "1.8"
56
57 kotlinOptions {
58 jvmTarget = "1.8"
59 apiVersion = "1.4"
60 languageVersion = "1.4"
61 allWarningsAsErrors = true
62 }
63 }
64
65 val customLintVersion = findProperty("lintVersion") as String?
66 val studioVersion: String = if (customLintVersion != null) {
67 logger.warn("Building using custom $customLintVersion version of Android Lint")
68 customLintVersion
69 } else {
70 "30.0.0-alpha14"
71 }
72 val kotlinVersion: String = "1.5.0"
73
<lambda>null74 dependencies {
75 implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
76 implementation("com.android.tools.external.com-intellij:kotlin-compiler:$studioVersion")
77 implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
78 implementation("com.android.tools.lint:lint-api:$studioVersion")
79 implementation("com.android.tools.lint:lint-checks:$studioVersion")
80 implementation("com.android.tools.lint:lint-gradle:$studioVersion")
81 implementation("com.android.tools.lint:lint:$studioVersion")
82 implementation("com.android.tools:common:$studioVersion")
83 implementation("com.android.tools:sdk-common:$studioVersion")
84 implementation("com.android.tools:sdklib:$studioVersion")
85 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
86 implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
87 implementation("org.ow2.asm:asm:8.0")
88 implementation("org.ow2.asm:asm-tree:8.0")
89 testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
90 testImplementation("junit:junit:4.11")
91 testImplementation("com.google.truth:truth:1.0")
92 testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
93 }
94
95 val zipTask: TaskProvider<Zip> = project.tasks.register(
96 "zipResultsOf${name.capitalize()}",
97 Zip::class.java
<lambda>null98 ) {
99 destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
100 archiveFileName.set("metalava-tests.zip")
101 }
102
103 val testTask = tasks.named("test", Test::class.java)
<lambda>null104 testTask.configure {
105 testLogging.events = hashSetOf(
106 TestLogEvent.FAILED,
107 TestLogEvent.PASSED,
108 TestLogEvent.SKIPPED,
109 TestLogEvent.STANDARD_OUT,
110 TestLogEvent.STANDARD_ERROR
111 )
112 if (isBuildingOnServer()) ignoreFailures = true
113 finalizedBy(zipTask)
114 }
<lambda>null115 zipTask.configure {
116 from(testTask.map { it.reports.junitXml.outputLocation.get() })
117 }
118
getMetalavaVersionnull119 fun getMetalavaVersion(): Any {
120 val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
121 if (versionPropertyFile.canRead()) {
122 val versionProps = Properties()
123 versionProps.load(FileInputStream(versionPropertyFile))
124 val metalavaVersion = versionProps["metalavaVersion"]
125 ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
126 return if (isBuildingOnServer()) {
127 metalavaVersion
128 } else {
129 // Local builds are not public release candidates.
130 "$metalavaVersion-SNAPSHOT"
131 }
132 } else {
133 throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
134 }
135 }
136
getBuildDirectorynull137 fun getBuildDirectory(): File {
138 return if (System.getenv("OUT_DIR") != null) {
139 File(System.getenv("OUT_DIR"), "host/common/metalava")
140 } else {
141 File("../../out/host/common")
142 }
143 }
144
145 /**
146 * The build server will copy the contents of the distribution directory and make it available for
147 * download.
148 */
getDistributionDirectorynull149 fun getDistributionDirectory(): File {
150 return if (System.getenv("DIST_DIR") != null) {
151 File(System.getenv("DIST_DIR"))
152 } else {
153 File("../../out/dist")
154 }
155 }
156
isBuildingOnServernull157 fun isBuildingOnServer(): Boolean {
158 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
159 }
160
161 /**
162 * @return build id string for current build
163 *
164 * The build server does not pass the build id so we infer it from the last folder of the
165 * distribution directory name.
166 */
getBuildIdnull167 fun getBuildId(): String {
168 return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
169 }
170
171 // KtLint: https://github.com/shyiko/ktlint
172
Projectnull173 fun Project.getKtlintConfiguration(): Configuration {
174 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
175 val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
176 dependencies.add(dependency)
177 }
178 }
179
<lambda>null180 tasks.register("ktlint", JavaExec::class.java) {
181 description = "Check Kotlin code style."
182 group = "Verification"
183 classpath = getKtlintConfiguration()
184 main = "com.pinterest.ktlint.Main"
185 args = listOf("src/**/*.kt", "build.gradle.kts")
186 }
187
<lambda>null188 tasks.register("ktlintFormat", JavaExec::class.java) {
189 description = "Fix Kotlin code style deviations."
190 group = "formatting"
191 classpath = getKtlintConfiguration()
192 main = "com.pinterest.ktlint.Main"
193 args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
194 }
195
196 val publicationName = "Metalava"
197 val repositoryName = "Dist"
198
<lambda>null199 publishing {
200 publications {
201 create<MavenPublication>(publicationName) {
202 from(components["java"])
203 pom {
204 licenses {
205 license {
206 name.set("The Apache License, Version 2.0")
207 url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
208 }
209 }
210 developers {
211 developer {
212 name.set("The Android Open Source Project")
213 }
214 }
215 scm {
216 connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
217 url.set("https://android.googlesource.com/platform/tools/metalava/")
218 }
219 }
220 }
221 }
222
223 repositories {
224 maven {
225 name = repositoryName
226 url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
227 }
228 }
229 }
230
231 // Workaround for https://github.com/gradle/gradle/issues/11717
<lambda>null232 tasks.withType(GenerateModuleMetadata::class.java).configureEach {
233 val outDirProvider = project.providers.environmentVariable("DIST_DIR")
234 inputs.property("buildOutputDirectory", outDirProvider).optional(true)
235 doLast {
236 val metadata = outputFile.asFile.get()
237 val text = metadata.readText()
238 val buildId = outDirProvider.orNull?.let { File(it).name } ?: "0"
239 metadata.writeText(
240 text.replace(
241 "\"buildId\": .*".toRegex(),
242 "\"buildId:\": \"${buildId}\"")
243 )
244 }
245 }
246
247 configureBuildInfoTask(project, isBuildingOnServer(), getDistributionDirectory())
248 configurePublishingArchive(
249 project,
250 publicationName,
251 repositoryName,
252 getBuildId(),
253 getDistributionDirectory()
254 )
255