Skip to content

Commit 320bd41

Browse files
bcorsoDagger Team
authored andcommitted
Update ImportSharedLibTask to use AGP 9 APIs
The Project#exec() method is removed in AGP 9. The recommended migration is to define a separate task class that injects ExecOperations to do the execution. This CL also removes the unnecessary `package` declarations from AndroidManifest, which is ignored as of Gradle 8+. RELNOTES=N/A PiperOrigin-RevId: 837095234
1 parent e029432 commit 320bd41

File tree

11 files changed

+98
-51
lines changed

11 files changed

+98
-51
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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 libPath = bazelOutput.toString().split('\n').find { line ->
47+
line.contains("$BUILD_DIR/")
48+
}?.trim() ?: throw GradleException("Could not find library path in $BUILD_CMD output")
49+
50+
val genFilesDir = project.buildFile.parentFile.findFileInPath(BUILD_DIR)
51+
?: throw GradleException("Couldn't find build folder '$BUILD_DIR'")
52+
53+
val inputFile = project.file("$genFilesDir/$libPath")
54+
val outputFile = outputDir.file(inputFile.name).get().asFile
55+
inputFile.inputStream().use { input ->
56+
outputFile.outputStream().use { output ->
57+
input.copyTo(output)
58+
}
59+
}
60+
}
61+
62+
companion object {
63+
const val BUILD_CMD = "bazel"
64+
const val BUILD_DIR = "bazel-bin"
65+
66+
/** Finds the file in the current directory, its parent directories, or returns null. */
67+
private fun File?.findFileInPath(fileName: String): File? {
68+
if (this == null || !isDirectory) {
69+
return null
70+
}
71+
return if (File(this, fileName).exists()) {
72+
this
73+
} else {
74+
parentFile.findFileInPath(fileName)
75+
}
76+
}
77+
}
78+
}

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

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>

java/dagger/hilt/android/plugin/main/src/test/data/simple-project/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">
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1817
</manifest>

0 commit comments

Comments
 (0)