Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LLin233 opened this issue Jun 26, 2019 · 9 comments

Comments

@LLin233
Copy link

LLin233 commented Jun 26, 2019

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!

@damienlo
Copy link

@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.

@hamedsilver
Copy link

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

@i-am-mani
Copy link

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

@engi2nee
Copy link

engi2nee commented Dec 25, 2020

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.

@eyal-lezmy
Copy link

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.

@engi2nee
Copy link

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

@P1NG2WIN
Copy link

@engi2nee Can you explaine what does it do?

@rhlmshr
Copy link

rhlmshr commented Mar 6, 2021

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

@tugceaktepe
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants