Skip to content

Commit f42ef5c

Browse files
bcorsoDagger Team
authored andcommitted
Update HiltGradlePlugin to AGP 9.
This required a number of changes: * Update Gradle 8.14.3 -> 9.2.0 * Update shadow plugin from `com.github.johnrengelman.shadow:8.1.1` to 'com.gradleup.shadow:9.2.2' (old artifact doesn't support Gradle 9). * Update HiltGradlePlugin to 9.0.0-beta02: * Migrate `BaseExtension` to `AndroidComponentsExtension` * Migrate `BaseVariant` to `Component` * Migrate `variant.javaCompileOutput.classpath` to `variant.compileClasspath` * Migrate `variant.javaCompileOutput.destinationDirectory` to `variant.artifacts.use(HiltCopyTask).toGet()` * Migrate `variant.javaCompileOutput.options.bootstrapClasspath` with `androidExtension.sdkComponents.bootClasspath` * Migrate `variant.javaCompileOutput.options.compilerArgumentProviders` with `variant.javaCompilation.annotationProcessor.argumentProviders` * `project.buildDir = ...` to `project.layout.buildDirectory.set(...)` * Update HiltGradlePlugin tests to use AGP 9.0.0-beta02: * Migrate to builtin kotlin * remove 'kotlin-android' * replace 'kotlin-kapt' with 'legacy-kapt' * Migrate javatests/artifacts/hilt-android/simple/app/build.gradle to kts and replace GradleVersion with custom AgpVersion. The GradleVersion does not work with APG versions like 9.0.0-beta02, so I had to do this with custom parsing. * remove `android.kotlinOptions` (removed in AGP 9) RELNOTES=N/A PiperOrigin-RevId: 837566667
1 parent 34dab76 commit f42ef5c

File tree

38 files changed

+500
-438
lines changed

38 files changed

+500
-438
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ concurrency:
1414
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}
1515

1616
env:
17-
USE_AGP_VERSION: '8.13.0'
17+
USE_AGP_VERSION: '9.0.0-beta02'
1818
USE_JAVA_DISTRIBUTION: 'zulu'
1919
USE_JAVA_VERSION_FOR_BAZEL: '21'
2020
USE_JAVA_VERSION_FOR_GRADLE: '21'

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ concurrency:
1313
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}
1414

1515
env:
16-
USE_AGP_VERSION: '8.13.0'
16+
USE_AGP_VERSION: '9.0.0-beta02'
1717
USE_JAVA_DISTRIBUTION: 'zulu'
1818
USE_JAVA_VERSION_FOR_BAZEL: '21'
1919
USE_JAVA_VERSION_FOR_GRADLE: '21'

java/dagger/hilt/android/plugin/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
33
kotlin_version = "2.2.0"
4-
agp_version = System.getenv('AGP_VERSION')
4+
agp_version = System.getenv('AGP_VERSION') ?: "9.0.0-beta02"
55
ksp_version = "$kotlin_version-2.0.2"
66
pluginArtifactId = 'hilt-android-gradle-plugin'
77
pluginId = 'com.google.dagger.hilt.android'
@@ -12,7 +12,7 @@ plugins {
1212
id 'org.jetbrains.kotlin.jvm' version "$kotlin_version"
1313
id 'java-gradle-plugin'
1414
id 'maven-publish'
15-
id 'com.github.johnrengelman.shadow' version '8.1.1'
15+
id 'com.gradleup.shadow' version '9.2.2'
1616
id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
1717
}
1818

@@ -24,10 +24,10 @@ allprojects {
2424
}
2525

2626
// Avoids conflict with BUILD file
27-
project.buildDir = 'buildOut'
27+
project.layout.buildDirectory.set(file('buildOut'))
2828

