how to get viewModel by viewModels? (fragment-ktx)
Solution 1:
Atlast we we have got stable version.
After moving to implementation 'androidx.fragment:fragment-ktx:1.1.0'
i faced another issue.
Compiler Error:
Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6
build.gradle (Module:app)
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
kotlinOptions {
jvmTarget = "1.8"
}
reference
After applying all the above the issue's is resolved.
Thanks everyone.
Solution 2:
try
implementation 'androidx.fragment:fragment-ktx:1.1.0-beta02'
2021 UPDATE
This is no longer in beta. It works with the latest stable version at the time of writing. You will also need to import it in your fragment's Kotlin file.
implementation 'androidx.fragment:fragment-ktx:1.3.2'
import androidx.fragment.app.activityViewModels
Solution 3:
I think you need to have this dependency for your activity if you want to use kotlin delegations for viewmodel
//ViewModels delegation extentensions for activity
implementation 'androidx.activity:activity-ktx:1.3.1'
Solution 4:
You use this latest alpha version:
dependencies {
implementation 'androidx.fragment:fragment-ktx:1.2.0-alpha01'
}
Solution 5:
The extension function viewModels(...) was added in version 1.1.0-alpha03. Therefore, in order to use it in your app you will have to implement the fragment-ktx of versions matching 1.1.0-alpah03 or later.
I just found out about this and so I resorted to using version 1.1.0-rc01 because I didn't want to use alpha/beta versions.