Skip to content

Cannot snapshot view that requires Hilt-aware Activity/Fragment #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rbro112 opened this issue Feb 4, 2025 · 0 comments
Open

Cannot snapshot view that requires Hilt-aware Activity/Fragment #441

rbro112 opened this issue Feb 4, 2025 · 0 comments

Comments

@rbro112
Copy link
Contributor

rbro112 commented Feb 4, 2025

A client recently encountered an issue running a compose preview for a view that requires a hilt-aware Activity/Fragment to bind to. Minimal repro:

@AndroidEntryPoint
public class BasicLayoutView
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :  FrameLayout(context, attrs, defStyle) {

  public val binding: ViewBasicLayoutBinding = ViewBasicLayoutBinding.inflate(LayoutInflater.from(context), this)
}

@Preview
@Composable
private fun ReferencedXMLPreview() {
  AndroidViewBinding(factory = ViewBasicLayoutUsedBinding::inflate) {
    basicLayoutView.binding.textView.text = "Hello, World!"
  }
}

Minimal XML view with the binding:

<?xml version="1.0" encoding="utf-8" ?>
<merge
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
>
  <TextView
    android:id="@+id/textView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
  />
</merge>

This causes a crash when attempting to snapshot the ReferencedXMLPreview:

android.view.InflateException: Binary XML file line #8 in <>:layout/view_basic_layout_used: Binary XML file line #8 in <>:layout/view_basic_layout_used: Error inflating class <>.BasicLayoutView
Caused by: android.view.InflateException: Binary XML file line #8 in <>:layout/view_basic_layout_used: Error inflating class <>.BasicLayoutView
Caused by: java.lang.reflect.InvocationTargetException
	<...>
Caused by: java.lang.IllegalStateException: class <>.BasicLayoutView, Hilt view must be attached to an @AndroidEntryPoint Fragment or Activity.
	at dagger.hilt.android.internal.managers.ViewComponentManager.getParentComponentManager(ViewComponentManager.java:138)
	at dagger.hilt.android.internal.managers.ViewComponentManager.createComponent(ViewComponentManager.java:86)
	at dagger.hilt.android.internal.managers.ViewComponentManager.generatedComponent(ViewComponentManager.java:77)
	<...>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant