Skip to content
Open
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a Gradle test to https://github.com/google/dagger/tree/master/javatests/artifacts/hilt-android to ensure we have test coverage for this.

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package dagger.hilt.processor.internal.optionvalues;

/**
* Valid Gradle project type values. Note that we exclude 'com.android.feature' as Hilt doesn't
* support it for now.
* Valid Gradle project type values. Note that 'com.android.dynamic-feature' (formerly
* 'com.android.feature') is supported in a limited capacity -- dynamic feature modules are
* treated as libraries and can only contribute test-specific graphs, not the main app graph.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will want to enforce this, e.g. by having a lint check to ensures no @InstallIn usages in the main source set of a DFM, i.e. they are only allowed within the test source sets. See https://github.com/google/dagger/tree/master/dagger-lint.

This prevents cases where your DFM accidentally adds a new @InstallIn module or entry point in non-test sources and the test passes (because the test generates a new component that includes this metadata), but fails in production.

*/
public enum GradleProjectType {
/** Project type is not set, e.g. Hilt Gradle Plugin not applied. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.Component
import com.android.build.api.variant.DynamicFeatureAndroidComponentsExtension
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.api.variant.ScopedArtifacts
import com.android.build.api.variant.TestAndroidComponentsExtension
Expand Down Expand Up @@ -397,6 +398,9 @@ class HiltGradlePlugin @Inject constructor(private val providers: ProviderFactor
when (androidExtension) {
is ApplicationAndroidComponentsExtension -> GradleProjectType.APP
is LibraryAndroidComponentsExtension -> GradleProjectType.LIBRARY
// Dynamic feature modules are treated as libraries since they cannot have a
// @HiltAndroidApp and cannot contribute to the main app's component tree.
is DynamicFeatureAndroidComponentsExtension -> GradleProjectType.LIBRARY
is TestAndroidComponentsExtension -> GradleProjectType.TEST
else -> error("Hilt plugin does not know how to configure '$androidExtension'")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dagger.hilt.android.plugin.util
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.Component
import com.android.build.api.variant.DynamicFeatureAndroidComponentsExtension
import com.android.build.api.variant.HasAndroidTest
import com.android.build.api.variant.HasUnitTest
import com.android.build.api.variant.LibraryAndroidComponentsExtension
Expand All @@ -39,6 +40,9 @@ internal fun AndroidComponentsExtension<*, *, *>.onRootVariants(
// For a library project, only the androidTest and unitTest variant are configured since
// Hilt components are not generated in a library.
is LibraryAndroidComponentsExtension -> onTestVariants(block)
// Dynamic feature modules behave like libraries for Hilt purposes. They cannot contribute
// bindings to the main app graph, but they can create test-specific graphs.
is DynamicFeatureAndroidComponentsExtension -> onTestVariants(block)
is TestAndroidComponentsExtension -> onVariants { block(it, null) }
else -> error("Hilt plugin does not know how to configure '$this'")
}
Expand Down