Android Unresolved reference: findNavController error

Amir picture Amir · Aug 17, 2018 · Viewed 10.6k times · Source

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)

Answer

Amir picture Amir · Aug 20, 2018

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)
}