title: Single instance for different scopes with dagger 2
body: <h1>Problem</h1>
  <p>I am building an app with a dynamic feature.</p>
  <p>To provide all the dependencies to the main module and the feature module I am using dagger 2. The feature component is depending on the main component and because of that, the feature component is having a different scope than the main component scope (<code>@Singleton</code> in that case)</p>
  <p>One of the interface injected in the main module are implemented on the feature module and provided by reflection in the main module. The implementation is also used in the feature module.</p>
  <p>The problem that I have is that the instance provided in the main module is different from the one in the feature module (because of the scopes) but I would like to have just one instance provided with dagger.</p>
  <h1>Code</h1>
  <p>Here some code and you can find the whole example project in <a href="https://github.com/brunoescalona/dagger-test" rel="nofollow noreferrer">github</a></p>
  <p>Dagger configuration for the main module:</p>
  <p><code>TestModule.kt</code></p>
  <pre><code>@Module
  class TestModule {
  
      @Provides
      @Singleton
      fun provideTestA() : TestA = TestAImplementation()
  
      private var testCProvider: TestC?= null
  
      @Provides
      @Singleton
      fun provideTestC(testComponent: TestComponent) : TestC {
          if(testCProvider != null) return testCProvider as TestC
  
          val provider = Class.forName(&quot;com.example.feature.services.TestCImplementation\$Provider&quot;).kotlin.objectInstance as TestC.Provider
          return provider
              .get(testComponent)
              .also { testCProvider = it }
      }
  }
  </code></pre>
  <p><code>TestComponent.kt</code></p>
  <pre><code>@Singleton
  @Component(modules = [TestModule::class])
  interface TestComponent {
      fun inject(activity: MainActivity)
  
      fun provideTestA() : TestA
  }
  </code></pre>
  <p>Dagger configuration for the feature module:</p>
  <p><code>TestDependencyModule.kt</code></p>
  <pre><code>@Module
  class TestDependencyModule {
  
      @Provides
      @TestScope
      fun provideTestB(): TestB = TestBImplementation()
  
      @Provides
      @TestScope
      fun provideTestC(testB: TestB): TestC = TestCImplementation(testB)
  }
  </code></pre>
  <p><code>TestDependencyComponent.kt</code></p>
  <pre><code>@TestScope
  @Component(
      modules = [TestDependencyModule::class],
      dependencies = [TestComponent::class]
  )
  interface TestDependencyComponent {
      fun inject(receiver: TestBroadcastReceiver)
  
      fun testC(): TestC
  }
  </code></pre>
  <h1>Expected result</h1>
  <p>The interfaces <code>TestC</code> and <code>TestA</code> are injected in the <code>MainActivity</code></p>
  <p>The interfaces <code>TestB</code> and <code>TestA</code> are injected in the <code>TestBroadcastReceiver</code></p>
  <p>As expected the instance of the <code>TestA</code> implementation is unique but for the implementation of the <code>TestB</code> is not that way. As <code>TestC</code> depends on <code>TestB</code> the one injected in <code>TestC</code> is different from the one injected in the <code>TestBroadcastReceiver</code> with the <code>@TestScope</code> annotation.</p>
  <p>So running the example project that you can find <a href="https://github.com/brunoescalona/dagger-test" rel="nofollow noreferrer">here</a> I get the following log output</p>
  <p>Instances injected in the <code>MainActivity</code></p>
  <pre><code>D/TestB: instance 40525431
  D/TestC: instance 119319268
  D/TestA: instance 60713805
  </code></pre>
  <p>Instances injected in the <code>TestBroadcastReceiver</code></p>
  <pre><code>D/TestB: instance 219966227
  D/TestA: instance 60713805
  </code></pre>
  <p>I would like to share the same instance of <code>TestB</code> in both modules.</p>
  <p>Any suggestion?
  Thanks in advance!</p>