Skip to content

Commit cf8e452

Browse files
bcorsoDagger Team
authored andcommitted
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: 837095234
1 parent 46f8142 commit cf8e452

File tree

12 files changed

+98
-53
lines changed

12 files changed

+98
-53
lines changed
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+
}

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

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
import dagger.hilt.android.plugin.task.ImportSharedLibTask
1618
import java.util.zip.ZipEntry
1719
import java.util.zip.ZipFile
1820
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
@@ -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

java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ class HiltGradlePlugin @Inject constructor(private val providers: ProviderFactor
231231
description = "Hilt aggregated compile only dependencies for '${variant.name}'"
232232
isCanBeConsumed = false
233233
isCanBeResolved = true
234-
isVisible = false
235234

236235
// Add the JavaCompile task classpath and output dir to the config, the task's classpath
237236
// will contain:
@@ -329,7 +328,7 @@ class HiltGradlePlugin @Inject constructor(private val providers: ProviderFactor
329328
description = "Hilt annotation processor classpath for '${variant.name}'"
330329
isCanBeConsumed = false
331330
isCanBeResolved = true
332-
isVisible = false
331+
333332
// Add user annotation processor configuration, so that SPI plugins and other processors
334333
// are discoverable.
335334
val apConfigurations: List<Configuration> = buildList {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="liba">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43
</manifest>
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="libc">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43
</manifest>

java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
package="simple.app">
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1817
<application android:name=".SimpleApp" android:label="Flavored App">
1918
</application>
2019
</manifest>

java/dagger/hilt/android/plugin/main/src/test/data/flavored-project/feature/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
package="simple.library">
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1817
</manifest>

java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
package="simple.app">
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1817
<application android:name=".SimpleApp" android:label="Flavored App">
1918
<receiver android:name=".SimpleReceiver" android:exported="false">
2019
</receiver>

java/dagger/hilt/android/plugin/main/src/test/data/simple-project-for-agp-test/feature/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
package="simple.library">
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1817
</manifest>

0 commit comments

Comments
 (0)