1

I want to inject data binding to activity using Dagger 2. I have a problem with its. I don't understand what is wrong.

ERROR

cannot access DataBindingComponent class file for androidx.databinding.DataBindingComponent not found

And my code:

MainActivity.kt

class MainActivity : AppCompatActivity() {
    @Inject lateinit var bindingUtil: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        val stepperContainer = (applicationContext as StepperApplication).stepperContainer
        stepperContainer.inject(this)
        super.onCreate(savedInstanceState)
    }
}

StepperApplication.kt

class StepperApplication : Application() {
    val stepperContainer by lazy {
        DaggerStepperComponent.builder().context(ContextModule(applicationContext)).build()
    }

    override fun onCreate() {
        super.onCreate()
    }
}

StepperComponent.kt

@Singleton
@Component (modules = [ContextModule::class])
interface StepperComponent {
    fun inject(activity: MainActivity)
}

ContextModule.kt

@Module
class ContextModule (var context: Context) {

    @Singleton
    @Provides
    fun context() : Context {
        return context
    }
}

ActivityMainModule.kt

@Module (subcomponents = [ActivityMainComponent::class])
class ActivityMainModule (var activity: MainActivity) {
    @Provides
    fun activityMainBinding(activity: MainActivity) : ActivityMainBinding {
        return DataBindingUtil.setContentView(activity, R.layout.activity_main)
    }
}

ActivityMainComponent.kt

@Subcomponent
interface ActivityMainComponent {

    @Subcomponent.Builder
    interface Builder {
        fun activityMain(activity: MainActivity) : Builder
        fun build() : ActivityMainComponent
    }
}

Gradle

implementation 'com.google.dagger:dagger:2.25.2'
kapt 'com.google.dagger:dagger-compiler:2.25.2'

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

What is the problem and how to solve it?

lapwingg
  • 112
  • 9
  • 1
    Have you enabled data binding feature? If yes, please post layout.xml code. – iCantC Jan 27 '20 at 08:58
  • @iCantC yes, I have enabled data binding feature, I posted layout.xml code – lapwingg Jan 27 '20 at 12:29
  • Hi, @lapwingg welcome to SO community. [This might help you](https://stackoverflow.com/questions/44549355/is-it-ok-to-inject-content-view-using-dagger2) – Muhammad Farhan Jan 27 '20 at 12:31
  • Thank you @MuhammadFarhan for the link, so you want to tell me that is the bad practice to inject UI to activity / fragment? Does DI use for other purposes? – lapwingg Jan 27 '20 at 12:54
  • @lapwingg From the link, I gave you prohibit to use dependency injection to any UI related injection. and It will be great if you use Dagger-2 Android support. This is a [great tutorial](https://www.youtube.com/watch?v=3qZh6Fyrz-k&list=PLgCYzUzKIBE8AOAspC3DHoBNZIBHbIOsC) series on Android Dagger-2 – Muhammad Farhan Jan 27 '20 at 13:50
  • It seems to me, the problem is solved. – lapwingg Jan 29 '20 at 17:28

0 Answers0