The StackOverflowError is triggered when the Android system's assist and autofill framework tries to read your app's UI structure. This can be initiated by actions like a user activating Google Assistant to see "what's on my screen" or an autofill service trying to scan for fillable fields.
On Android 8, a bug in the Compose AndroidComposeView causes an infinite recursion when calculating the position of your composables on the screen for this framework. This leads to the stack filling up and the app crashing, as seen in your stack trace with CalculateMatrixToWindowApi21 and populateVirtualStructure being called repeatedly.
Since the crash is caused by the Assist framework, the most effective way to mitigate it without updating your Compose version is to tell the system to ignore your ComposeView for autofill and assist purposes. This will prevent the recursive loop from ever starting.
You can do this by setting the importantForAutofill flag on your ComposeView.
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.material.Text
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
class YourFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS
}
setContent {
// Composable content
Text("Hello from Compose!")
}
}
}
}