Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 706
wamae Avatar asked Nov 12 '19 08:11

wamae


People also ask

What is safe args in Android Studio?

Use Safe Args to pass data with type safety. The Navigation component has a Gradle plugin called Safe Args that generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data, because it ensures type-safety.

Is there any dependency that needs to get added before using safe args?

SafeArgs is not the same kind of library module as the other parts of navigation; it's not an API per se, but rather a gradle plugin that will generate code. So I needed to pull it in as a gradle dependency and then apply the plugin to run at build time, to generate the necessary code.


Video Answer


3 Answers

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.

like image 181
Stephen Mullen Avatar answered Oct 17 '22 11:10

Stephen Mullen


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">
like image 23
MSpeed Avatar answered Oct 17 '22 11:10

MSpeed


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
like image 1
Mahmoud Hendi Avatar answered Oct 17 '22 11:10

Mahmoud Hendi