blob: 5ce14a81f5fda58b7329d4283a86d9343eea7986 [file] [log] [blame]
Aurimas Liutikase2d99092020-08-13 17:26:58 -07001import com.android.tools.metalava.CREATE_ARCHIVE_TASK
2import com.android.tools.metalava.CREATE_BUILD_INFO_TASK
Aurimas Liutikasd507ea72020-08-13 16:23:54 -07003import com.android.tools.metalava.configureBuildInfoTask
Aurimas Liutikase2d99092020-08-13 17:26:58 -07004import com.android.tools.metalava.configurePublishingArchive
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -07005import org.gradle.api.tasks.testing.logging.TestLogEvent
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -07006import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
7import java.io.FileInputStream
8import java.io.FileNotFoundException
9import java.util.Properties
10
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070011buildDir = getBuildDirectory()
12
Aurimas Liutikase2d99092020-08-13 17:26:58 -070013defaultTasks = mutableListOf(
14 "installDist",
15 "test",
16 CREATE_ARCHIVE_TASK,
17 CREATE_BUILD_INFO_TASK,
18 "ktlint"
19)
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070020
21repositories {
22 google()
Aurimas Liutikasc2325f72021-02-17 16:53:53 -080023 mavenCentral()
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070024 val lintRepo = project.findProperty("lintRepo") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070025 if (lintRepo != null) {
26 logger.warn("Building using custom $lintRepo maven repository")
27 maven {
28 url = uri(lintRepo)
29 }
30 }
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070031}
32
33plugins {
Aurimas Liutikasc2325f72021-02-17 16:53:53 -080034 kotlin("jvm") version "1.4.30"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070035 id("application")
36 id("java")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -070037 id("maven-publish")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070038}
39
Aurimas Liutikasf5a53df2020-08-13 17:32:40 -070040group = "com.android.tools.metalava"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070041version = getMetalavaVersion()
42
43application {
Aurimas Liutikase0739482021-04-08 16:11:15 -070044 mainClass.set("com.android.tools.metalava.Driver")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070045 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
46}
47
48java {
49 sourceCompatibility = JavaVersion.VERSION_1_8
50 targetCompatibility = JavaVersion.VERSION_1_8
51}
52
53tasks.withType(KotlinCompile::class.java) {
54 sourceCompatibility = "1.8"
55 targetCompatibility = "1.8"
56
57 kotlinOptions {
58 jvmTarget = "1.8"
Aurimas Liutikas28dd0442020-08-18 16:44:00 -070059 apiVersion = "1.4"
60 languageVersion = "1.4"
Aurimas Liutikasf3306a22020-05-05 14:46:08 -070061 allWarningsAsErrors = true
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070062 }
63}
64
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070065val customLintVersion = findProperty("lintVersion") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070066val studioVersion: String = if (customLintVersion != null) {
67 logger.warn("Building using custom $customLintVersion version of Android Lint")
68 customLintVersion
69} else {
Aurimas Liutikas5dc56d32021-03-29 16:42:34 -070070 "30.0.0-alpha14"
Aurimas Liutikas222577c2020-05-01 17:21:57 -070071}
Aurimas Liutikasc2325f72021-02-17 16:53:53 -080072val kotlinVersion: String = "1.4.30"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070073
74dependencies {
75 implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080076 implementation("com.android.tools.external.com-intellij:kotlin-compiler:$studioVersion")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070077 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")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080082 implementation("com.android.tools:common:$studioVersion")
83 implementation("com.android.tools:sdk-common:$studioVersion")
84 implementation("com.android.tools:sdklib:$studioVersion")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070085 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
86 implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080087 implementation("org.ow2.asm:asm:8.0")
88 implementation("org.ow2.asm:asm-tree:8.0")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070089 testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
90 testImplementation("junit:junit:4.11")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080091 testImplementation("com.google.truth:truth:1.0")
92 testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070093}
94
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070095tasks.withType(Test::class.java) {
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -070096 testLogging.events = hashSetOf(
97 TestLogEvent.FAILED,
98 TestLogEvent.PASSED,
99 TestLogEvent.SKIPPED,
100 TestLogEvent.STANDARD_OUT,
101 TestLogEvent.STANDARD_ERROR
102 )
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700103 val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) {
104 destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
105 archiveFileName.set("metalava-tests.zip")
106 }
107 if (isBuildingOnServer()) ignoreFailures = true
108 finalizedBy(zipTask)
109 doFirst {
110 zipTask.configure {
111 from(reports.junitXml.destination)
112 }
113 }
114}
115
116fun getMetalavaVersion(): Any {
Aurimas Liutikasb5d8fc22019-07-08 10:49:40 -0700117 val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700118 if (versionPropertyFile.canRead()) {
119 val versionProps = Properties()
120 versionProps.load(FileInputStream(versionPropertyFile))
121 val metalavaVersion = versionProps["metalavaVersion"]
122 ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
123 return if (isBuildingOnServer()) {
124 metalavaVersion
125 } else {
126 // Local builds are not public release candidates.
127 "$metalavaVersion-SNAPSHOT"
128 }
129 } else {
130 throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
131 }
132}
133
134fun getBuildDirectory(): File {
135 return if (System.getenv("OUT_DIR") != null) {
136 File(System.getenv("OUT_DIR"), "host/common/metalava")
137 } else {
138 File("../../out/host/common")
139 }
140}
141
142/**
143 * The build server will copy the contents of the distribution directory and make it available for
144 * download.
145 */
146fun getDistributionDirectory(): File {
147 return if (System.getenv("DIST_DIR") != null) {
148 File(System.getenv("DIST_DIR"))
149 } else {
150 File("../../out/dist")
151 }
152}
153
154fun isBuildingOnServer(): Boolean {
155 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
156}
157
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700158/**
159 * @return build id string for current build
160 *
161 * The build server does not pass the build id so we infer it from the last folder of the
162 * distribution directory name.
163 */
164fun getBuildId(): String {
165 return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
166}
167
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700168// KtLint: https://github.com/shyiko/ktlint
169
170fun Project.getKtlintConfiguration(): Configuration {
171 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700172 val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700173 dependencies.add(dependency)
174 }
175}
176
177tasks.register("ktlint", JavaExec::class.java) {
178 description = "Check Kotlin code style."
179 group = "Verification"
180 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700181 main = "com.pinterest.ktlint.Main"
182 args = listOf("src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700183}
184
185tasks.register("ktlintFormat", JavaExec::class.java) {
186 description = "Fix Kotlin code style deviations."
187 group = "formatting"
188 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700189 main = "com.pinterest.ktlint.Main"
190 args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700191}
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700192
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700193val publicationName = "Metalava"
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700194val repositoryName = "Dist"
195
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700196publishing {
197 publications {
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700198 create<MavenPublication>(publicationName) {
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700199 from(components["java"])
200 pom {
201 licenses {
202 license {
203 name.set("The Apache License, Version 2.0")
204 url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
205 }
206 }
207 developers {
208 developer {
209 name.set("The Android Open Source Project")
210 }
211 }
212 scm {
213 connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
214 url.set("https://android.googlesource.com/platform/tools/metalava/")
215 }
216 }
217 }
218 }
219
220 repositories {
221 maven {
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700222 name = repositoryName
223 url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700224 }
225 }
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700226}
227
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700228// Workaround for https://github.com/gradle/gradle/issues/11717
229tasks.withType(GenerateModuleMetadata::class.java).configureEach {
230 doLast {
231 val metadata = outputFile.asFile.get()
Aurimas Liutikase0739482021-04-08 16:11:15 -0700232 val text = metadata.readText()
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700233 metadata.writeText(
234 text.replace(
235 "\"buildId\": .*".toRegex(),
236 "\"buildId:\": \"${getBuildId()}\"")
237 )
238 }
Aurimas Liutikasd507ea72020-08-13 16:23:54 -0700239}
240
Aurimas Liutikasa8d4d7f2020-08-13 17:57:19 -0700241configureBuildInfoTask(project, isBuildingOnServer(), getDistributionDirectory())
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700242configurePublishingArchive(
243 project,
244 publicationName,
245 repositoryName,
246 getBuildId(),
247 getDistributionDirectory()
248)