I\'m experiencing this crash when using proguard after integrating the NavigationComponent (android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01
) into
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
Did face the same issue. The answer above gave me the correct direction to look into. According to navigation docs
-keep class * extends android.support.v4.app.Fragment{} <-this is not needed
All you need to do is keep names for data classes which are passed via safe args e.g
-keepnames class com.your.package.models.*
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
This is fixed since Android Gradle Plugin 4.1.
No need to define Proguard rules for fragments defined in the android:name
attribute.
See https://issuetracker.google.com/issues/142601969