Reputation: 207
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: rememberNavController
while 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
.
Upvotes: 3
Views: 3507
Reputation: 91
To implement navigation component by Jetpack android, you will be required to add these 3 dependencies in your build.gradle file (app):
// Navigation component
implementation libs.androidx.navigation.fragment
implementation libs.androidx.navigation.navigation.ui
implementation libs.androidx.navigation.navigation.compose
Here are the changes in your libs.versions.toml file:
Under [versions] section, add this line of code:
navVersion = "2.8.7"
Under [libraries] section, add the following lines of code:
androidx-navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navVersion"}
androidx-navigation-navigation-ui = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navVersion"}
androidx-navigation-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navVersion"}
Upvotes: 0
Reputation: 31
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
Upvotes: 3
Reputation: 2214
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
Upvotes: 1
Reputation: 162
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"
Upvotes: 1