Safeargs library doesnt generate direction class

天涯浪子 提交于 2020-07-17 09:20:54

问题


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


回答1:


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.




回答2:


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"



回答3:


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!




回答4:


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



回答5:


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>



回答6:


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!




回答7:


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"



回答8:


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




回答9:


Following two steps works for me:

  1. Clean project.
  2. Rebuild project

Android studio 4.0 Kotlin 1.3.72




回答10:


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")
    }
}



回答11:


Also make sure to use an up to date version. Had the problem, that I had an 1.X version.

// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.2.1"
implementation "androidx.navigation:navigation-ui-ktx:2.2.1"

And the args correctely in the nav.xml:

<fragment
    android:id="@+id/categoryFragment"
    android:name="com...CategoryFragment"
    android:label="CategoryFragment"
    tools:layout="@layout/fragment_category">
    <action
        android:id="@+id/action_categoryFragment_to_ProductFragment" // /!\
        app:destination="@id/productFragment"
        <argument
            android:name="products"
            android:defaultValue=""
            app:argType="string" />
    </action>
</fragment>

Code:

override fun onItemClicked(category: Category) {
     val action = CategoryFragmentDirections.actionCategoryToProductFragment(products = "myString") // /!\
     view?.findNavController()?.navigate(action)
}



回答12:


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




回答13:


In my case, the problem was that I was using the same fragment with the same id in 2 navigation graphs.

Android Studio didn’t recognize the actions of one of the navigation graphs.

So the solution for me was to change the id of one of the navigation graphs, rebuild, change again the id and rebuild again.

After that everything worked again




回答14:


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!



来源:https://stackoverflow.com/questions/50686907/safeargs-library-doesnt-generate-direction-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!