Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hilt Gradle Plugin KSP configuration to support KSP2 #4546

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package dagger.hilt.android.plugin.util

import com.android.build.api.variant.ComponentIdentity
import com.google.devtools.ksp.gradle.KspTaskJvm
import com.google.devtools.ksp.gradle.KspAATask
import com.google.devtools.ksp.gradle.KspTask
import kotlin.reflect.KClass
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.compile.JavaCompile
Expand Down Expand Up @@ -73,7 +75,7 @@ internal fun addKspTaskProcessorOptions(
variantIdentity: ComponentIdentity,
produceArgProvider: (Task) -> CommandLineArgumentProvider
) = project.plugins.withId("com.google.devtools.ksp") {
checkClass("com.google.devtools.ksp.gradle.KspTaskJvm") {
check(kspOneTaskClass != null || kspTwoTaskClass != null) {
"""
The KSP plugin was detected to be applied but its task class could not be found.

Expand All @@ -85,13 +87,25 @@ internal fun addKspTaskProcessorOptions(
See https://github.com/google/dagger/issues/3965 for more details.
""".trimIndent()
}
project.tasks.withType(KspTaskJvm::class.java).configureEach { task ->
if (task.name == "ksp${variantIdentity.name.capitalize()}Kotlin" ||
fun <T : Task> configureEach(
kclass: KClass<T>,
block: T.(CommandLineArgumentProvider) -> Unit
) {
project.tasks.withType(kclass.java).configureEach { task ->
if (task.name == "ksp${variantIdentity.name.capitalize()}Kotlin" ||
// Task names in shared/src/AndroidMain in KMP projects has a platform suffix.
task.name == "ksp${variantIdentity.name.capitalize()}KotlinAndroid") {
task.commandLineArgumentProviders.add(produceArgProvider.invoke(task))
val argProvider = produceArgProvider.invoke(task)
task.block(argProvider)
}
}
}
if (kspOneTaskClass != null) {
configureEach(KspTask::class) { commandLineArgumentProviders.add(it) }
}
if (kspTwoTaskClass != null) {
configureEach(KspAATask::class) { commandLineArgumentProviders.add(it) }
}
}

private inline fun checkClass(fqn: String, msg: () -> String) {
Expand All @@ -102,10 +116,21 @@ private inline fun checkClass(fqn: String, msg: () -> String) {
}
}

internal fun Task.isKspTask(): Boolean = try {
val kspTaskClass = Class.forName("com.google.devtools.ksp.gradle.KspTask")
kspTaskClass.isAssignableFrom(this::class.java)
} catch (ex: ClassNotFoundException) {
false
}
private val kspOneTaskClass =
try {
Class.forName("com.google.devtools.ksp.gradle.KspTask")
} catch (ex: ClassNotFoundException) {
null
}

private val kspTwoTaskClass =
try {
Class.forName("com.google.devtools.ksp.gradle.KspAATask")
} catch (ex: ClassNotFoundException) {
null
}

internal fun Task.isKspTask() =
kspOneTaskClass?.isAssignableFrom(this::class.java) == true ||
kspTwoTaskClass?.isAssignableFrom(this::class.java) == true

Loading