androidx.navigation 库更新到 2.2.2 版本发布构建问题

androidx.navigation library update to 2.2.2 version Release build issues

我对 androidx.navigation 上次更新有疑问

我注意到如果构建是 RELEASE,那么在新更新后 (2.2.2) 会崩溃 错误如

Caused by: android.view.InflateException: Binary XML file line #12 in 
com.somedomain.android:layout/activity_main: Binary XML file line #12 in 
com.somedomain.android:layout/activity_main: Error inflating class 
androidx.fragment.app.FragmentContainerView


Caused by: android.view.InflateException: Binary XML file line #12 in 
com.somedomain.android:layout/activity_main: Error inflating class 
androidx.fragment.app.FragmentContainerView

androidx.fragment.app.Fragment$e: Unable to instantiate fragment 
androidx.navigation.fragment.NavHostFragment: make sure class name exists

我正在使用:

ext.versions = [
   ...
   nav_version        : '2.2.2',
]

ext.libs = [
   ...
   nav_fragment       : "androidx.navigation:navigation-fragment-ktx:$versions.nav_version",
   nav_ui             : "androidx.navigation:navigation-ui-ktx:$versions.nav_version",
]

应用内gradle

 ...
    buildTypes {
        debug {
            manifestPlaceholders = [crashlyticsCollectionEnabled: "false"]
            ext.enableCrashlytics = false
            signingConfig signingConfigs.debug
            debuggable = true
        }
       
        release {
            manifestPlaceholders = [crashlyticsCollectionEnabled: "true"]
            ext.enableCrashlytics = true
            minifyEnabled true
            shrinkResources true
            debuggable = false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    ...
implementation libs.nav_fragment
implementation libs.nav_ui

导航主机在activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
   <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

</layout>

语言:科特林

有人遇到过这样的问题吗?

注意:2.2.1 版一切正常。 调试版本构建也可以

我认为问题与此更改有关。 根据官方 Android 导航组件 release notes

navigation-common-ktx ProGuard 规则现在正确地只保留使用的 NavArgs 类 而不是所有 NavArgs 实例。这是之前在 Navigation 2.3.0-alpha03 中发布的。 (b/150213558

该死的。我发现。

需要加上-keep class androidx.navigation.fragment.NavHostFragment

proguard-rules.pro 文件中。