jakubkinst / Android-ViewModelBinding

A lightweight library aiming to speed up Android app development by leveraging the new Android Data Binding together with the Model-View-ViewModel design pattern.
294 stars 32 forks source link

Compatibility with Dagger2 #11

Open mykola-dev opened 8 years ago

mykola-dev commented 8 years ago

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

jakubkinst commented 8 years ago

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

mykola-dev commented 8 years ago

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