EN VI

Android - Kotlin safe args direction is not generating?

2024-03-13 20:30:07
Android - 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:

According guide section

  1. Add classpath
buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath(libs.androidx.navigation.safe.args.gradle.plugin)
    }
}
  1. Add plugin
plugins {
    id("androidx.navigation.safeargs.kotlin")
}

The version in libs.versions.toml file

androidx-navigation-safe-args-gradle-plugin = { module = "androidx.navigation:navigation-safe-args-gradle-plugin", version.ref = "navigationVersion" }
  1. Sync and build project

You must have android.useAndroidX=true in your gradle.properties file

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login