54

Out of nowhere, the build is crashing with a strange error related to the navigation component even though it used to work before, the error is in the generated class, in my case NativeLanguageSelectionFragmentDirections

Here is the error

e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (10, 16): Class 'GoToSelectLearningLangAction' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (13, 12): 'getActionId' overrides nothing
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (15, 12): 'getArguments' overrides nothing

And here is the code of the navigation graph

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/first_login_graph.xml"
    app:startDestination="@id/nativeLanguageSelectionFragment">

    <fragment
        android:id="@+id/nativeLanguageSelectionFragment"
        android:name="com.linguistic.linguistic.framework.presentation.loginscreens.ui.main.NativeLanguageSelectionFragment"
        android:label="NativeLanguageSelectionFragment"
        tools:layout="@layout/language_selection_fragment">
        <action
            android:id="@+id/goToSelectLearningLangAction"
            app:destination="@id/learningLanguageSelectionFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/nativeLanguageSelectionFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/learningLanguageSelectionFragment"
        android:name="com.linguistic.linguistic.framework.presentation.loginscreens.ui.main.LearningLanguageSelectionFragment"
        android:label="LearningLanguageSelectionFragment"
        tools:layout="@layout/language_selection_fragment">
        <argument
            android:name="nativeLanguageID"
            app:argType="string"
            app:nullable="true" />
        <action
            android:id="@+id/action_learningLanguageSelectionFragment_to_welcomeFragment"
            app:destination="@id/welcomeFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/learningLanguageSelectionFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/welcomeFragment"
        android:name="com.linguistic.linguistic.framework.presentation.loginscreens.ui.main.WelcomeFragment"
        android:label="fragment_welcome"
        tools:layout="@layout/fragment_welcome" />
</navigation>

I am using

 "androidx.navigation:navigation-fragment-ktx:2.3.5"
 "androidx.navigation:navigation-ui-ktx:2.3.5"
David Ibrahim
  • 2,777
  • 4
  • 17
  • 41
  • did u try invalidating cache and restart? – ked May 19 '21 at 19:55
  • Yes, for sure, I tried another android Studio version xD – David Ibrahim May 19 '21 at 19:58
  • 2
    That sounds like [this issue](https://issuetracker.google.com/188564435), but that issue only affects Navigation 2.4.0-alpha01. Are you sure you aren't using Navigation 2.4.0-alpha01? – ianhanniballake May 19 '21 at 20:10
  • 2
    yes I am using navigation `androidx.navigation:navigation-compose:2.4.0-alpha01` for compose since part of the project is written in android compose – David Ibrahim May 19 '21 at 20:13
  • 1
    even after downgrading the dependency mentioned in my previous comment to `androidx.navigation:navigation-compose:1.0.0-alpha10`, I am still facing the same issue – David Ibrahim May 20 '21 at 07:24

7 Answers7

74

I had this problem too. Until they release the fix. Please try this:

plugins {
  id("androidx.navigation.safeargs")
}

instead of

plugins {
  id("androidx.navigation.safeargs.kotlin")
}
Reza
  • 1,164
  • 11
  • 9
  • This was a quick way of working around the same issue for me, thanks. Are there any drawbacks of using the non-Kotlin version? – Martin Gunnarsson Nov 01 '21 at 12:38
  • 7
    You can't use named arguments for example. – peshkira Nov 16 '21 at 10:55
  • 2
    What is the cause of this problem?... What is the difference between these two plugins – Venkataramanan Mar 07 '22 at 06:59
  • 1
    @Venkataramanan, to generate Java language code suitable for Java or mixed Java and Kotlin modules use androidx.navigation.safeargs, to generate Kotlin code suitable for Kotlin-only modules use androidx.navigation.safeargs.kotlin, from [here](https://developer.android.com/guide/navigation/navigation-pass-data#Safe-args) – Yura Shinkarev May 04 '22 at 08:21
41

As for my case, I had to update Safe Arg plugin as well to get rid of the error message.

[build.gradle]
buildscript {
    ...
    dependencies {
        ...
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-alpha04'
}

[app/build.gradle]
dependencies {
    ...
    implementation 'androidx.fragment:fragment-ktx:1.4.0-alpha04'
    api 'androidx.navigation:navigation-fragment-ktx:2.4.0-alpha04'
    api 'androidx.navigation:navigation-ui-ktx:2.4.0-alpha04'
}
solamour
  • 2,764
  • 22
  • 22
  • 3
    This solution works for me. Changing from "2.3.5" to "2.4.0-alpha04" fix my error. Got some warnings ⚠ when building, but works. Thanks – QuartZ Jul 17 '21 at 20:03
  • 2
    Thanks, I just forgot to update the `nav_version` of `androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version` in the top-level gradle file to the same `nave_version` which is in the module's gradle file. – James Bond Dec 19 '21 at 11:37
13

All of these dependencies

androidx.navigation:navigation-fragment-ktx
androidx.navigation:navigation-ui-ktx
androidx.navigation:navigation-safe-args-gradle-plugin

need to be using the same version - ex: at the time of writing this it's 2.4.0-alpha10

ink
  • 159
  • 2
  • 10
11

Since the version v2.4.0-alpha02 was released, this known bug is no longer present.

Bug Fixes

  • Safe Args no longer crashes when attempting to generate direction properties in Kotlin. (Id2416, b/188564435)
GoRoS
  • 5,183
  • 2
  • 43
  • 66
2

just update the safe-args dependency to the latest version(>=2.4.0-rc01) too!:

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-rc01"
Boken
  • 4,825
  • 10
  • 32
  • 42
imansdn
  • 968
  • 1
  • 7
  • 17
1

in my case, I get this error for my new module. I added the safe-args classPath in build.gradle (project)

KOTLIN-DSL:

    "classpath"(group = "androidx.navigation",name = "navigation-safe-args-gradle-plugin", version = "2.4.1")

without the plunging in my module build.gradle. so I added this on top of build.gradle:

id("androidx.navigation.safeargs.kotlin")

GROOVY:

classpath  "navigation-safe-args-gradle-plugin : 2.4.1"

and:

id 'androidx.navigation.safeargs.kotlin'
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

2.4.0-beta02 at this moment available