29-
subprojects {
30-
project.buildDir = 'buildOut'
29+
subprojects {subproject ->
30+
subproject.layout.buildDirectory.set(file('buildOut'))
3131
afterEvaluate {
3232
dependencies {
3333
// This is needed to align older versions of kotlin-stdlib.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
`kotlin-dsl`
3+
}
4+
5+
repositories {
6+
google()
7+
mavenCentral()
8+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2025 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+
*/
16+
17+
package dagger.hilt.android.plugin.task
18+
19+
import java.io.ByteArrayOutputStream
20+
import java.io.File
21+
import javax.inject.Inject
22+
import org.gradle.api.DefaultTask
23+
import org.gradle.api.GradleException
24+
import org.gradle.api.file.DirectoryProperty
25+
import org.gradle.api.tasks.OutputDirectory
26+
import org.gradle.api.tasks.TaskAction
27+
import org.gradle.process.ExecOperations
28+
29+
abstract class ImportSharedLibTask @Inject constructor(
30+
private val execOperations: ExecOperations
31+
) : DefaultTask() {
32+
33+
@get:OutputDirectory
34+
abstract val outputDir: DirectoryProperty
35+
36+
@TaskAction
37+
fun execute() {
38+
val bazelOutput = ByteArrayOutputStream()
39+
val buildResult = execOperations.exec {
40+
this.commandLine(BUILD_CMD, "build", "import-shared-lib")
41+
this.standardOutput = bazelOutput
42+
this.errorOutput = bazelOutput
43+
}
44+
buildResult.assertNormalExitValue()
45+
46+
val genFilesDir = project.buildFile.parentFile.findFileInPath(BUILD_DIR)
47+
?: throw GradleException("Couldn't find build folder '$BUILD_DIR'.")
48+
49+
val libPath = bazelOutput.toString().split('\n').find { it.contains("$BUILD_DIR/")}?.trim()
50+
?: throw GradleException("Couldn't find library path in $BUILD_CMD's output ($BUILD_DIR).")
51+
52+
val inputFile = project.file("$genFilesDir/$libPath")
53+
val outputFile = outputDir.file(inputFile.name).get().asFile
54+
inputFile.inputStream().use { input ->
55+
outputFile.outputStream().use { output ->
56+
input.copyTo(output)
57+
}
58+
}
59+
}
60+
61+
companion object {
62+
const val BUILD_CMD = "bazel"
63+
const val BUILD_DIR = "bazel-bin"
64+
65+
/** Finds the file in the current directory, its parent directories, or returns null. */
66+
private fun File?.findFileInPath(fileName: String): File? {
67+
if (this == null || !isDirectory) {
68+
return null
69+
}
70+
return if (File(this, fileName).exists()) {
71+
this
72+
} else {
73+
parentFile.findFileInPath(fileName)
74+
}
75+
}
76+
}
77+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

java/dagger/hilt/android/plugin/main/build.gradle

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
import java.util.zip.ZipEntry
1717
import java.util.zip.ZipFile
18-
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
18+
import dagger.hilt.android.plugin.task.ImportSharedLibTask
1919
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
2020

2121
plugins {
2222
id 'org.jetbrains.kotlin.jvm'
2323
id 'java-gradle-plugin'
2424
id 'maven-publish'
25-
id 'com.github.johnrengelman.shadow'
25+
id 'com.gradleup.shadow'
2626
}
2727

2828
configurations {
@@ -67,6 +67,7 @@ dependencies {
6767
// Use compile-only for other plugin dependencies to avoid brining those
6868
// to projects that don't use them.
6969
compileOnly "com.android.tools.build:gradle:$agp_version"
70+
compileOnly "com.android.legacy-kapt:com.android.legacy-kapt.gradle.plugin:$agp_version"
7071
compileOnly "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
7172
compileOnly "com.google.devtools.ksp:symbol-processing-gradle-plugin:$ksp_version"
7273

@@ -79,6 +80,7 @@ dependencies {
7980
testImplementation 'com.google.truth:truth:1.0.1'
8081
testImplementation 'org.javassist:javassist:3.26.0-GA'
8182
testPluginCompile "com.android.tools.build:gradle:$agp_version"
83+
testPluginCompile "com.android.legacy-kapt:com.android.legacy-kapt.gradle.plugin:$agp_version"
8284
testPluginCompile "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8385
testPluginCompile "com.google.devtools.ksp:symbol-processing-gradle-plugin:$ksp_version"
8486
}
@@ -105,42 +107,8 @@ kotlin {
105107

106108
// Imports a shared library from the main project. The library and its classes
107109
// will be shadowed in the plugin's artifact.
108-
tasks.register("importSharedLib").configure {
109-
def outputDir = file("${project.projectDir}/libs")
110-
outputs.dir(outputDir)
111-
doLast {
112-
def buildCmd = 'bazel'
113-
def buildDir = 'bazel-bin'
114-
def findGenFilesParent
115-
findGenFilesParent = { File dir ->
116-
if (dir == null || !dir.isDirectory()) {
117-
return null
118-
}
119-
if (new File(dir, buildDir).exists()) {
120-
return dir
121-
} else {
122-
return findGenFilesParent(dir.parentFile)
123-
}
124-
}
125-
// Build shared lib
126-
def bazelOutput = new ByteArrayOutputStream()
127-
def buildResult = exec {
128-
commandLine buildCmd, 'build', 'import-shared-lib'
129-
standardOutput = bazelOutput
130-
errorOutput = bazelOutput
131-
}
132-
buildResult.assertNormalExitValue()
133-
// Find shared lib Jar in build directory.
134-
def genFilesDir = findGenFilesParent(project.buildFile.parentFile)
135-
if (genFilesDir == null) {
136-
throw new GradleException("Couldn't find build folder '$buildDir'")
137-
}
138-
def libPath = bazelOutput.toString().split('\n')
139-
.find { line -> line.contains("$buildDir/") }.trim()
140-
def inputFile = file("$genFilesDir/$libPath")
141-
def outputFile = file("$outputDir/${inputFile.name}")
142-
outputFile << inputFile.newInputStream()
143-
}
110+
tasks.register("importSharedLib", ImportSharedLibTask) {
111+
outputDir.set(file("${project.projectDir}/libs"))
144112
}
145113
tasks.getByName('compileKotlin').dependsOn('importSharedLib')
146114

0 commit comments

Comments
 (0)