Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions java/dagger/hilt/android/plugin/buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
`kotlin-dsl`
}

repositories {
google()
mavenCentral()
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
40 changes: 4 additions & 36 deletions java/dagger/hilt/android/plugin/main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<Configuration> = buildList {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="liba">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="libc">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.app">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:name=".SimpleApp" android:label="Flavored App">
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.library">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.app">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:name=".SimpleApp" android:label="Flavored App">
<receiver android:name=".SimpleReceiver" android:exported="false">
</receiver>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.library">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class GradleTestRunner(val tempFolder: TemporaryFolder) {
writeText(
"""
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="minimal">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name="${appClassName ?: "android.app.Application"}"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
Expand Down
Loading