FragmentContainerViewIdBugDemo - minimal repro project demonstrating a bug in FragmentContainerView's id check logic in the context of a dynamic feature module

Overview

FragmentContainerViewIdBugDemo

minimal reproduce project demonstrating an apparent bug in FragmentContainerView's id check logic in the context of a dynamic feature module

Problem

FragmentContainerView inflation from XML can fail citing a missing android:id, but the XML source has an ID specified.

Characteristic stack trace

2022-01-04 18:15:14.139 29700-29700/com.example.fcvidbugdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fcvidbugdemo, PID: 29700
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fcvidbugdemo/com.example.dynamicfeature.DynamicFeatureActivity}: android.view.InflateException: Binary XML file line #17 in com.example.fcvidbugdemo.dynamicfeature:layout/activity_dynamic_feature: Binary XML file line #17 in com.example.fcvidbugdemo.dynamicfeature:layout/activity_dynamic_feature: Error inflating class androidx.fragment.app.FragmentContainerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792)
        ...
     Caused by: android.view.InflateException: Binary XML file line #17 in com.example.fcvidbugdemo.dynamicfeature:layout/activity_dynamic_feature: Binary XML file line #17 in com.example.fcvidbugdemo.dynamicfeature:layout/activity_dynamic_feature: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: android.view.InflateException: Binary XML file line #17 in com.example.fcvidbugdemo.dynamicfeature:layout/activity_dynamic_feature: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: java.lang.IllegalStateException: FragmentContainerView must have an android:id to add Fragment com.example.dynamicfeature.DynamicFeatureFragment
        at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:171)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
        ...

XML Source

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_host_view"
        android:name="com.example.dynamicfeature.DynamicFeatureFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Cause

https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/fragment/fragment/src/main/java/androidx/fragment/app/FragmentContainerView.kt#146 shows FragmentContainerView considering its view ID to be invalid if it's less than or equal to 0, but legal Android resource IDs can be interpreted by Dalvik/ART as negative values. This is because View.getId() returns a signed 32 bit integer, so its max is 231-1 and min is -231, whereas Android resource IDs use the full unsigned 32 bit integer range from 1 to 232-1 [see https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java#28042]. When a resource ID value exceeds what the signed integer max, it overflows to negative values. These can wrap all the way around to -1 for a resource with hex ID 0xFFFFFFFF, so the only truly invalid Android resource ID (when represented by a signed 32 bit integer) is 0.

The role dynamic features play in this issue is unclear, but it seems that being part of a dynamic feature module imposes a huge bump up of resource IDs generated by AAPT; this makes the problem more likely to come up, but it could theoretically happen in a project without dynamic feature modules as well.

Proposed Fix

Change the FragmentContainerView constructor such that any view ID not equal to 0 is considered valid.

You might also like...
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles

This repository serves as template and demo for building android applications for scale. It is suited for large teams where individuals can work independently on feature wise and layer wise reducing the dependency on each other.

Clean Android multi-module offline-first scalable app in 2022. Including Jetpack Compose, MVI, Kotlin coroutines/Flow, Kotlin serialization, Hilt and Room.

Android Kotlin starter project - 2022 edition Android starter project, described precisely in this article. Purpose To show good practices using Kotli

Titanium Android module: Add a view to a BottomNavigation
Titanium Android module: Add a view to a BottomNavigation

ti.bottomNavView Titanium Android module that allows you to add a custom view to a BottomNavigation: Install moduleti.bottomNavView/module Method

Purpose of this is to learn to create audit trail module.
Purpose of this is to learn to create audit trail module.

Redis Trail An effective audit trail solution can be crucial to an organization's security and data integrity as it can help find the who, what, and w

YAML-based source-based kotlin module descriptors

kproject - Liberate your Kotlin projects YAML-based source-based kotlin module descriptors that runs on top of gradle. Define your kotlin multiplatfor

sample project that shows you how you can use Ktor to creat a server for real Project.

Ktor-Sample This is a sample project that shows you how you can use Ktor to creat a server for real Project. What is done Save data to database (Get a

Basic-Android-Project - A Basic Android Project with proper structure and all necessary dependencies

Basic-Android-Project A Basic Android Project with proper structure and all nece

A project that helps us generate the test project to test the Gradle plugin.

Ktlint Gradle Provides the function to generate a Gradle project for us to test your Gradle plugin Latest plugin version: [1.0.0] Table of content How

Owner
Programmer, storyteller, adventurer. My favorite systems are embedded, preferably within robots!
null
Collection of JVM library logic that the Sirloin software development team is currently using

Collection of JVM library logic that the Sirloin software development team is currently using

Sirloin Dev 4 May 10, 2022
This project is basically PowerNukkit but just in Kotlin (check out the original PowerNukkit source here: https://github.com/PowerNukkit/PowerNukkit)

Introduction Nukkit is nuclear-powered server software for Minecraft: Pocket Edition. It has a few key advantages over other server software: Written

Chrones 5 Jul 7, 2021
This is a work-in-progress (🔧️) ultraviolet index viewer app for demonstrating Instant Apps + Kotlin + Dagger + MVP

UV Index A simple ultraviolet index viewer app for demonstrating: Instant Apps + Kotlin + Dagger + MVP Built With Weatherbit as weather API Android In

Mustafa Berkay Mutlu 65 Oct 31, 2022
Lambë Language 7 Dec 21, 2022
Attend HoYoLAB Check-in events automatically

Croissant Attend HoYoLAB Check-in events automatically https://play.google.com/store/apps/details?id=com.joeloewi.croissant Stacks Room Database Hilt

joeloewi 2 Nov 23, 2022
A simple tool used to check the users you follow that do not follow you back.

instafbchecker - Instagram no life guide Current Release: v1.0.1 (30/08/2022) A command line tool used to check which users dont follow you back on In

Nathan 2 Aug 30, 2022
Kotlin compiler plugin that allows class delegation to be dynamic like property delegations

kotlin-dynamic-delegation Kotlin compiler plugin that allows class delegation to be dynamic like property delegations. The plugin is working in progre

Him188 14 Sep 8, 2022
For Kotlin with SpringBoot project that have multi-module-structure template

Goals kotlin + spring-boot + gradle + multi-module building Module-Structure ---root |--- src.main.kotlin.KotlinSpringbootMultiModuleTemplateAppl

pguma 1 Jul 24, 2022
DSU-Sideloader - A simple app made to help users easily install GSIs via DSU's Android feature

DSU Sideloader A simple app made to help users easily install GSIs via DSU's And

null 430 Jan 4, 2023