Skip to content

Commit

Permalink
Filter compiler args passed to hiltJavaCompile
Browse files Browse the repository at this point in the history
Passing compiler args from other libraries are causing overlapping outputs and
cache misses. Here we filter out args we don't use in hope to reduce the chance
of that.

Fixes: #3546
RELNOTES=N/A
PiperOrigin-RevId: 492215422
  • Loading branch information
kuanyingchou authored and Dagger Team committed Dec 7, 2022
1 parent 4d5173b commit 2e1f780
Showing 1 changed file with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.TestExtension
import com.android.build.gradle.TestedExtension
import com.android.build.gradle.api.AndroidBasePlugin
import com.android.build.gradle.tasks.JdkImageInput
import dagger.hilt.android.plugin.task.AggregateDepsTask
import dagger.hilt.android.plugin.task.HiltTransformTestClassesTask
import dagger.hilt.android.plugin.util.AggregatedPackagesTransform
Expand All @@ -44,6 +45,7 @@ import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.attributes.Attribute
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.process.CommandLineArgumentProvider
import org.gradle.util.GradleVersion
Expand Down Expand Up @@ -382,9 +384,13 @@ class HiltGradlePlugin @Inject constructor(
getInputClasspath(DAGGER_ARTIFACT_TYPE_VALUE).plus(mainBootstrapClasspath)
// Copies argument providers from original task, which should contain the JdkImageInput
variant.javaCompileProvider.get().let { originalCompileTask ->
originalCompileTask.options.compilerArgumentProviders.forEach {
compileTask.options.compilerArgumentProviders.add(it)
}
originalCompileTask.options.compilerArgumentProviders
.filter {
it is HiltCommandLineArgumentProvider || it is JdkImageInput
}
.forEach {
compileTask.options.compilerArgumentProviders.add(it)
}
}
compileTask.options.compilerArgs.add("-XDstringConcat=inline")
} else {
Expand Down Expand Up @@ -439,43 +445,47 @@ class HiltGradlePlugin @Inject constructor(

private fun configureProcessorFlags(project: Project, hiltExtension: HiltExtension) {
val androidExtension = project.baseExtension() ?: error("Android BaseExtension not found.")
androidExtension.defaultConfig.javaCompileOptions.annotationProcessorOptions.apply {

val projectType = when (androidExtension) {
is AppExtension -> GradleProjectType.APP
is LibraryExtension -> GradleProjectType.LIBRARY
is TestExtension -> GradleProjectType.TEST
else -> error("Hilt plugin does not know how to configure '$this'")
}

// Pass annotation processor flags via a CommandLineArgumentProvider so that plugin
// options defined in the extension are populated from the user's build file. Checking the
// option too early would make it seem like it is never set.
androidExtension.defaultConfig.javaCompileOptions.annotationProcessorOptions
.compilerArgumentProvider(HiltCommandLineArgumentProvider(hiltExtension, projectType))
}

private class HiltCommandLineArgumentProvider(
private val hiltExtension: HiltExtension,
private val projectType: GradleProjectType
): CommandLineArgumentProvider {
override fun asArguments() = mutableListOf<String>().apply {
// Pass annotation processor flag to enable Dagger's fast-init, the best mode for Hilt.
argument("dagger.fastInit", "enabled")
add(createArgument("dagger.fastInit", "enabled"))
// Pass annotation processor flag to disable @AndroidEntryPoint superclass validation.
argument("dagger.hilt.android.internal.disableAndroidSuperclassValidation", "true")
add(createArgument("dagger.hilt.android.internal.disableAndroidSuperclassValidation", "true"))

val projectType = when (androidExtension) {
is AppExtension -> GradleProjectType.APP
is LibraryExtension -> GradleProjectType.LIBRARY
is TestExtension -> GradleProjectType.TEST
else -> error("Hilt plugin does not know how to configure '$this'")
add(createArgument("dagger.hilt.android.internal.projectType", projectType.toString()))

// Pass annotation processor flag to disable the aggregating processor if aggregating
// task is enabled.
if (hiltExtension.enableAggregatingTask) {
add(createArgument("dagger.hilt.internal.useAggregatingRootProcessor", "false"))
}
// Pass annotation processor flag to disable cross compilation root validation.
// The plugin option duplicates the processor flag because it is an input of the
// aggregating task.
if (hiltExtension.disableCrossCompilationRootValidation) {
add(createArgument("dagger.hilt.disableCrossCompilationRootValidation", "true"))
}
argument("dagger.hilt.android.internal.projectType", projectType.toString())

// Pass certain annotation processor flags via a CommandLineArgumentProvider so that plugin
// options defined in the extension are populated from the user's build file. Checking the
// option too early would make it seem like it is never set.
compilerArgumentProvider(
// Suppress due to https://docs.gradle.org/7.2/userguide/validation_problems.html#implementation_unknown
@Suppress("ObjectLiteralToLambda")
object : CommandLineArgumentProvider {
override fun asArguments() = mutableListOf<String>().apply {
// Pass annotation processor flag to disable the aggregating processor if aggregating
// task is enabled.
if (hiltExtension.enableAggregatingTask) {
add("-Adagger.hilt.internal.useAggregatingRootProcessor=false")
}
// Pass annotation processor flag to disable cross compilation root validation.
// The plugin option duplicates the processor flag because it is an input of the
// aggregating task.
if (hiltExtension.disableCrossCompilationRootValidation) {
add("-Adagger.hilt.disableCrossCompilationRootValidation=true")
}
}
}
)
}

private fun createArgument(key: String, value: String): String = "-A$key=$value"
}

private fun verifyDependencies(project: Project) {
Expand Down

0 comments on commit 2e1f780

Please sign in to comment.