Androidx Navigation 2.3.0 adds navigation support for dynamic feature Modules, so we use this to separate multiple function modules for modularization

Updated navigation 2.3.0

Dynamic feature Module that is basically not used in China

Android App Bundle is an official dynamic distribution scheme launched in 2018, which is similar to various domestic plug-in schemes. However, it requires Google Play Store support, which makes it unavailable in China

Since the navigation component supports navigation between dynamic feature modules, we can use dynamic feature Module to split functional modules to achieve modularization

The traditional split scheme is like this: Feature modules are isolated from each other, and App Module depends on each feature Module indirectly on base library, public library

Traditional architecture

The structure of dynamic feature Module is as follows

The dynamic feature architecture

Dynamic Feature Modules can also be installed on demand, that is, they may not be included in the APK initially downloaded by the user, but installed at run time. We can include them directly in APK

Use the Dynamic Feature Module

First we introduce dependencies in base Lib

dependencies {
    def nav_version = "2.3.0 - alpha06"

    api "androidx.navigation:navigation-fragment-ktx:$nav_version"
    api "androidx.navigation:navigation-ui-ktx:$nav_version"
 api "androidx.navigation:navigation-dynamic-features-fragment:$nav_version" } Copy the code

We create main_nav. XML in the RES /navigation directory in the App Module

main_nav.xml

Next we set the default host in activity_main

The default host

DynamicNavHostFragment is used instead of NavHostFragment

Direct jump fragment

Let’s create a Dynamic feature module called Feature1

Create a Dynamic feature Module
The part before the package name must be the same as the applicationId

The preceding part of the package name of the Dynamic feature Module must be the same as the applicationId of the app Module package name. Otherwise, subsequent include operations may fail

Select load mode

Here we chose to integrate the Module at installation time

Then we created a fragment under this Module called Feature1OneFragment

We then import the fragment directly into main_nav. XML and add the action

Direct introduction of fragments

Then you can open Feature1OneFragment under the App’s MainFragment

Start the fragments

In my demo, feature2 introduced a fragment directly, so it jumped to Feature2OneFragment

Jump to activity

Create an activity in Feature1 (Feature2 in the demo)

Jump the activity

ModuleName also needs to be specified

Start the activity

Use graph inside the Dynamic Feature Module

We can configure a navigation Graph for the Dynamic Feature Module separately to handle jumps within the Dynamic feature Module

Create feature1_nav. XML in Feature1, where startDestination is Feature1OneFragment

feature1_nav.xml

In main_nav. XML we need to use the graph in a different way

include-dynamic

We use a new tag called include-dynamic, and we see several unused attributes

  • graphPackagedynamic feature moduleThe package name
  • graphResNamedynamic feature moduleThe name of the inner graph
  • moduleNameFor the module name

Note: The graphPackage here can be omitted

  1. An exception in the graphId cannot be found if the module package name is not configured in the format described above
  2. The id of the include-dynamic tag must be the same as that of thefeature1_nav.xmlThe id in the navigation TAB is consistent, or the id is not set in the latter TAB

Once you navigate from the App Module to Feature1’s startDestination, you can use its internal logic for subsequent navigation

Include a jump

Description Switch between feature modules

Deep Link is not supported

The Navigation component does not currently support the deep link of Dynamic Include Graph

Therefore, I have not found a particularly elegant way at present, and the known solutions are as follows

  • reflection
  • Use the ServiceLoader
  • Using dependency Injection

demo

About me

I am a Fly_with24

  • The Denver nuggets

  • Jane’s book

  • Github