我在使用androidx.navigation时遇到了问题
我注意到,在最新的更新(2.2.2)之后,如果构建是RELEASE会出现崩溃,错误信息如下:
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>
语言: Kotlin
有人遇到过这样的问题吗?
注意:使用版本2.2.1时一切正常,调试版本构建也没有问题。
我认为这个问题与这个更改有关。根据官方Android导航组件发布说明,导航公共KTX ProGuard规则现在只保留使用的NavArgs类,而不是所有的NavArgs实例。这是在Navigation 2.3.0-alpha03中发布的。(b/150213558)