Search code examples
androiddagger-2androidinjector

Unused @Binds methods with Dagger


I have the following definition of the Splash screen Activity:

@Subcomponent(modules = arrayOf(
        SplashActivitySubcomponent.ComponentModule::class)
)
interface SplashActivitySubcomponent : AndroidInjector<SplashActivity> {
    @Subcomponent.Builder
    abstract class Builder : AndroidInjector.Builder<SplashActivity>()

    @Module
    abstract class ComponentModule {
        @Binds
        @IntoMap
        @ActivityKey(SplashActivity::class)
        abstract fun bindSplashActivityInjectorFactory(builder: SplashActivitySubcomponent.Builder): AndroidInjector.Factory<out Activity>
    }
}

I do not have fragments in the Activity. What happens is that bindSplashActivityInjectorFactory is unused. The problem is that I can't get rid of it otherwise the app crashes at runtime.

To use

@Suppress("unused")

isn't enough because I still get a warning from the kotlin-lint. Why do I need to define this method when it is not used? What can I do to avoid the warning?

This is the error I get:

UnnecessaryAbstractClass - [ComponentModule] at com/xxxx/splash/di/SplashActivitySubcomponent.kt:20:5

OptionalAbstractKeyword - [bindSplashActivityInjectorFactory] at com/xxxx/splash/di/SplashActivitySubcomponent.kt:22:9


Solution

  • For now I fixed the problem doing this:

    @Subcomponent(modules = arrayOf(SplashActivitySubcomponent.ComponentModule::class))
    interface SplashActivitySubcomponent : AndroidInjector<SplashActivity> {
        @Subcomponent.Builder
        abstract class Builder : AndroidInjector.Builder<SplashActivity>()
    
        @Module
        @Suppress("UnnecessaryAbstractClass", "OptionalAbstractKeyword")
        abstract class ComponentModule {
            @Suppress("unused")
            @Binds
            @IntoMap
            @ActivityKey(SplashActivity::class)
            abstract fun bindSplashActivityInjectorFactory(builder: SplashActivitySubcomponent.Builder): AndroidInjector.Factory<out Activity>
        }
    }
    

    So I just added:

    @Suppress("UnnecessaryAbstractClass", "OptionalAbstractKeyword")