architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

How to use navigation components with multi-Module application and BottomNavigationView

Open LLin233 opened this issue 5 years ago • 9 comments

https://stackoverflow.com/questions/54616996/android-navigation-component-and-bottomnavigationview-in-a-multi-module-application

I am facing extract same circumstance here... trying to create a multi-module application where every feature is a standalone library (module). Single activity pattern is applied throughout the project, created different navGraphs for different modules and included them in app's navGraph, but can't make it work.

could anyone give me some ideas? Thank you!

LLin233 avatar Jun 26 '19 04:06 LLin233

@LLin233 You can create a module "navigation" and move your graph(s) there. References to Fragments will be in highlighted red but it compiles and it works.

All your feature modules can take that navigation as dependency in order to access the safeArgs (if you are using them) and the actions.

damienlo avatar Jun 26 '19 11:06 damienlo

@LLin233 check my sample https://github.com/hamedsilver/ArchApp

hamedsilver avatar Aug 22 '20 12:08 hamedsilver

@damienlo is this method still the preferred way? or any other better options are available now?

i-am-mani avatar Oct 15 '20 09:10 i-am-mani

as an addition to @damienlo fix, you can do a "gradle hack" to share the resources between the navigation module and the app module (assuming it has access to all feature modules), this way the fragment names won't be highlighted as red. credits to @eyal-lezmy in this talk (skip to 44:00): https://www.droidcon.com/media-detail?video=334638906

  1. First create a new package under main in your navigation module called "sharedRes", and move the navigation resource folder with the navigation graphs xml into it.
  2. in the app's build.gradle add android { sourceSets.debug.res.srcDirs += "$rootDir/navigation/src/main/sharedRes"}}
  3. in the navigation module's build.gradle add android { sourceSets.main.res.srcDirs += "$rootDir/navigation/src/main/sharedRes" }
  4. clean build and rebuild.

engi2nee avatar Dec 25 '20 20:12 engi2nee

If I remember well this hack is not "working" anymore with the rendent versions of Android Studio. But in our project we end up using a symbolic link to duplicate the file in both modules.

eyal-lezmy avatar Dec 26 '20 00:12 eyal-lezmy

@eyal-lezmy weird, it is working correctly on our project, using the latest version of navigation component library and android studio 4.1.1.

engi2nee avatar Dec 27 '20 12:12 engi2nee

@engi2nee Can you explaine what does it do?

P1NG2WIN avatar Feb 15 '21 21:02 P1NG2WIN

@P1NG2WIN Refer the video at 44:00. It is discussed what it does behind the scenes.

rhlmshr avatar Mar 06 '21 17:03 rhlmshr

damienlo I created navigation module and move all nav graphs there. Yes compilation it's ok but it gives runtime error like this:


 Caused by: android.view.InflateException: Binary XML file line #52 in com.mypackage:layout/activity_main: Binary XML file line #52 in com.mypackage:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
                                                                                                    Caused by: android.view.InflateException: Binary XML file line #52 in com.mypackage:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
                                                                                                    Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.mypackage.login.splash.SplashFragment: make sure class name exists

Android Studio Flamingo 2022.2.1 Patch 2 AGP version 8.0

tugceaktepe avatar Jun 30 '23 14:06 tugceaktepe