androidkotlinandroid-navigation

Kotlin safe args direction is not generating


I have just created a new project in Kotlin and I wanted to add navigation and safe args in it.
I have created 2 fragment (DishListFragment, DishDetailFragment) and an nav graph with action to one fragment to another in it. I have added one button to my first fragment and set a on click listener using view binding:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.deneme.setOnClickListener {
            val action = DishListFragmentDirections.actionDishListFragmentToDishDetailFragment()
            findNavController().navigate(action)
        }

    }

but when I try to create a direction action named DishListFragmentDirections android studio giving me red errors saying:

Unresolved reference: DishListFragmentDirections

and says I should create DishListFragmentDirections class.

Things I have tried:
Into my app level build.gradle, I have added:

implementation(libs.androidx.navigation.safe.args.generator)
implementation(libs.androidx.navigation.safe.args.gradle.plugin)

I have tried invalidating caches and rebuilding my project but nothing helped me. I have tried doing things in doc but I think they have changed structure or something.

Module build.gradle.kts

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsKotlinAndroid)
}

android {
    namespace = "com.example.myapplication"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.myapplication"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = 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 {
        viewBinding = true
    }

}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.activity)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.navigation.fragment)
    implementation(libs.androidx.navigation.safe.args.generator)
    implementation(libs.androidx.navigation.safe.args.gradle.plugin)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
}

Project build.gradle.kts

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.jetbrainsKotlinAndroid) apply false
}

Solution

  • I know I'm a bit late to the conversation, but here's how I resolved the issue. You can update your Gradle files accordingly. I hope this helps anyone struggling with adding dependencies.

    1. libs.versions.toml

    [versions]
    ####
    
    agp = "8.4.1"
    kotlin = "1.9.0"
    navigation = "2.7.7"
    androidxNavigationSafeargsGradlePlugin = "2.7.7"
    
    
    [libraries]
    ####
    
    androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation" }
    androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation" }
    
    
    [plugins]
    ####
    
    android-application = { id = "com.android.application", version.ref = "agp"}
    jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
    androidx-navigation-safe-args = { id ="androidx.navigation.safeargs.kotlin",version.ref ="androidxNavigationSafeargsGradlePlugin" }
    

    2. Top-level Build file

    // Top-level build file where you can add configuration options common to 
    // all sub-projects/modules.
    
    plugins {
    
     alias(libs.plugins.android.application) apply false
     alias(libs.plugins.jetbrains.kotlin.android) apply false
     alias(libs.plugins.androidx.navigation.safe.args) apply false
     
    }
    

    3. Module-level Build file

    plugins{
    
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.plugins.androidx.navigation.safe.args)
    
    }
    
    android {
       namespace = "com.mvp"
       compileSdk = 34
        defaultConfig {
         applicationId = "com.mvp"
         minSdk = 24
         targetSdk = 34
         versionCode = 1
         versionName = "1.0"
         
     }
    
    //*** other configurations ***//
    // ...........
    
    dependencies {
    
     implementation(libs.androidx.core.ktx)
     implementation(libs.androidx.appcompat)
     implementation(libs.material)
     implementation(libs.androidx.constraintlayout)
     implementation(libs.androidx.navigation.fragment.ktx)
     implementation(libs.androidx.navigation.ui.ktx)
    
    }