Things you should do in case your NavDirections class is not generating properly
So, recently I’ve been working a lot with the nav components. The most frustrating thing about it that sometimes some of the automatically generated NavDirections class would not generate properly. Just for no reason. I would have an error pop-up saying that a certain fragment could not be found in my navigation graph, but after quadruple-checking everything I was 100% sure that the problem is not my code.
Anyways, StackOverflow didn’t really help either as I would find answers similar to this one. And as one of the people pointed out there the problem looks like a cache one indeed. So here’s the algorithm that worked for me:
- Clear and Invalidate cache. File -> Invalidate Caches… -> Tick the “Clear file system cache and Local History” -> Invalidate and Restart
- In your app-level build.gradle file in the “plugins” section remove the ’androidx.navigation.safeargs.kotlin’.
- Try to build the app after that (it’s going to fail of course)
- Then insert the safeargs back again and build the app.
Voila! My stubborn NavDirections class got generated properly.
Let me know in the comments if you have also faced a similar problem and how have you resolved it.