1

Usage/ injection:

    @Inject
    @field:Named("MyStringSaverFunction")
    lateinit var stringSaverFunction: (String) -> Unit

My Module

@Module(includes = [AbcModule.UiBinding::class])
object AbcModule {
    @Module
    interface UiBinding {
        @PerFragment
        @ContributesAndroidInjector
        fun provideFragment(): AbcFragment
    }
    @Provides
    @JvmStatic
    @Singleton
    @Named("AbcSharedPrefs")
    fun provideAbcSharedPrefs(context: Context) : SharedPreferences {
        return context.getSharedPreferences("AbcSharedPrefs", Context.MODE_PRIVATE)
    }

    @Provides
    @JvmStatic
    @Singleton
    @Named("MyStringSaverFunction")
    fun provideDfIntentSaver(@Named("AbcSharedPrefs") sharedPreferences: SharedPreferences): (String) -> Unit {
        return { theString: String ->
            sharedPreferences.edit().putString("TheKey", theString).apply()
        }
    }
}

Error details:

error: [Dagger/MissingBinding] @javax.inject.Named("MyStringSaverFunction") kotlin.jvm.functions.Function1<? super java.lang.String,kotlin.Unit> cannot be provided without an @Provides-annotated method.
public abstract interface MyAppComponent extends dagger.android.AndroidInjector<com.myapp.android.MyApplication> {
                ^
      @javax.inject.Named("MyStringSaverFunction") kotlin.jvm.functions.Function1<? super java.lang.String,kotlin.Unit> is injected at
          com.myapp.android.AbcFragment.stringSaverFunction
      com.myapp.android.AbcFragment is injected at

> Task :myapp:kaptDebugKotlin FAILED
          dagger.android.AndroidInjector.inject(T) [com.myapp.android.di.MyAppComponent → com.myapp.android.di.AbcModule_Bindable_ProvideFragment.AbcFragmentSubcomponent]
FAILURE: Build failed with an exception.
ericn
  • 12,476
  • 16
  • 84
  • 127

1 Answers1

3

Use @JvmSuppressWildcards at the injection site to match the signature -

@Inject
@field:Named("MyStringSaverFunction")
@JvmSuppressWildcards
lateinit var stringSaverFunction: (String) -> Unit
Vishal Arora
  • 2,524
  • 2
  • 11
  • 14
  • Thanks but now I'm getting `error: cannot find symbol MyFragment_MembersInjector.injectSharedPreferences(instance, DaggerFormsPreviewAppComponent.this.provideMySharedPrefsProvider.get());` – ericn Nov 04 '19 at 10:42
  • Can you please share the complete stacktrace ? – Vishal Arora Nov 04 '19 at 10:44
  • Hi @VishalArora, updated my question with more code and the complete stacktrace of the new error – ericn Nov 05 '19 at 03:07
  • There was some crazy intermittent issue with Dagger and/ or Gradle, all is working now – ericn Nov 05 '19 at 04:23