How to use Hilt with DatabindingComponent?
See original GitHub issueHello,
I have switched over to Hilt for DI. Everything works like a charm. However, I have only an issue with Databinding Adapters
to generate them with Hilt. I was able to use DatabindingComponent
and converting my objects into classes. Can you help me have the insight to work on the same with Hilt?
Dagger way
BindingComponent
@BindingScope
@Subcomponent(modules = [BindingModule::class])
interface BindingComponent : DataBindingComponent {
@Subcomponent.Factory
interface Factory {
fun create(): BindingComponent
}
override fun getImageBindingAdapter(): ImageBindingAdapter
override fun getTextBindingAdapter(): TextBindingAdapter
}
BindingModule
@Module
internal object BindingModule {
private const val DEFAULT_MEMORY_MULTIPLIER = 0.5
@Provides
@BindingScope
internal fun provideImageLoader(context: Context) = ImageLoader.Builder(context).apply {
bitmapPoolPercentage(DEFAULT_MEMORY_MULTIPLIER)
availableMemoryPercentage(DEFAULT_MEMORY_MULTIPLIER)
crossfade(true)
}.build()
@Provides
@InternalApi
@BindingScope
internal fun provideImageBindingAdapter(
imageLoader: ImageLoader
) = ImageBindingAdapter(imageLoader)
@Provides
@InternalApi
@BindingScope
internal fun provideTextBindingAdapter() = TextBindingAdapter()
}
@Scope
@MustBeDocumented
internal annotation class BindingScope
@Qualifier
@Retention(AnnotationRetention.BINARY)
@MustBeDocumented
private annotation class InternalApi
ImageBindingAdapter
@BindingScope
class ImageBindingAdapter @Inject constructor(private val imageLoader: ImageLoader) {
@BindingAdapter("android:src")
fun ImageView.bindImage(url: String?) {
load(url, imageLoader) {
fallback(R.drawable.ic_error)
transformations(CircleCropTransformation())
size(IMAGE_WIDTH_SIZE_DEFAULT, IMAGE_HEIGHT_SIZE_DEFAULT)
build()
}
}
private companion object {
private const val IMAGE_WIDTH_SIZE_DEFAULT = 200
private const val IMAGE_HEIGHT_SIZE_DEFAULT = 200
}
}
Hilt Way
@Module
@InstallIn(DataBindingComponent::class)
class BindingModule {}
Error trace
/path-to-project/app/build/tmp/kapt3/stubs/debug/io/github/nuhkoca/project-name/binding/BindingModule.java:5: error: cannot find symbol
@dagger.hilt.InstallIn(value = {DataBindingComponent.class})
^
symbol: class DataBindingComponenterror: [Hilt]
@InstallIn, 'value' class is invalid or missing: @dagger.hilt.InstallIn({<error>})
[Hilt] Processing did not complete. See error above for details./path-to-project/app/build/tmp/kapt3/stubs/debug/io/github/nuhkoca/project-name/binding/BindingModule.java:8: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class BindingModule {
class BindingModule {}
Issue Analytics
- State:
- Created 3 years ago
- Comments:12
Top Results From Across the Web
Data Binding Adapters And Dependency Injection- A Short ...
The goal of this article is to illustrate how can we gel Data Binding with Dependency Injection using Hilt-Android.
Read more >Dependency injection with Hilt | Android Developers
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
Read more >DataBindingComponent not being generated when using ...
The DataBindingComponent class does not seem to be generated. Having a look at the documentation for DataBindingComponent we see: If using ...
Read more >[Solved]-Cannot find symbol return ...
In my case this error caused by androidx.hilt:hilt-lifecycle-viewmodel ... Cannot find symbol DataBindingComponent on Android Studio 3.2 Canary 16 Kotlin ...
Read more >Class androidx.databinding.DataBindingComponent
androidx.hilt.work ... DataBindingComponent android.databinding. ... @see DataBindingUtil#setDefaultComponent(DataBindingComponent) * @see ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dmapr ohh I am seeing a very beneficial corresponding out there! Thank you for clarifying this point with the authors 😃 I will definitely update my repository. It is great to see how to properly work with
Hilt
forDataBinding
! Thanks again.@nuhkoca — @bcorso pointed out the correct way of setting up the custom binding component in the https://github.com/google/dagger/issues/2603, you may want to check it out.