1

I am trying to convert some part my code from java to kotlin. Currently, I am facing a problem. To scope a viewmodel to NavGrap im using

private val mPrintsViewModel : PrintsViewModel by navGraphViewModels(R.id.nav_print)

It compiles fine. But I get an error while running the app.

Unresolved reference: navGraphViewModels

I viewed several solutions like

  1. Add following lines to gradle
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
  1. Clear cache and restart

  2. Verify if the IDE kotlin version matches with the project kotlin version.

None of the above solutions work.

This issue happens for all kotlin extension functions.

I am using kotlin-1.4.10

Similar question : Question 1, Question 2

1 Answers1

1

The navGraphViewModels property delegate is part of the navigation-fragment-ktx dependency. It isn't part of Kotlin, nor part of the kotlin-android or kotlin-android-extensions plugins.

Make sure your dependencies block has a dependency on navigation-fragment-ktx (and specifically not just navigation-fragment.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443