'Android : `androidx.navigation.NavArgs` not found

I fetch this below code from From Codelab for navigation controller

But getting this below error: spend already more the 3 hours but not any success. Here is my error

Please help me to solve this error.



Solution 1:[1]

Change this

  implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.0-alpha01'
  implementation 'android.arch.navigation:navigation-ui-ktx:2.2.0-alpha01'

to this:

  implementation 'androidx.navigation:navigation-fragment-ktx:2.2.0-alpha01'
  implementation 'androidx.navigation:navigation-ui-ktx:2.2.0-alpha01'

Notice that android.arch.navigation is replaced with androidx.navigation

also, make sure to apply this plugin on top of build.gradle:

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

and this to the dependencies to the project level build.gradle:

 classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-rc01"

Solution 2:[2]

If you are using kotlin and androidX than replace this

apply plugin: "androidx.navigation.safeargs"

with

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

and in project level build.gradle

dependencies {
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-alpha01"
}

on app level build.gradle

def nav_version = "2.1.0-alpha01"

implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version" 

If you are using kotlin Ktx

implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Hope this will help other people also. It helps me too

Solution 3:[3]

Try to add

classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha05"

Instead of

classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-rc02"

Solution 4:[4]

Version 3.3.1 of Android Studio have similar problems like this but solution is simple. Close project and Import your project again. You will see the result.

Solution 5:[5]

In your build.gradle app level add this:

apply plugin: 'androidx.navigation.safeargs'

Update:

This codelab is very old try not updating libraries.

Solution 6:[6]

I tried everything which is written above i.e adding all plugins and dependencies (which is completely right though) but I was doing this silly mistake -> I was creating navArgs variable outside the fragment

Don't ->

private val args by navArgs<VideoCallFragmentArgs>()

class YourFragment : Fragment() {

     override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
          ): View? {
    return inflater.inflate(R.layout.fragment_video_call, container, false)
}
}

Always create navArgs variable inside the fragment.
Do ->

class YourFragment : Fragment() {

private val args by navArgs<VideoCallFragmentArgs>()

     override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
          ): View? {
    return inflater.inflate(R.layout.fragment_video_call, container, false)
}
}

Solution 7:[7]

Another possible case:

Check in your code that you have actually defined argument tag in the xml file (sender && receiver).

In another case, Args will not be generated.

Following code will not generate Args:

<fragment
    android:id="@+id/receiverFragment"
    android:name="com.example.app.ReceiverFragment"
    android:label="ReceiverFragment" >
</fragment>

Following code will generate Args:

<fragment
    android:id="@+id/receiverFragment"
    android:name="com.example.app.ReceiverFragment"
    android:label="ReceiverFragment" >
    <argument
        android:name="myArgument"
        app:argType="integer"
        app:nullable="false" />
</fragment>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amin Keshavarzian
Solution 2 Harsh Mittal
Solution 3
Solution 4 eOlcer
Solution 5
Solution 6 Kushal Sharma
Solution 7