24

I am using Kotlin and have all references added in my project.

// Navigation
implementation "android.arch.navigation:navigation-common-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-runtime-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$rootProject.nav_version"

I also have these on top of the build.gradle

apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' apply plugin: 'androidx.navigation.safeargs'

And I am using it like this inside my fragment

class HomeFragment : BaseFragment(){

    ...

    override fun onCategoryItemClicked(category: Category) {
        view.findNavController()?.navigate(R.id.phrasesFragment)
    }
}

I can see this generated extension(file) too

fun Fragment.findNavController(): NavController =
    NavHostFragment.findNavController(this)
Amir
  • 1,290
  • 1
  • 13
  • 20

3 Answers3

7

After lots of try and error, I found the source of issue. upgrading my gradle to gradle:3.3.0-alpha06 was the key. I revert it to the previous version and it works fine now. So I think something is happening there which needs to be fixed by #Google.

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha05'

By the way while using tha latest version of the gradle(At the time of writing this, I mean gradle:3.3.0-alpha06), this will work

    Navigation.findNavController(view!!).navigate(R.id.phrasesFragment)

instead of

override fun onCategoryItemClicked(category: Category) {
    view.findNavController()?.navigate(R.id.phrasesFragment)
}
Amir
  • 1,290
  • 1
  • 13
  • 20
  • 2
    Thanks for the heads up, this was a very frustrating issue for me as well. New Gradle update borked half of the dependencies i worked with. Made today a wild goose chase! Come on Google! Do BETTER! – Patty P Aug 22 '18 at 19:34
  • I also noticed this but at first I tough was something else cause I upgraded different things along with the gradle plugin https://issuetracker.google.com/issues/112806420 – Daniele Segato Aug 29 '18 at 18:24
  • @DanieleSegato I raised an issue against Gradle but it turned out its google or android studio problem so I don't know where to raise it! – Amir Aug 30 '18 at 04:34
  • There are multiple issue about this on the Android issue tracker, just go star them :-) – Daniele Segato Aug 30 '18 at 04:38
1

In my case, downgrading navigation libraries helped.

I downgraded from 2.3.5 to 2.3.4.

implementation 'androidx.navigation:navigation-ui-ktx:2.3.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'

2.3.5 was latest stable at the time of writing this answer.

Just try changing version of the library, and see which works. Find versions here.

Ananth
  • 2,597
  • 1
  • 29
  • 39
-1

Just add this in your build.gradle(:app) -

plugins {
    id 'kotlin-android-extensions'
}
  • 1
    `kotlin-android-extensions` is deprecated best not using it anymore https://developer.android.com/topic/libraries/view-binding/migration – Amir Aug 24 '22 at 05:46
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 26 '22 at 14:05