问题描述
我有一个动态功能模块,它带有自己的嵌套导航图。我还有一个动态功能模块,它只是一个片段。如果您有一个动态功能模块及其自己的导航图,那么我了解您需要使用 include-dynamic 标签。当我构建应用程序并将其部署到Play商店进行内部测试时,动态模块会正确下载。但是,如果我在本地构建/运行-尝试导航到包含导航图的动态功能模块时,应用程序崩溃。我以为在本地运行时一切都捆绑在一起了?有什么我想念的吗?
我得到的错误是android.content.res.Resources$NotFoundException: com.xyz.app_name.two:navigation/two_navigation
main_navigation_graph
<!-- this works locally. module does not contain a nav graph -->
<fragment
android:id="@+id/OneFragment"
android:name="com.xyz.app_name.one.OneFragment"
android:label="{nickname}"
app:moduleName="one"
tools:layout="@layout/one_fragment">
<argument
android:name="nickname"
app:argType="string" />
</fragment>
<!-- this doesn't work locally -->
<include-dynamic
android:id="@+id/two_graph"
app:graphPackage="com.xyz.app_name.two"
app:graphResName="two_navigation"
app:moduleName="two" />
feature_two_graph
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
android:id="@id/two_graph"
app:moduleName="two"
app:startDestination="@id/twoFragment">
<fragment
android:id="@+id/twoFragment"
android:name="com.xyz.app_name.two.TwoFragment"
android:label="{nickname}"
tools:layout="@layout/two_fragment">
<argument
android:name="nickname"
app:argType="string" />
<action
android:id="@+id/action_twoFragment_to_dialogFragment"
app:destination="@id/dialogFragment" />
</fragment>
<dialog
android:id="@+id/dialogFragment"
android:name="com.xyz.app_name.two.ui.DialogFragment"
android:label="bottom_sheet"
tools:layout="@layout/bottom_sheet" />
</navigation>
解决方法
我有同样的问题。
当尝试将动态功能图添加为目标时,请按如下所示使用include-dynamic:
<include-dynamic
android:id="@+id/nestedMyfeatureGraph"
app:graphPackage="com.mycompany.myapp.mydynamicfeature"
app:graphResName="nav_graph_feature"
app:moduleName="mydynamicfeature"
/>
请记住,app:graphPackage
应该引用动态功能的 applicationId (我知道,it's confusing)。如果您有任何自定义gradle脚本在 applicationId 中添加后缀或类似的东西,则引用图形将失败。
它可能仅在Playstore上对您有用,因为我猜您的脚本仅为非prodRelease构建类型添加了后缀,这是一种常见模式。
所以,要解决我必须替换的问题
app:graphPackage="com.mycompany.myapp.mydynamicfeature"
使用
app:graphPackage="${applicationId}.mydynamicfeature"