I am using:
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
annotationProcessor 'androidx.lifecycle:lifecycle-common-java8:2.7.0'
libraries in my Android Java project. If I try to move it to 2.8.0
version, I have a message from compiler:
ViewModelProvider(androidx.lifecycle.viewmodel.ViewModelProviderImpl) has private access in 'androidx.lifecycle.ViewModelProvider
.
UPDATE:
Thanks to @IanHannibalLake, I have replaced deprecated library
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
with implementation 'androidx.lifecycle:lifecycle-process:2.7.0'
and now I am able to compile the project with 2.7.0
libraries version in the configuration:
implementation 'androidx.lifecycle:lifecycle-process:2.7.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
annotationProcessor 'androidx.lifecycle:lifecycle-common-java8:2.7.0'
But an attempt to compile with 2.8.0
versions give me the same error from the compiler on the string:
SearchListViewModel viewModel = new ViewModelProvider(this, viewModelFactory).get(SearchListViewModel.class);
What change was made in the lifecycle
libraries in version 2.7.0
that prevents me from compiling the project with the next version 2.8.0
!?
UPDATE 2: I have found a fresh bug ViewModel 2.8.0: constructor has private access in ViewModelProvider with the same question.
The error is happening because the android-lifecycle library is now a kotlin multiplatform project and the gradle dependency resolution picks the jvm variant instead of the android one. This only happens to projects which do not apply the kotlin-android gradle plugin!
The problem is the "org.jetbrains.kotlin.platform.type" variant attribute is not requested and so gradle picks the jvm lifecycle-viewmodel variant. When you add the kotlin-android plugin the attribute is automatically set to "androidJvm". For more information about gradle variant attributes see https://docs.gradle.org/current/userguide/variant_attributes.html
You can debug this with the following command:
gradlew -q app:dependencyInsight --dependency lifecycle-viewmodel --configuration debugAndroidCompileClasspath
This is a bug and should be fixed by Google. Probably by setting the "org.gradle.jvm.environment" variant to "android" A workaround could look like this but i am not a Gradle expert and there should be a better solution.
implementation("androidx.lifecycle:lifecycle-viewmodel")
constraints {
def platformTypeArgument = Attribute.of("org.jetbrains.kotlin.platform.type", String)
implementation("androidx.lifecycle:lifecycle-viewmodel:2.8.1") {
because "The wrong dependency is picked"
attributes {
attribute(platformTypeArgument, "androidJvm")
}
}
implementation("androidx.lifecycle:lifecycle-runtime:2.8.1") {
because "The wrong dependency is picked"
attributes {
attribute(platformTypeArgument, "androidJvm")
}
}
}
UPDATE:
I found the following error for compose https://issuetracker.google.com/issues/328687152 which is basically the same problem. Just upgrade the Android Gradle Plugin to version 8.4.0 and the problem is solved.