xref: /aosp_15_r20/external/dagger2/java/dagger/hilt/android/plugin/main/build.gradle (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1/*
2 * Copyright (C) 2020 The Dagger Authors.
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 */
16import java.util.zip.ZipEntry
17import java.util.zip.ZipFile
18
19plugins {
20  id 'org.jetbrains.kotlin.jvm'
21  id 'java-gradle-plugin'
22  id 'maven-publish'
23  id 'com.github.johnrengelman.shadow'
24}
25
26configurations {
27  // Config for dependencies that will be shadowed / jarjared
28  shadowed
29  // Make all shadowed dependencies be compileOnly dependencies to not affect
30  // main compilation / configuration
31  compileOnly.extendsFrom(shadowed)
32  // Make all shadowed dependencies be included in the plugin test classpath
33  // since they are compileOnly in the main configuration
34  testPluginCompile.extendsFrom(shadowed)
35  // Config for plugin classpath to be used during tests
36  testPluginCompile {
37    canBeConsumed = false
38    canBeResolved = true
39  }
40}
41
42// Renames default jar to avoid using it in publications.
43jar {
44  archiveClassifier = "before-jarjar"
45}
46shadowJar {
47  archiveClassifier = ""
48  configurations = [project.configurations.shadowed]
49  dependencies {
50    // Don't jarjar stdlib deps that are automatically added by Kotlin plugin
51    exclude(dependency("org.jetbrains.kotlin::"))
52    exclude(dependency("org.jetbrains:annotations:"))
53  }
54  doLast {
55    outputs.files.each { jarFile ->
56      checkJarFile(jarFile,  'dagger',  'META-INF')
57    }
58  }
59}
60
61dependencies {
62  shadowed project(':agp-wrapper-impl')
63  shadowed fileTree(dir: 'libs', include: '*.jar')
64  implementation gradleApi()
65  compileOnly "com.android.tools.build:gradle:$agp_version"
66  compileOnly "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
67  compileOnly "com.google.devtools.ksp:symbol-processing-gradle-plugin:$ksp_version"
68  implementation 'org.ow2.asm:asm:9.6'
69  implementation "com.squareup:javapoet:1.13.0"
70
71  testImplementation gradleTestKit()
72  testImplementation 'junit:junit:4.12'
73  testImplementation 'com.google.truth:truth:1.0.1'
74  testImplementation 'org.javassist:javassist:3.26.0-GA'
75  testPluginCompile "com.android.tools.build:gradle:$agp_version"
76  testPluginCompile "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
77  testPluginCompile "com.google.devtools.ksp:symbol-processing-gradle-plugin:$ksp_version"
78}
79
80// Configure the generating task of plugin-under-test-metadata.properties to
81// include additional dependencies for the injected plugin classpath that
82// are not present in the main runtime dependencies. This allows us to test
83// the desired AGP version while keeping a compileOnly dep on the main source.
84tasks.withType(PluginUnderTestMetadata.class).named("pluginUnderTestMetadata").configure {
85  it.pluginClasspath.from(configurations.testPluginCompile)
86}
87
88compileKotlin {
89  kotlinOptions {
90    allWarningsAsErrors = true
91    freeCompilerArgs += [ "-opt-in=kotlin.ExperimentalStdlibApi" ]
92    jvmTarget = 11
93  }
94}
95
96// Imports a shared library from the main project. The library and its classes
97// will be shadowed in the plugin's artifact.
98tasks.register("importSharedLib").configure {
99  def outputDir = file("${project.projectDir}/libs")
100  outputs.dir(outputDir)
101  doLast {
102    def buildCmd = 'bazel'
103    def buildDir = 'bazel-bin'
104    def findGenFilesParent
105    findGenFilesParent = { File dir ->
106      if (dir == null || !dir.isDirectory()) {
107        return null
108      }
109      if (new File(dir, buildDir).exists()) {
110        return dir
111      } else {
112        return findGenFilesParent(dir.parentFile)
113      }
114    }
115    // Build shared lib
116    def bazelOutput = new ByteArrayOutputStream()
117    def buildResult = exec {
118      commandLine buildCmd, 'build', 'import-shared-lib'
119      standardOutput = bazelOutput
120      errorOutput = bazelOutput
121    }
122    buildResult.assertNormalExitValue()
123    // Find shared lib Jar in build directory.
124    def genFilesDir = findGenFilesParent(project.buildFile.parentFile)
125    if (genFilesDir == null) {
126      throw new GradleException("Couldn't find build folder '$buildDir'")
127    }
128    def libPath = bazelOutput.toString().split('\n')
129      .find { line -> line.contains("$buildDir/") }.trim()
130    def inputFile = file("$genFilesDir/$libPath")
131    def outputFile = file("$outputDir/${inputFile.name}")
132    outputFile << inputFile.newInputStream()
133  }
134}
135tasks.getByName('compileKotlin').dependsOn('importSharedLib')
136
137// Task that generates a top-level property containing the version of the
138// project so that it can be used in code and at runtime.
139def pluginVersionOutDir = file("$buildDir/generated/source/plugin-version/")
140tasks.register("generatePluginVersionSource").configure {
141  def version = getPublishVersion()
142  inputs.property('version', version)
143  outputs.dir(pluginVersionOutDir)
144  doLast {
145    def versionFile =
146      file("$pluginVersionOutDir/dagger/hilt/android/plugin/Version.kt")
147    versionFile.parentFile.mkdirs()
148    versionFile.text = """
149      // Generated file. Do not edit!
150      package dagger.hilt.android.plugin
151
152      val HILT_VERSION = "${version}"
153    """.stripIndent()
154  }
155}
156sourceSets.main.java.srcDir pluginVersionOutDir
157tasks.getByName('compileKotlin').dependsOn('generatePluginVersionSource')
158
159// Create sources Jar from main kotlin sources
160tasks.register("sourcesJar", Jar).configure {
161  group = JavaBasePlugin.DOCUMENTATION_GROUP
162  description = "Assembles sources JAR"
163  archiveClassifier.set("sources")
164  from(sourceSets["main"].allSource)
165  dependsOn('generatePluginVersionSource')
166}
167
168// Create javadoc Jar. The jar is empty since we don't really have docs
169// for this plugin but this is required to upload to Sonatype.
170// https://central.sonatype.org/pages/requirements.html#supply-javadoc-and-sources
171tasks.register("javadocJar", Jar).configure {
172  group = JavaBasePlugin.DOCUMENTATION_GROUP
173  description = "Assembles javadoc JAR"
174  archiveClassifier.set("javadoc")
175}
176
177// Disable Gradle metadata publication.
178tasks.withType(GenerateModuleMetadata) {
179  enabled = false
180}
181
182// TODO(danysantiago): Use POM template in tools/ to avoid duplicating lines.
183publishing {
184  publications {
185    plugin(MavenPublication) {
186      artifactId = pluginArtifactId
187      version = getPublishVersion()
188      from components.kotlin
189      artifact(shadowJar)
190      artifact(sourcesJar)
191      artifact(javadocJar)
192      pom {
193        addPomTemplate(owner)
194      }
195    }
196    // https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_markers
197    pluginMarker(MavenPublication) {
198      groupId = pluginId
199      artifactId = "${pluginId}.gradle.plugin"
200      version = getPublishVersion()
201      pom {
202        addPomTemplate(owner)
203        withXml {
204          def dependencyNode =
205                  asNode().appendNode("dependencies").appendNode("dependency")
206          dependencyNode.appendNode("groupId", group)
207          dependencyNode.appendNode("artifactId", pluginArtifactId)
208          dependencyNode.appendNode("version", getPublishVersion())
209        }
210      }
211    }
212  }
213  // Publish to build output repository.
214  repositories {
215    maven {
216      url = uri("$buildDir/repo")
217    }
218  }
219}
220
221group = 'com.google.dagger'
222
223// TODO(danysantiago): Use POM template in tools/ to avoid duplicating lines.
224def addPomTemplate(pom) {
225  pom.name = 'Hilt Android Gradle Plugin'
226  pom.description = 'A fast dependency injector for Android and Java.'
227  pom.url = 'https://github.com/google/dagger'
228  pom.scm {
229    url = 'https://github.com/google/dagger/'
230    connection = 'scm:git:git://github.com/google/dagger.git'
231    developerConnection = 'scm:git:ssh://[email protected]/google/dagger.git'
232    tag = 'HEAD'
233  }
234  pom.issueManagement {
235    system = 'GitHub Issues'
236    url = 'https://github.com/google/dagger/issues'
237  }
238  pom.licenses {
239    license {
240      name = 'Apache 2.0'
241      url = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
242    }
243  }
244  pom.organization {
245    name = 'Google, Inc.'
246    url = 'https://www.google.com'
247  }
248  pom.withXml {
249    def projectNode = asNode()
250    // Adds:
251    // <parent>
252    //   <groupId>org.sonatype.oss</groupId>
253    //   <artifactId>oss-parent</artifactId>
254    //   <version>7</version>
255    // </parent>
256    def parentNode = projectNode.appendNode('parent')
257    parentNode.appendNode('groupId', 'org.sonatype.oss')
258    parentNode.appendNode('artifactId', 'oss-parent')
259    parentNode.appendNode('version', '7')
260    // Adds scm->tag because for some reason the DSL API does not.
261    // <scm>
262    //   <tag>HEAD</tag>
263    // </scm>
264    projectNode.get('scm').first().appendNode('tag', 'HEAD')
265  }
266}
267
268def getPublishVersion() {
269  def publishVersion = findProperty("PublishVersion")
270  return (publishVersion != null) ? publishVersion : "LOCAL-SNAPSHOT"
271}
272
273def checkJarFile(File jarFile, String... allowedPrefixes) {
274  def zip = new ZipFile(jarFile)
275  try {
276    Enumeration<ZipEntry> list = zip.entries()
277    while (list.hasMoreElements()) {
278      String entry = list.nextElement().name
279      if (!allowedPrefixes.any { entry.startsWith(it) }) {
280        throw new GradleException(
281          "Found a file that is not in " +
282          "${ allowedPrefixes.collect { "'$it'" }.join('/') }: $entry")
283      }
284    }
285  } finally {
286      zip.close()
287  }
288}
289