[Dagger Hilt] dagger module with same full name overriden in component
See original GitHub issueCurrent Behavior
In an application with two dagger module with the same package and the same name (for example in two different gradle-modules).
If those modules are installed in any component with @InstallIn
one of them will be overriden by the other silently.
Expected Behavior
Dagger Hilt should fail and warn the user about the collision. The user should not have two modules with the same name but Dagger Hilt should not silently override the module.
Context (Environment)
Dagger Hilt 29.1-alpha
module 1
package com.example.twin
@Module
@InstallIn(SingletonComponent::class)
interface FooModule {
@Binds
fun bindRandomSuff(
impl: RandomStuffImpl
): RandomStuff
}
module 2
package com.example.twin
@Module
@InstallIn(SingletonComponent::class)
interface FooModule {
@Binds
fun bindAnotherRandomSuff(
impl: AnotherRandomStuffImpl
): AnotherRandomStuff
}
generated code
// ...
import com.example.twin.FooModule
// ...
@Component(
modules = {
ActivityRetainedCBuilderModule.class,
ServiceCBuilderModule.class,
ApplicationContextModule.class,
FooModule.class,
}
)
@Singleton
public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint,
// ....
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Dagger 2 issue overriding single provides annotated method ...
Dagger 2 doesn't support overrides. Modules that override for simple testing fakes can create a subclass of the module to emulate that behavior....
Read more >Using Dagger in multi-module apps - Android Developers
Dagger has a mechanism called component dependencies that you can use to solve this issue. Instead of the child component being a subcomponent ......
Read more >Dependency Injection with Dagger 2 - CodePath Cliffnotes
Dagger 2 walks through the dependency graph and generates code that is both easy to understand and trace, while also saving you from...
Read more >Advanced Dagger Semantics
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team...
Read more >Dagger Hilt Tutorial - Step by Step Guide - MindOrks
To understand it better in a basic way, think module as a provider of dependency and consider an activity or any other class...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No problem!
(Btw, I should have also explained that the reason we need to check the set of seen modules in
AggregatedDepsProcessor
is not because we expect duplicate classes but because we may try to process the same class multiple times. This can happen because the processor supports multiple annotation types (e.g.@Module
,@InstallIn
, etc…) so it’s possible that we try to process the same class multiple times if it’s annotated with multiple supported annotations.That is the set of seen modules in the current build unit.
If both
FooModule
classes are in the same build unit you will get anerror: duplicate class: ...FooModule
when compiling (Note: this error is from javac not Dagger) soAggregatedDepsProcessor
doesn’t even get a chance to run.If both
FooModule
classes are in different build units (which I think is your case since you said they were in different Gradle modules:module1
andmodule2
) then each Gradle module will only see its ownFooModule
class.Finally, if anything depends on
module1
andmodule2
and tries to resolveFooModule
, it will get whicheverFooModule
showed up first in the class path and doesn’t even know there was more than one.