google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

Dagger 2 cannot provide generated Android DataBinding (compilation error) #339

Closed ZakTaccardi closed 8 years ago

ZakTaccardi commented 8 years ago

Dagger2 generates code. The DataBinding library generates code. Bad things (build failure) happen when dagger2 needs to @Provides the generated binding classes

See the MainActivityModule.java file.


    @Provides
    @Singleton
    @SuppressWarnings("deprecation")
    /**
     * Providing this scene here causes a compilation error because of the {@link MoreTextBinding} parameter
     */
    public Scene layoutScene(LinearLayout container, MoreTextBinding binding) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return new Scene(container, binding.getRoot());
        } else {
            return new Scene(container, (ViewGroup) binding.getRoot());
        }
    }

Full code demonstrating this compilation error can be found here: https://code.google.com/p/android/issues/detail?id=203853

Could be a duplicate of https://github.com/google/dagger/issues/252 (but if so this issue needs to be re-opened)

gk5885 commented 8 years ago

What is the error?

ronshapiro commented 8 years ago

I don't think it's a dupe of #252 since that discusses types that are generated as part of javac annotation processing, but my sense is that the Android Data Binding API generates code in a different step. I think @lisawray and/or @ekchang have also had this issue with databinding

ekchang commented 8 years ago

There is no default constructor for the generated binding classes so you need to provide it for the graph to figure out how to get it.

@Provides @PerActivity MoreTextBinding provideMoreTextBinding() {
    return DataBindingUtil.setContentView(activity, R.layout.more_text); // or DataBindingUtil.inflate() etc
}
BuhTigRunner commented 8 years ago

I also have a similar problem public class MainActivity extends BaseActivity<ScreenTestBinding> - error C:\Android Projects\TestArchitecture\app\src\main\java\com\example\dmn687\testarchitecture\MainActivity.java Error:(12, 55) error: package com.example.dmn687.testarchitecture.databinding does not exist Error:(16, 48) error: cannot find symbol class ScreenTestBinding C:\Android Projects\TestArchitecture\app\src\main\java\com\example\dmn687\testarchitecture\BaseActivity.java Error:(9, 55) error: package com.example.dmn687.testarchitecture.databinding does not exist Error:(23, 15) error: cannot find symbol class ScreenTestBinding where T is a type-variable: T extends Object declared in class BaseActivity Where ScreenTestBinding - generated class extends android.databinding.ViewDataBinding If use public class MainActivity extends BaseActivity<ViewDataBinding> - all ok