Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safeargs library doesnt generate direction class

I use navigation library and safeargs for passing data. I define argument to fragment like that.

<fragment
        android:id="@+id/otherFragment"
        android:name="com.asd.navigate.OtherFragment"
        android:label="OtherFragment">
        <argument
            android:name="screenTitle"
            android:defaultValue="0"
            app:type="string" />
    </fragment>

OtherFragmentArgs generated, I can use it but OtherFragmentDirection class doesnt generate when I click "make project". Is that bug or I have to do something different.

Thnx for advice.

buildscript {
    ...
    dependencies {
       ...
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha01"

    }
}

build.gradle

apply plugin: "androidx.navigation.safeargs"

MainActivity.kt

enter image description here

like image 718
6155031 Avatar asked Jun 04 '18 18:06

6155031


3 Answers

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.

enter image description here

like image 199
shmulik.r Avatar answered Nov 09 '22 09:11

shmulik.r


I forgot to apply plugin in app.gradle file, just add this line

apply plugin: "androidx.navigation.safeargs.kotlin"

or this line if you are using java

apply plugin: "androidx.navigation.safeargs"
like image 89
Sanjeev Avatar answered Nov 09 '22 09:11

Sanjeev


I faced a similar issue working with android studio 4.2 preview.

The issue was that the IDE somehow was not picking the source folders where the generated direction classes where being placed by the plugin. So adding the following to my app build.gradle file resolved the issue for me.

sourceSets {
    main {
        java {
            srcDirs += 'build/generated/source/navigation-args'
        }
    }
}

Hope this helps!

like image 50
Christian Avatar answered Nov 09 '22 08:11

Christian