I use navigation library and safeargs for passing data. I define argument to fragment like that.
I followed all instructions to the letter and noticed the Directions classes were being generated, but would not compile when called in code. This happened while using Android Studio 4.1 and 4.2 Preview.
After a few hours, I decided to try downgrading to 4.0 Stable. All issues are now gone, I can call the FragmentDirections and it compiles.
I added a bug in the issue tracker: https://issuetracker.google.com/issues/158777967
If you already added the dependencies > classpath
and apply plugin
but changes aren't applied, be sure to Clean
and Built
the project in order changes are applied.
In Android Studio:
Build tab > Clean project
Build tab > ReBuild project
In my case I manually added the arguments... in the wrong place
We can fix this issue by adding the below codes in gradle and rebuilding the app. In the project gradle add below code.
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"
In app gradle add below code
apply plugin: "androidx.navigation.safeargs"
In my case, the missing part was firstly in the build.gradle.kts
buildscript {
repositories {
google()
}
dependencies {
def nav_version = "X.X.X" // Use the latest version
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
and then, I had to add the plugin in app and navigation modules (I usually separate navigation in a different one):
apply plugin: "androidx.navigation.safeargs.kotlin"
Clean -> Build But still had to add manually the path to the action
import yourpackage.FragmentDirections.Companion.actionFirstFragmentToSecondFragment
Follow these steps:
Step 1: Add dependencies
def nav_version = "2.3.2"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Step 2: In your app-level
build.gradle
make sure you have below plugins added.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin'
}
Step 3: Add this in project_level
build.gradle
inside dependencies section :
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.2"
Step 4: In your
nav_graph.xml
make sure to connect one fragment to another withactions
.Step 5: Clean & Rebuild your project.