4

I'm using NavigationComponent in my app and now I'm facing following problem:

I have 2 different flavors (free and paid version) which are using the same navigation.xml located in the main package. When I want to run e.g. the paid variant it failed with these errors in the generated directions file:

e: AndroidStudioProjects/foo/app/build/generated/source/navigation-args/paid/debug/de/dk/fc/ui/knowledge/KnowledgeFragmentDirections.kt: (5, 24): Unresolved reference: paid

e: AndroidStudioProjects/foo/app/build/generated/source/navigation-args/paid/debug/de/dk/fc/ui/knowledge/KnowledgeFragmentDirections.kt: (14, 41): Unresolved reference: R

Here is a snippet of my navigation.xml

<fragment
        android:id="@+id/fragment_knowledge"
        android:name="de.dk.fc.ui.knowledge.KnowledgeFragment"
        tools:layout="@layout/fragment_knowledge">

        <action
            android:id="@+id/action_fragmentKnowledge_to_fragmentInfo"
            app:destination="@id/fragment_info" />

    </fragment>

I also added these lines of code to my build.gradle file:

 sourceSets {
        getByName("main").java.srcDirs("build/generated/source/navigation-args")
    }

What I've tried:

  • Clean Project
  • Invalidate Cache
  • Duplicated navigation.xml to each flavor package

Do you have any ideas how to solve this problem?

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Deno Agüero
  • 519
  • 2
  • 9
  • 27
  • It seems that you are referencing something which is not included in your build flavor. Can you share more details or what is `paid` reference in your navigations xml? – hardartcore May 20 '22 at 07:14

2 Answers2

4

Did you upgrade to the Android Gradle Plugin version 7.2.0 and ran the recommended before this error occurred? One of them is to move the package name from the Android manifest file to the build files.

It seems that this change causes difficulties with the navigation component when having different applicationIds in the project (as you probably have for your different flavours).

Simply put the package name back into the manifest file and it should work again. This should fix it at least temporarily until the navigation component can handle the changes.

J. Cervus
  • 185
  • 10
0

If you are working with multi-modular project i recommend you to create a separate nav graph for each feature/module that needs one, as shown here MAD Skills navigation between modules.

in your main package nav graph use the tag <include app:graph="@navigation/graph_name"/> to include the graph from a different module

it should work even if you have moved package name from manifest to build files