• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
<lambda>null17 plugins {
18     id("application")
19     id("org.jetbrains.kotlin.jvm")
20     id("maven-publish")
21     id("metalava-build-plugin")
22 }
23 
<lambda>null24 application {
25     mainClass.set("com.android.tools.metalava.Driver")
26     applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
27 }
28 
<lambda>null29 dependencies {
30     implementation(project(":metalava-model"))
31     implementation(project(":metalava-model-psi"))
32     implementation(project(":metalava-model-source"))
33     implementation(project(":metalava-model-text"))
34     implementation(project(":metalava-reporter"))
35     implementation(libs.androidToolsExternalUast)
36     implementation(libs.androidToolsExternalKotlinCompiler)
37     implementation(libs.androidToolsExternalIntellijCore)
38     implementation(libs.androidLintApi)
39     implementation(libs.androidLintChecks)
40     implementation(libs.androidLintGradle)
41     implementation(libs.androidLint)
42     implementation(libs.androidToolsCommon)
43     implementation(libs.androidToolsSdkCommon)
44     implementation(libs.androidToolsSdklib)
45     implementation(libs.clikt)
46     implementation(libs.kotlinStdlib)
47     implementation(libs.asm)
48     implementation(libs.asmTree)
49     implementation(libs.gson)
50 
51     testImplementation(project(":metalava-model-turbine"))
52     testImplementation(project(":metalava-testing"))
53     testImplementation(testFixtures(project(":metalava-model")))
54     testImplementation(testFixtures(project(":metalava-model-text")))
55     testImplementation(libs.androidLintTests)
56     testImplementation(libs.junit4)
57     testImplementation(libs.truth)
58     testImplementation(libs.kotlinTest)
59 }
60 
61 /** The location into which a fake representation of the prebuilts/sdk directory will be written. */
62 val testPrebuiltsSdkDir = layout.buildDirectory.dir("prebuilts/sdk")
63 
64 /**
65  * Register tasks to emulate parts of the prebuilts/sdk repository using source from this directory.
66  *
67  * [sourceDir] is the path to the root directory of code that is compiled into a jar that
68  * corresponds to the jar specified by [destJar].
69  *
70  * [destJar] is the path to a jar within prebuilts/sdk. The fake jar created from [sourceDir] is
71  * copied to getBuildDirectory()/prebuilts/sdk/$destJar.
72  *
73  * The jars created by this can be accessed by tests via the `METALAVA_TEST_PREBUILTS_SDK_ROOT`
74  * environment variable which is set to point to getBuildDirectory()/prebuilts/sdk; see [testTask].
75  */
registerTestPrebuiltsSdkTasksnull76 fun registerTestPrebuiltsSdkTasks(sourceDir: String, destJar: String): TaskProvider<Jar> {
77     val basename = sourceDir.replace("/", "-")
78     val javaCompileTaskName = "$basename.classes"
79     val jarTaskName = "$basename.jar"
80 
81     val compileTask =
82         project.tasks.register(javaCompileTaskName, JavaCompile::class) {
83             options.compilerArgs = listOf("--patch-module", "java.base=" + file(sourceDir))
84             source = fileTree(sourceDir)
85             classpath = project.files()
86             destinationDirectory.set(layout.buildDirectory.dir(javaCompileTaskName))
87         }
88 
89     val destJarFile = File(destJar)
90     val dir = destJarFile.parent
91     val filename = destJarFile.name
92     if (dir == ".") {
93         throw IllegalArgumentException("bad destJar argument '$destJar'")
94     }
95 
96     val jarTask =
97         project.tasks.register(jarTaskName, Jar::class) {
98             from(compileTask.flatMap { it.destinationDirectory })
99             archiveFileName.set(filename)
100             destinationDirectory.set(testPrebuiltsSdkDir.map { it.dir(dir) })
101         }
102 
103     return jarTask
104 }
105 
106 val testPrebuiltsSdkApi30 =
107     registerTestPrebuiltsSdkTasks("src/testdata/prebuilts-sdk-test/30", "30/public/android.jar")
108 val testPrebuiltsSdkApi31 =
109     registerTestPrebuiltsSdkTasks("src/testdata/prebuilts-sdk-test/31", "31/public/android.jar")
110 val testPrebuiltsSdkExt1 =
111     registerTestPrebuiltsSdkTasks(
112         "src/testdata/prebuilts-sdk-test/extensions/1",
113         "extensions/1/public/framework-ext.jar"
114     )
115 val testPrebuiltsSdkExt2 =
116     registerTestPrebuiltsSdkTasks(
117         "src/testdata/prebuilts-sdk-test/extensions/2",
118         "extensions/2/public/framework-ext.jar"
119     )
120 val testPrebuiltsSdkExt3 =
121     registerTestPrebuiltsSdkTasks(
122         "src/testdata/prebuilts-sdk-test/extensions/3",
123         "extensions/3/public/framework-ext.jar"
124     )
125 
<lambda>null126 project.tasks.register("test-sdk-extensions-info.xml", Copy::class) {
127     from("src/testdata/prebuilts-sdk-test/sdk-extensions-info.xml")
128     into(testPrebuiltsSdkDir)
129 }
130 
<lambda>null131 project.tasks.register("test-prebuilts-sdk") {
132     dependsOn(testPrebuiltsSdkApi30)
133     dependsOn(testPrebuiltsSdkApi31)
134     dependsOn(testPrebuiltsSdkExt1)
135     dependsOn(testPrebuiltsSdkExt2)
136     dependsOn(testPrebuiltsSdkExt3)
137     dependsOn("test-sdk-extensions-info.xml")
138 }
139 
<lambda>null140 tasks.named<Test>("test").configure {
141     dependsOn("test-prebuilts-sdk")
142     setEnvironment(
143         "METALAVA_TEST_PREBUILTS_SDK_ROOT" to testPrebuiltsSdkDir.get().asFile.absolutePath
144     )
145 }
146 
147 // Copy the version.properties from the root project into its own directory.
148 val copyVersion =
<lambda>null149     tasks.register("copyVersion", Copy::class.java) {
150         from("${rootProject.projectDir}/version.properties")
151         into(project.layout.buildDirectory.dir("version"))
152     }
153 
154 // Add the version directory to the directories from which the jar is built.
155 sourceSets.getByName("main").output.dir(copyVersion)
156