0

I've been trying to set up a dependency injection but there is something that I can't deal with. I almost understand Dagger expect some weird issues like this one.

Here is the log error:

 e: /Users/costular/AndroidProjects/TransmissionRemote/app/build/tmp/kapt3/stubs/debug/com/costular/transmissionremote/di/components/ServerComponent.java:8: error: com.costular.transmissionremote.torrents.TorrentRepository cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
e: 

e:     public abstract void inject(@org.jetbrains.annotations.NotNull()
e:                          ^
e:       com.costular.transmissionremote.torrents.TorrentRepository is injected at
e:           com.costular.transmissionremote.torrents.torrent_list.TorrentsActivity.torrentRepository
e:       com.costular.transmissionremote.torrents.torrent_list.TorrentsActivity is injected at
e:           com.costular.transmissionremote.di.components.ServerComponent.inject(activity)
e: /Users/costular/AndroidProjects/TransmissionRemote/app/build/tmp/kapt3/stubs/debug/com/costular/transmissionremote/di/components/TorrentComponent.java:4: error: This @Singleton component cannot depend on scoped components:
e: 

e: @dagger.Component(modules = {com.costular.transmissionremote.di.modules.NetworkModule.class, com.costular.transmissionremote.di.modules.StorageModule.class, com.costular.transmissionremote.di.modules.TorrentModule.class}, dependencies = {com.costular.transmissionremote.di.components.ApplicationComponent.class})
e: ^
e:       @Singleton com.costular.transmissionremote.di.components.ApplicationComponent

Here is the code: ApplicationComponent.kt

    @Singleton
@Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent {

    fun inject(myApp: MyApp)

    fun context(): Context

    fun app(): MyApp
}

ApplicationModule.kt

    @Module
class ApplicationModule(private val myApp: MyApp) {

    @Provides
    @Singleton
    fun provideApplication(): MyApp = myApp

    @Provides
    @Singleton
    fun providesContext(): Context = myApp.applicationContext

}

TorrentComponent.kt

 @Singleton
@Component(
        modules = arrayOf(NetworkModule::class, StorageModule::class, TorrentModule::class),
        dependencies = arrayOf(ApplicationComponent::class)
)
interface TorrentComponent {

    fun inject(torrentsActivity: TorrentsActivity)

}

TorrentModule.kt

    @Module
class TorrentModule {

    @Singleton


     @Provides
        fun provideTorrentRepository(client: TransmissionClient): TorrentRepository = TorrentRepository(client)

    }

NetworkModuke.kt

@Module
class NetworkModule(val host: String){

    @Provides
    @Singleton
    fun providesRPCClient(): TransmissionClient = TransmissionClient.getInstance(host)


}

TorrentsActivity.kt

 class TorrentsActivity : AppCompatActivity() {

    @Inject
    lateinit var torrentRepository: TorrentRepository

    lateinit var presenter: TorrentsPresenter

    private val fab by lazy { findViewById(R.id.fab) as FloatingActionButton }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar = findViewById(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar)

        // Fab listener
        fab.setOnClickListener { presenter.addTorrent() }

        // Fragment initialization
        // Injection
        DaggerTorrentComponent.builder()
                .applicationComponent((application as MyApp).applicationComponent)
                .build()
}

Thank you so much!

Costular
  • 51
  • 2
  • 8
  • 1
    Your error is in `ServerComponent` where the error states that you also have an `inject(TorrentsActivity)`. I'd guess that's an artifact you didn't delete while refactoring? – David Medenjak Aug 19 '17 at 06:45
  • I checked your link but I think it's different. I deleted the component and every call and after that app compiled perfectly. However, after clean I did add again and it's still not working. I've a @provide method. I don't understand the error at all. – Costular Aug 19 '17 at 09:46
  • Solved! It was a problem with mixed scopes. Thank you anyway David! – Costular Aug 19 '17 at 10:10

0 Answers0