I use navigation library and safeargs for passing data. I define argument to fragment like that.
As Christian wrote, adding source set do app gradle file helped. The problem occurs in Android Studio 4.1 and 4.2 for me. In Kotlin DSL:
android {
....
sourceSets {
getByName("main").java.srcDirs("build/generated/source/navigation-args")
}
}
If anybody's having trouble with auto-generated classes with SafeArgs, check your AndroidStudio version. I was using 4.1 beta (at this moment) and when downgraded to 4.0 it works perfectly. Definitely do try that. Cheers!
For me, I realized that I also needed to put the same arguments into the receiver fragment in navi_graph.xml as well in order for Arg class to be generated(already have directions). For example:
<fragment android:id="@+id/receiver_fragment" ...>
<argument android:name="arg_from_sender_fragment".../>
<argument android:name="arg2_from_sender_fragment".../>
</fragment>
In your app/module level add the following dependency :
def nav_version = "2.3.0-alpha04" //your nav_version defined
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
all the answers are correct too. just felt that I should add this info. It worked for me
If all the answers above didn't work for you and you are still having a problem with the directions class not being generated. Make sure you are looking for the right directions class.
For example, we have to pass arguments from FragmentA
to FragmentB
.
In the navigation graph file, we have to add the <argument/>
tag under FragmentB
's tag. This way, we will get two generated classes FragmentBArgs
and FragmentADirections
. As you can see, it is the FragmentA
's name who got used for the directions class created, not FragmentB
.
So, keep in mind that unlike what this question's user is looking for, the directions class generated will get the class name of the action starter fragment appended with the word "Directions". Not the destination fragment's class name (argument's owner).
Here is what the documentation says:
A class is created for each destination where an action originates. The name of this class is the name of the originating destination, appended with the word "Directions".
A class is created for the receiving destination. The name of this class is the name of the destination, appended with the word "Args".
Also, adding the argument tag under the action tag will not generate a directions class. It's used to give a different default value for the destination fragment's argument.
Hope this helps!
Look for the class of the fragment which is the source of navigation. If you define navigation from FragmentA to FragmentB, you will find FragmentADirections class with the actions you defined (in nav_graph.xml) in it.
Then, to generate direction class ( Also argument class) you need to go Project level gradle
then click the build
command. Here I attached a screenshot to give a clear understanding.