Skip to content

feat(hilt): Add limited dynamic feature module support for tests#5193

Open
Senthil455 wants to merge 1 commit into
google:masterfrom
Senthil455:dynamic-feature-support
Open

feat(hilt): Add limited dynamic feature module support for tests#5193
Senthil455 wants to merge 1 commit into
google:masterfrom
Senthil455:dynamic-feature-support

Conversation

@Senthil455

Copy link
Copy Markdown

When applying the Hilt plugin to a dynamic feature module (com.android.dynamic-feature), AGP provides a DynamicFeatureAndroidComponentsExtension which was not handled by the plugin's type checks, causing the error:

Failed to apply plugin 'com.google.dagger.hilt.android'.
Hilt plugin does not know how to configure 'extension 'androidComponents''

Changes

This change adds support for dynamic feature modules in a limited capacity by handling DynamicFeatureAndroidComponentsExtension in two key locations:

  1. Variants.kt (\onRootVariants) - Dynamic feature modules are treated like libraries: only test variants (unitTest and androidTest) are configured as Hilt roots where component trees are generated.

  2. HiltGradlePlugin.kt (\configureProcessorFlags) - Dynamic feature modules are assigned \GradleProjectType.LIBRARY\ since they cannot have a @HiltAndroidApp.

  3. GradleProjectType.java - Updated the Javadoc to reflect that dynamic features are now supported in a limited capacity.

Design Rationale

  • Dynamic feature modules cannot contribute bindings/modules to the main app's monolithic graph
  • They should be able to create test-specific graphs for writing unit tests and instrumentation tests
  • The library behavior is the right model: only test variants generate Hilt components
  • @HiltAndroidApp is still correctly rejected in dynamic features (enforced by AndroidEntryPointProcessingStep's GradleProjectType.APP check)
  • The bytecode transform (onAllVariants) still applies to all variants since @androidentrypoint classes may exist in tests

Fixes #5089

When applying the Hilt plugin to a dynamic feature module
(com.android.dynamic-feature), AGP provides a
DynamicFeatureAndroidComponentsExtension which was not handled by
the plugin's type checks, causing the error:
"Hilt plugin does not know how to configure 'extension androidComponents'"

This change adds support for dynamic feature modules in a limited capacity:
- Dynamic feature modules are treated like libraries for Hilt purposes
- Only test variants (unitTest and androidTest) are configured as Hilt roots,
  allowing test-specific graphs to be created
- Dynamic features cannot contribute bindings/modules to the main app graph
- @HiltAndroidApp is still correctly rejected in dynamic feature modules
  (enforced by AndroidEntryPointProcessingStep's GradleProjectType.APP check)

Fixes google#5089
* 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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add limited dynamic feature support (for tests)

2 participants