blob: 2d18ae27a18c1c2d60f88d3cdbd7fa674935dff6 [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 Liutikas0f980f72020-06-25 13:15:39 -070011if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
12 throw GradleException("You are using Java ${JavaVersion.current()}, but this build only supports Java 8. Please set your JAVA_HOME to JDK 8")
13}
14
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070015buildDir = getBuildDirectory()
16
Aurimas Liutikase2d99092020-08-13 17:26:58 -070017defaultTasks = mutableListOf(
18 "installDist",
19 "test",
20 CREATE_ARCHIVE_TASK,
21 CREATE_BUILD_INFO_TASK,
22 "ktlint"
23)
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070024
25repositories {
26 google()
27 jcenter()
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070028 val lintRepo = project.findProperty("lintRepo") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070029 if (lintRepo != null) {
30 logger.warn("Building using custom $lintRepo maven repository")
31 maven {
32 url = uri(lintRepo)
33 }
34 }
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070035}
36
37plugins {
Aurimas Liutikas51d82cc2020-04-29 15:01:30 -070038 kotlin("jvm") version "1.3.72"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070039 id("application")
40 id("java")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -070041 id("maven-publish")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070042}
43
Aurimas Liutikasf5a53df2020-08-13 17:32:40 -070044group = "com.android.tools.metalava"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070045version = getMetalavaVersion()
46
47application {
48 mainClassName = "com.android.tools.metalava.Driver"
49 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
50}
51
52java {
53 sourceCompatibility = JavaVersion.VERSION_1_8
54 targetCompatibility = JavaVersion.VERSION_1_8
55}
56
57tasks.withType(KotlinCompile::class.java) {
58 sourceCompatibility = "1.8"
59 targetCompatibility = "1.8"
60
61 kotlinOptions {
62 jvmTarget = "1.8"
63 apiVersion = "1.3"
64 languageVersion = "1.3"
Aurimas Liutikasf3306a22020-05-05 14:46:08 -070065 allWarningsAsErrors = true
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070066 }
67}
68
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070069val customLintVersion = findProperty("lintVersion") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070070val studioVersion: String = if (customLintVersion != null) {
71 logger.warn("Building using custom $customLintVersion version of Android Lint")
72 customLintVersion
73} else {
Matthew Gharrity3fff9952020-07-31 11:52:15 -070074 "27.2.0-alpha07"
Aurimas Liutikas222577c2020-05-01 17:21:57 -070075}
Aurimas Liutikas51d82cc2020-04-29 15:01:30 -070076val kotlinVersion: String = "1.3.72"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070077
78dependencies {
79 implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
80 implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
81 implementation("com.android.tools.lint:lint-api:$studioVersion")
82 implementation("com.android.tools.lint:lint-checks:$studioVersion")
83 implementation("com.android.tools.lint:lint-gradle:$studioVersion")
84 implementation("com.android.tools.lint:lint:$studioVersion")
85 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
86 implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
87 testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
88 testImplementation("junit:junit:4.11")
89}
90
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070091tasks.withType(Test::class.java) {
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -070092 testLogging.events = hashSetOf(
93 TestLogEvent.FAILED,
94 TestLogEvent.PASSED,
95 TestLogEvent.SKIPPED,
96 TestLogEvent.STANDARD_OUT,
97 TestLogEvent.STANDARD_ERROR
98 )
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070099 val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) {
100 destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
101 archiveFileName.set("metalava-tests.zip")
102 }
103 if (isBuildingOnServer()) ignoreFailures = true
104 finalizedBy(zipTask)
105 doFirst {
106 zipTask.configure {
107 from(reports.junitXml.destination)
108 }
109 }
110}
111
112fun getMetalavaVersion(): Any {
Aurimas Liutikasb5d8fc22019-07-08 10:49:40 -0700113 val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700114 if (versionPropertyFile.canRead()) {
115 val versionProps = Properties()
116 versionProps.load(FileInputStream(versionPropertyFile))
117 val metalavaVersion = versionProps["metalavaVersion"]
118 ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
119 return if (isBuildingOnServer()) {
120 metalavaVersion
121 } else {
122 // Local builds are not public release candidates.
123 "$metalavaVersion-SNAPSHOT"
124 }
125 } else {
126 throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
127 }
128}
129
130fun getBuildDirectory(): File {
131 return if (System.getenv("OUT_DIR") != null) {
132 File(System.getenv("OUT_DIR"), "host/common/metalava")
133 } else {
134 File("../../out/host/common")
135 }
136}
137
138/**
139 * The build server will copy the contents of the distribution directory and make it available for
140 * download.
141 */
142fun getDistributionDirectory(): File {
143 return if (System.getenv("DIST_DIR") != null) {
144 File(System.getenv("DIST_DIR"))
145 } else {
146 File("../../out/dist")
147 }
148}
149
150fun isBuildingOnServer(): Boolean {
151 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
152}
153
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700154/**
155 * @return build id string for current build
156 *
157 * The build server does not pass the build id so we infer it from the last folder of the
158 * distribution directory name.
159 */
160fun getBuildId(): String {
161 return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
162}
163
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700164// KtLint: https://github.com/shyiko/ktlint
165
166fun Project.getKtlintConfiguration(): Configuration {
167 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700168 val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700169 dependencies.add(dependency)
170 }
171}
172
173tasks.register("ktlint", JavaExec::class.java) {
174 description = "Check Kotlin code style."
175 group = "Verification"
176 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700177 main = "com.pinterest.ktlint.Main"
178 args = listOf("src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700179}
180
181tasks.register("ktlintFormat", JavaExec::class.java) {
182 description = "Fix Kotlin code style deviations."
183 group = "formatting"
184 classpath = getKtlintConfiguration()
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700185 main = "com.pinterest.ktlint.Main"
186 args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700187}
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700188
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700189val publicationName = "Metalava"
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700190val repositoryName = "Dist"
191
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700192publishing {
193 publications {
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700194 create<MavenPublication>(publicationName) {
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700195 from(components["java"])
196 pom {
197 licenses {
198 license {
199 name.set("The Apache License, Version 2.0")
200 url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
201 }
202 }
203 developers {
204 developer {
205 name.set("The Android Open Source Project")
206 }
207 }
208 scm {
209 connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
210 url.set("https://android.googlesource.com/platform/tools/metalava/")
211 }
212 }
213 }
214 }
215
216 repositories {
217 maven {
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700218 name = repositoryName
219 url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700220 }
221 }
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700222}
223
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700224// Workaround for https://github.com/gradle/gradle/issues/11717
225tasks.withType(GenerateModuleMetadata::class.java).configureEach {
226 doLast {
227 val metadata = outputFile.asFile.get()
228 var text = metadata.readText()
229 metadata.writeText(
230 text.replace(
231 "\"buildId\": .*".toRegex(),
232 "\"buildId:\": \"${getBuildId()}\"")
233 )
234 }
Aurimas Liutikasd507ea72020-08-13 16:23:54 -0700235}
236
Aurimas Liutikasa8d4d7f2020-08-13 17:57:19 -0700237configureBuildInfoTask(project, isBuildingOnServer(), getDistributionDirectory())
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700238configurePublishingArchive(
239 project,
240 publicationName,
241 repositoryName,
242 getBuildId(),
243 getDistributionDirectory()
244)