Unresolved reference: rememberNavController in jetpack compose

1.1k Views Asked by At

I am trying to implement navigation in jetpack compose but I have added the following dependency as described in google docs

implementation "androidx.navigation:navigation-compose:2.7.3"

but I am getting the error Unresolved reference: rememberNavControllerwhile in the next line which is to implement navController

@Composable
fun NavigationComposable(){
    val navController = rememberNavController()
}

Here is the dependency in the gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.littlePro'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.littlePro"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.2.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}
dependencies {


    implementation "androidx.navigation:navigation-compose:2.7.3"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.7.2'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.2.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}

I have also tried to import some dependencies in the composable file like

import androidx.compose.runtime.Composable
import androidx.navigation.NavDestination
import androidx.navigation.NavHost
import androidx.navigation.NavHostController

@Composable
fun NavigationComposable(){
    val navController = rememberNavController()
}

but it gives the error Unresolved reference: navigation.

3

There are 3 best solutions below

0
On
val nav_version = "2.7.6"
implementation("androidx.navigation:navigation-compose:$nav_version")

This works for me. You can try using version catalog as I am using that. Also, check your gradle version, maybe that's an issue. I am using gradle DSL for managing dependencies. Hope it works

4
On

This one seems to be missing from your imports:

import androidx.navigation.compose.rememberNavController

If this doesn't help then try:

Build-> Clean Project

and then,

Build-> Rebuild Project

Also, you can try File->Invalidate Caches-> Check 1st and 2nd-> Invalidate and Restart

1
On

I have had some issues with 2.7.3, try downgrading to 2.5.0 and see if that helps

implementation "androidx.navigation:navigation-compose:2.5.0"