question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hilt not working with AbstractComposeView for Jepack Compose

See original GitHub issue

I am using DaggerHilt for dependency injection. In my AbstractComposeView, I need to access the ViewModel in a Composable function. To do that I have to annotate my AbstractComposeView with AndroidEntryPoint, since my Composable is part of the AbstractComposeView (which is being used in a Service with AndroidEntryPoint). but I am getting this error.

error: [Hilt] The base class, 'androidx.compose.ui.platform.AbstractComposeView', of the @AndroidEntryPoint, 'com.qwillio.vendi.keyboard.presentation.KeyboardView', contains a constructor with default parameters. This is currently not supported by the Gradle plugin. Either specify the base class as described at https://dagger.dev/hilt/gradle-setup#why-use-the-plugin or remove the default value declaration. [Hilt] Processing did not complete. See error above for details. [Hilt]

This is my AbstractComposeView,

@AndroidEntryPoint
class KeyboardView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AbstractComposeView(context, attrs, defStyleAttr) {

    private var keyboardActionListener: OnKeyboardActionListener? = null

    fun setOnKeyboardActionListener(keyboardActionListener: OnKeyboardActionListener) {
        this.keyboardActionListener = keyboardActionListener
    }

    @Composable
    override fun Content() {
        Vendiboard {
            keyboardActionListener?.onKeyboardAction(it)
        }
    }
}

build.gradle

    implementation "com.google.dagger:hilt-android:2.40.5"
    kapt "com.google.dagger:hilt-android-compiler:2.40.5"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
Arxingcommented, Aug 26, 2022

The following code can solve your problem.

class BaseAbstractComposeView : AbstractComposeView {
    constructor(context: Context) : super(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
}

@AndroidEntryPoint
class KeyboardView : BaseAbstractComposeView {

    constructor(context: Context) : super(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var keyboardActionListener: OnKeyboardActionListener? = null

    fun setOnKeyboardActionListener(keyboardActionListener: OnKeyboardActionListener) {
        this.keyboardActionListener = keyboardActionListener
    }

    @Composable
    override fun Content() {
        Vendiboard {
            keyboardActionListener?.onKeyboardAction(it)
        }
    }
}
0reactions
frhnfrqcommented, Jun 15, 2022

Any chance of making Dagger Hilt views work with Services by default?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AndroidEntryPoint not working with AbstractComposeView
I am using DaggerHilt for dependency injection. In my AbstractComposeView, I need to access the ViewModel in a Composable function.
Read more >
Use Hilt with other Jetpack libraries - Android Developers
Hilt currently supports the following Jetpack components: ViewModel; Navigation; Compose; WorkManager. You must add the Hilt dependencies to ...
Read more >
Jetpack Compose: Adding a Hilt ViewModel to the Navigation ...
A composable, like a fragment or activity, can have a viewModel linked to it, this is especially important for having the viewModel handle...
Read more >
Interoperability with Jetpack Compose - RayWenderlich.com
As you complete this tutorial, you'll learn about: Jetpack Compose Interoperability APIs; Composable Lifecycles; Composition Strategy ...
Read more >
Source Code for ComposeView.android.kt - AndroidX Tech
showLayoutBounds = value } } /** * The Jetpack Compose UI content for this view. ... "ViewTreeSavedStateRegistryOwner not set for this ComposeView.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found