Proguard causing runtime exception with Android Navigation Component

I know that Proguard and R8 should be keeping all the children of library classes but in this case, the fragment class seems to be missing. This keep rule solved my issue but technically we should not need this rule at all!

-keep class * extends android.support.v4.app.Fragment{}

If you are using AndroidX, then use this rule: -keep class * extends androidx.fragment.app.Fragment{}

If you use argType in your navigation XML, you also need a rule for the referenced classes, for example: -keep class com.example.model.MyModel. Or even better, exclude parcelable and serializable classes from being renamed, as recommended by the official documentation. -keepnames class * extends android.os.Parcelable -keepnames class * extends java.io.Serializable


My issue was that I was using the fragment name on the layout. After R8, the names are obfuscated causing the issue.

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_fragment"                
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent""
    app:defaultNavHost="true"
    app:navGraph="@navigation/navigation_home" />

In my case, the solution was to keep only the name in the Proguard file, as such:

#-------------------------------------------------
# JetPack Navigation
# This fixes: Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists
#-------------------------------------------------
-keepnames class androidx.navigation.fragment.NavHostFragment