Why is android studio not generating code for all the safe args?
Asked Answered
S

3

14

I am using safe-args to pass arguments from one fragment to the other. Android studio intermittently generates the fragmentArgs class with all the arguments.

What I have tried, and does work, is altering the nav graph file, making the project, then undo those changes, and finally, make the project again.

Dependencies Used:

Project module:

ext.nav_version = '2.2.0-beta01'

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

App module:

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

    <fragment
            android:id="@+id/someFragment"
            android:name="package_name.SomeFragment"
            android:label="@string/some_fragment">
        <argument
                android:name="source"
                android:defaultValue="-1"
                app:argType="integer" />
        <argument
                android:name="nationalId"
                android:defaultValue="-1"
                app:argType="integer" />
        <argument
                android:name="hudumaNumber"
                android:defaultValue="-1"
                app:argType="string" />
        <argument
                android:name="middleName"
                android:defaultValue="None"
                app:argType="string" />

    </fragment>

I expect that all the arguments will be found in the generated class, but that is not the case.

Scissile answered 12/11, 2019 at 8:9 Comment(2)
show us the code of generated classesHollister
I explained it there check this link: https://mcmap.net/q/830625/-navigation-component-no-directions-class-generatedEscarole
C
12

Try switching to:

apply plugin: "androidx.navigation.safeargs"

instead of:

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

This will generate Java code instead of Kotlin code inside your app\build\generated folder. This doesn't effect your own code and you can continue to use Kotlin or Java for your Fragments.

Circumscribe answered 16/1, 2020 at 8:22 Comment(2)
Switching to androidx.navigation.safeargs indeed helped. I wonder why is this happening thoUndershorts
NB: Java functions cannot have named parameters eg foo(bar = 1) so if you use those, you will get an error when making this changeBeebread
C
2

You just need to rebuild your project,

  1. Delete any lines that cause error for now
  2. Rebuild your project
  3. Add those lines again
Cordwood answered 3/5, 2020 at 20:15 Comment(1)
thanks. haha, been using safeargs for years but for some reason this was unintuitiveBookcase
B
1

My problem was that I had deleted the name attribute in nav_graph.xml

 <fragment
        android:id="@+id/emailVerifyFragment"
        android:name="com.package.name.EmailVerifyFragment" <<<--- this is necessary!
        tools:layout="@layout/fragment_email_address_verify">
Beebread answered 10/6, 2021 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.