Android-ViewModelBinding icon indicating copy to clipboard operation
Android-ViewModelBinding copied to clipboard

Compatibility with Dagger2

Open mykola-dev opened this issue 10 years ago • 2 comments

When i trying to inject some fields into viewModel with Dagger2, it fails, because apt compiler cannot find ActivityMainBinding (generated binding class). Same issue when injecting into Activity, because it extends class with generic type that has no exist yet at APT compile stage. PS: i use Kotlin 1.0, but this issue also should be reproduceable in plain Java

example, that failed to compile https://github.com/deviant-studio/Android-ViewModelBinding/commit/e9417c5bc7badf441236597a74426dde4bf6c3f9

mykola-dev avatar Mar 11 '16 19:03 mykola-dev

Does it work with plain data binding? I mean without ViewModelBinding library but with some data binding usage.

jakubkinst avatar Mar 13 '16 18:03 jakubkinst

Well, if generic type of injectable class is ViewDataBinding child (or other autogenerated class), dagger will fail. The only workaround i found is to wrap all injectable fields into some helper class and inject into instance of that class. But it looks ugly :)

example

class DaggerSingletones {
    init {
        App.getMainComponent().inject(this)
    }

    @Inject lateinit var service: RestService
    @Inject lateinit var db: DB
}

and yes, it works ok with simple plain databinding usage without generics https://github.com/zaki50/realm_template/blob/master/app/src/main/java/com/example/realm_template/MainActivity.java

mykola-dev avatar Mar 13 '16 19:03 mykola-dev