Proguard causing runtime exception with Android Navigation Component

前端 未结 4 883
臣服心动
臣服心动 2020-12-09 02:18

I\'m experiencing this crash when using proguard after integrating the NavigationComponent (android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01) into

相关标签:
4条回答
  • 2020-12-09 02:23

    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

    0 讨论(0)
  • 2020-12-09 02:27

    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.*
    
    0 讨论(0)
  • 2020-12-09 02:29

    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
    
    0 讨论(0)
  • 2020-12-09 02:39

    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

    0 讨论(0)
提交回复
热议问题