From d15021c4aa234cf73e3b4f0c7bd0d1e5f5e59925 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Thu, 4 Dec 2025 13:16:21 -0800 Subject: [PATCH] Update ImportSharedLibTask to use AGP 9 APIs This CL: * Removes the deprecated Project#exec() method and migrates to declaring a task that injects `ExecOperations` to do the execution. * Removes deprecated `isVisible` usages in configurations. * Removes unnecessary `package` declarations from AndroidManifest, which is ignored as of Gradle 8+. RELNOTES=N/A PiperOrigin-RevId: 840372053 --- .../android/plugin/buildSrc/build.gradle.kts | 8 ++ .../plugin/task/ImportSharedLibTask.kt | 77 +++++++++++++++++++ .../hilt/android/plugin/main/build.gradle | 40 +--------- .../hilt/android/plugin/HiltGradlePlugin.kt | 3 +- .../src/main/AndroidManifest.xml | 3 +- .../src/main/AndroidManifest.xml | 3 +- .../app/src/main/AndroidManifest.xml | 3 +- .../feature/src/main/AndroidManifest.xml | 3 +- .../app/src/main/AndroidManifest.xml | 3 +- .../feature/src/main/AndroidManifest.xml | 3 +- .../src/main/AndroidManifest.xml | 3 +- .../main/src/test/kotlin/GradleTestRunner.kt | 2 +- 12 files changed, 98 insertions(+), 53 deletions(-) create mode 100644 java/dagger/hilt/android/plugin/buildSrc/build.gradle.kts create mode 100644 java/dagger/hilt/android/plugin/buildSrc/src/main/kotlin/dagger/hilt/android/plugin/task/ImportSharedLibTask.kt diff --git a/java/dagger/hilt/android/plugin/buildSrc/build.gradle.kts b/java/dagger/hilt/android/plugin/buildSrc/build.gradle.kts new file mode 100644 index 00000000000..f81561ee0f1 --- /dev/null +++ b/java/dagger/hilt/android/plugin/buildSrc/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + `kotlin-dsl` +} + +repositories { + google() + mavenCentral() +} \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/buildSrc/src/main/kotlin/dagger/hilt/android/plugin/task/ImportSharedLibTask.kt b/java/dagger/hilt/android/plugin/buildSrc/src/main/kotlin/dagger/hilt/android/plugin/task/ImportSharedLibTask.kt new file mode 100644 index 00000000000..e76ae6a3c59 --- /dev/null +++ b/java/dagger/hilt/android/plugin/buildSrc/src/main/kotlin/dagger/hilt/android/plugin/task/ImportSharedLibTask.kt @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2025 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.hilt.android.plugin.task + +import java.io.ByteArrayOutputStream +import java.io.File +import javax.inject.Inject +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction +import org.gradle.process.ExecOperations + +abstract class ImportSharedLibTask @Inject constructor( + private val execOperations: ExecOperations +) : DefaultTask() { + + @get:OutputDirectory + abstract val outputDir: DirectoryProperty + + @TaskAction + fun execute() { + val bazelOutput = ByteArrayOutputStream() + val buildResult = execOperations.exec { + this.commandLine(BUILD_CMD, "build", "import-shared-lib") + this.standardOutput = bazelOutput + this.errorOutput = bazelOutput + } + buildResult.assertNormalExitValue() + + val genFilesDir = project.buildFile.parentFile.findFileInPath(BUILD_DIR) + ?: throw GradleException("Couldn't find build folder '$BUILD_DIR'.") + + val libPath = bazelOutput.toString().split('\n').find { it.contains("$BUILD_DIR/")}?.trim() + ?: throw GradleException("Couldn't find library path in $BUILD_CMD's output ($BUILD_DIR).") + + val inputFile = project.file("$genFilesDir/$libPath") + val outputFile = outputDir.file(inputFile.name).get().asFile + inputFile.inputStream().use { input -> + outputFile.outputStream().use { output -> + input.copyTo(output) + } + } + } + + companion object { + const val BUILD_CMD = "bazel" + const val BUILD_DIR = "bazel-bin" + + /** Finds the file in the current directory, its parent directories, or returns null. */ + private fun File?.findFileInPath(fileName: String): File? { + if (this == null || !isDirectory) { + return null + } + return if (File(this, fileName).exists()) { + this + } else { + parentFile.findFileInPath(fileName) + } + } + } +} \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/build.gradle b/java/dagger/hilt/android/plugin/main/build.gradle index 1384af777c1..d288985f56f 100644 --- a/java/dagger/hilt/android/plugin/main/build.gradle +++ b/java/dagger/hilt/android/plugin/main/build.gradle @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import dagger.hilt.android.plugin.task.ImportSharedLibTask import java.util.zip.ZipEntry import java.util.zip.ZipFile import org.jetbrains.kotlin.gradle.dsl.JvmTarget @@ -105,42 +107,8 @@ kotlin { // Imports a shared library from the main project. The library and its classes // will be shadowed in the plugin's artifact. -tasks.register("importSharedLib").configure { - def outputDir = file("${project.projectDir}/libs") - outputs.dir(outputDir) - doLast { - def buildCmd = 'bazel' - def buildDir = 'bazel-bin' - def findGenFilesParent - findGenFilesParent = { File dir -> - if (dir == null || !dir.isDirectory()) { - return null - } - if (new File(dir, buildDir).exists()) { - return dir - } else { - return findGenFilesParent(dir.parentFile) - } - } - // Build shared lib - def bazelOutput = new ByteArrayOutputStream() - def buildResult = exec { - commandLine buildCmd, 'build', 'import-shared-lib' - standardOutput = bazelOutput - errorOutput = bazelOutput - } - buildResult.assertNormalExitValue() - // Find shared lib Jar in build directory. - def genFilesDir = findGenFilesParent(project.buildFile.parentFile) - if (genFilesDir == null) { - throw new GradleException("Couldn't find build folder '$buildDir'") - } - def libPath = bazelOutput.toString().split('\n') - .find { line -> line.contains("$buildDir/") }.trim() - def inputFile = file("$genFilesDir/$libPath") - def outputFile = file("$outputDir/${inputFile.name}") - outputFile << inputFile.newInputStream() - } +tasks.register("importSharedLib", ImportSharedLibTask) { + outputDir.set(file("${project.projectDir}/libs")) } tasks.getByName('compileKotlin').dependsOn('importSharedLib') diff --git a/java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt b/java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt index d1b43be5c52..091309603f5 100644 --- a/java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt +++ b/java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt @@ -231,7 +231,6 @@ class HiltGradlePlugin @Inject constructor(private val providers: ProviderFactor description = "Hilt aggregated compile only dependencies for '${variant.name}'" isCanBeConsumed = false isCanBeResolved = true - isVisible = false // Add the JavaCompile task classpath and output dir to the config, the task's classpath // will contain: @@ -329,7 +328,7 @@ class HiltGradlePlugin @Inject constructor(private val providers: ProviderFactor description = "Hilt annotation processor classpath for '${variant.name}'" isCanBeConsumed = false isCanBeResolved = true - isVisible = false + // Add user annotation processor configuration, so that SPI plugins and other processors // are discoverable. val apConfigurations: List = buildList { diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryA/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryA/src/main/AndroidManifest.xml index e704d4ad270..74b7379f732 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryA/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryA/src/main/AndroidManifest.xml @@ -1,4 +1,3 @@ - + \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryC/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryC/src/main/AndroidManifest.xml index a4967e32571..74b7379f732 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryC/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/android-libraryC/src/main/AndroidManifest.xml @@ -1,4 +1,3 @@ - + \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/app/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/app/src/main/AndroidManifest.xml index 0ce23153704..068f230227a 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/app/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/app/src/main/AndroidManifest.xml @@ -13,8 +13,7 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/feature/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/feature/src/main/AndroidManifest.xml index fc219c87776..17c6cc1e68d 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/feature/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/feature/src/main/AndroidManifest.xml @@ -13,6 +13,5 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/app/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/app/src/main/AndroidManifest.xml index ce521fba8fd..8aabfe52b43 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/app/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/app/src/main/AndroidManifest.xml @@ -13,8 +13,7 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/feature/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/feature/src/main/AndroidManifest.xml index ebf4852c23a..003b53766b5 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/feature/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/feature/src/main/AndroidManifest.xml @@ -13,6 +13,5 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/src/test/data/simple-project/src/main/AndroidManifest.xml b/java/dagger/hilt/android/plugin/main/src/test/data/simple-project/src/main/AndroidManifest.xml index 29060e05ce5..a38c740c192 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/data/simple-project/src/main/AndroidManifest.xml +++ b/java/dagger/hilt/android/plugin/main/src/test/data/simple-project/src/main/AndroidManifest.xml @@ -13,6 +13,5 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + \ No newline at end of file diff --git a/java/dagger/hilt/android/plugin/main/src/test/kotlin/GradleTestRunner.kt b/java/dagger/hilt/android/plugin/main/src/test/kotlin/GradleTestRunner.kt index 8b935fe0106..1966ec81670 100644 --- a/java/dagger/hilt/android/plugin/main/src/test/kotlin/GradleTestRunner.kt +++ b/java/dagger/hilt/android/plugin/main/src/test/kotlin/GradleTestRunner.kt @@ -219,7 +219,7 @@ class GradleTestRunner(val tempFolder: TemporaryFolder) { writeText( """ - +