question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Dagger Hilt] dagger module with same full name overriden in component

See original GitHub issue

Current 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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bcorsocommented, Nov 5, 2020

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.

1reaction
bcorsocommented, Nov 5, 2020

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 an error: duplicate class: ...FooModule when compiling (Note: this error is from javac not Dagger) so AggregatedDepsProcessor 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 and module2) then each Gradle module will only see its own FooModule class.

Finally, if anything depends on module1 and module2 and tries to resolve FooModule, it will get whichever FooModule showed up first in the class path and doesn’t even know there was more than one.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found