Error in finding NavHostFragment Jetpack Navigation Android

Related searches

I am using Jetpack Navigation for handling the navigation of Fragments. After following the documentation, when I run the app I am getting the below error.

Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.navigation.fragment.NavHostFragment" 


apply plugin: ''
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 'android-P'

defaultConfig {
    applicationId "com.meditab.imspatient"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {

def nav_version = "1.0.0-alpha01"

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.constraintlayout:constraintlayout:1.1.0' // Constraint layout

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // Kotlin

// Anko
implementation "org.jetbrains.anko:anko-commons:$anko_version" // Anko Commons
implementation "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
implementation "org.jetbrains.anko:anko-coroutines:$anko_version" // Anko Coroutines

// Support libraries
implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1' // Support v4
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1' // AppCompat
implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1' // RecyclerView
implementation '' // Design Support library
implementation 'androidx.cardview:cardview:1.0.0-alpha1' // CardView

// Jetpack Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"

// Dagger 2
implementation ''
kapt ''
provided 'javax.annotation:jsr250-api:1.0'

// Retrofit, OkHttp, OkHttpInterceptor and GSON
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation ''
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

implementation 'com.rengwuxian.materialedittext:library:2.1.4' // Material EditText

kotlin {
experimental {
    coroutines "enable"

Code of the Activity hosting the NavHostFragment

override fun onCreate(savedInstanceState: Bundle?) {

// Handle the clicks for bottom navigation view
bottom_navigation_view_homepage_activity.setOnNavigationItemSelectedListener { item ->

    when (item.itemId) { -> findNavController(item.itemId).navigate( -> findNavController(item.itemId).navigate(




Activity's XML file

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""

        app:menu="@menu/bottom_navigation_view" />

I have implemented the Navigation Graph properly. Also, the project is refactored to AndroidX.

If this issue happens while using a release build, chances are that the class name has been minified by proguard. That way, the reference android:name="androidx.navigation.fragment.NavHostFragment" in the XML file will not match any valid class.

Try to add this line in the proguard file

-keepnames class androidx.navigation.fragment.NavHostFragment

so that the class name does not get minified and that it stays as mentionned in the XML file.

NavHostFragment, The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with� Jetpack Navigation Graph in Android Studio — Image Credits: Using Jetpack Navigation, you can separate navigation flow from the other parts of code like business logic, database operations etc. This way navigation flow can be easily modified without having to make many changes in the code.

Try to call Navigation.findNavController(bottom_navigation_view_homepage_activity,

You also don't need to set setOnNavigationItemSelectedListener, just edit your navigation graph "nav_graph" as follow:


Basically set your fragment id in nav_graph same as the item ids in your bottom nav view.

Get started with the Navigation component, To add a dependency on Navigation, you must add the Google Maven repository to your project. Your feedback helps make Jetpack better. Please Bug fixes. Fixed an issue where adding a NavHostFragment to ViewPager2 failed with an� NavHostFragment provides an area within your layout for self-contained navigation to occur. NavHostFragment is intended to be used as the content area within a layout resource defining your app's chrome around it, e.g.:

  1. in kotlin, you dont need call findviewbyid, please implement databinding in your layout.

  2. please check if your 'id_menu' called in Bootomnavigaiton is same with 'id_fragment' in navigation graph

  3. you can use this method to nagigate bottom_nav to show fragment like his


    navController = Navigation.findNavController(this,
    NavigationUI.setupActionBarWithNavController(this, navController)

and dont forget to overide thid method

override fun onSupportNavigateUp(): Boolean {
    return NavigationUI.navigateUp(navController,null)

please tell me if this code work or not.

Navigation, NavHostFragment provides an area within your layout for self-contained navigation to occur. NavHostFragment Find a NavController given a local Fragment . In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears. Type a name in the File name field, such as "nav_graph". Select Navigation from the Resource type drop-down list, and then click OK.

Navigate to a destination, Navigation refers to the interactions that allow users to navigate across, into, and back out from the different pieces of content within your app. Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer.

NavHostFragment.findNavController(Fragment) Navigation.findNavController(Activity, @IdRes int viewId) Navigation.findNavController(View) After you've retrieved a NavController, you can call one of the overloads of navigate() to navigate between destinations. Each overload provides support for various navigation scenarios, as described in the

Android JetPack Navigation Architecture Component. The Navigation Architecture component is a part of the new AndroidX package that’s introduced since Android SDK 28.

  • AS 3.2 Canary 15 is available now, check if it fixes your problem