androidandroid-architecture-navigationdynamic-feature-module

Dynamic feature module navigation IllegalStateException: The included <navigation>'s id is different from the destination id


I have one app and one dynamic feature module i wish to navigate from

App nav 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/nav_graph"
    app:startDestination="@id/mainFragment">

    <!-- Main Fragment from App Module -->
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.xyz.MainFragment"
        android:label="MainFragment"
        tools:layout="@layout/fragment_main">
        <action
            android:id="@+id/action_mainFragment_to_nav_graph_home"
            app:destination="@id/nav_graph_home" />
    </fragment>

    <!-- Home Navigation from App Module-->
    <include app:graph="@navigation/nav_graph_home" />

    <include-dynamic
        android:id="@+id/nav_graph_dashboard"
        android:name="com.feature.dashboard"
        app:graphResName="nav_graph_dashboard"
        app:moduleName="dashboard" />

</navigation>

And feature navigation

<?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/nav_graph_dashboard"
    app:moduleName="dashboard"
    app:startDestination="@id/dashboardFragment1">

    <fragment
        android:id="@+id/dashboardFragment1"
        android:name="com.feature.DashboardFragment1"
        android:label="DashboardFragment1">
    </fragment>

</navigation>

Returns error

java.lang.IllegalStateException: The included <navigation>'s id com.xyz.dashboard:id/nav_graph_dashboard is different from the destination id com.xyz:id/nav_graph_dashboard. Either remove the <navigation> id or make them match.

It seems that removing id from feature navigation solves the issue but i couldn't find how to make them match even though both <include-dynamic> and <navigation> have the same id android:id="@+id/nav_graph_dashboard" I don't need id for <navigation> for this example but i wonder if it's possible when <navigation> has one


Solution

  • Remove the + from your ID in your feature navigation graph:

    android:id="@id/nav_graph_dashboard"
    

    When you use the @+id syntax, you create a new ID in your dynamic feature's package (you'll note the exception explicitly calls out the package names for each resource ID for exactly that reason). By removing the +, you use the already created ID from the main module's package, which makes them match.