Android warns that ViewModelProviders is deprecated
ViewModelProviders is deprecatedShort answer
ViewModelProviders belongs to the lifecycle-extensions artifact and has been deprecated since Lifecycle 2.2.0. Replace ViewModelProviders.of(this) with the by viewModels() delegate in Kotlin, or new ViewModelProvider(this) in Java. A ViewModel with constructor parameters needs a ViewModelProvider.Factory. Drop lifecycle-extensions.
Fix it fast
1Find out which cause you have
Find every use of the old class and the old dependency.
grep -rn "ViewModelProviders" app/src grep -rn "lifecycle-extensions" --include=*.gradle --include=*.kts --include=*.toml .2Match what you see, then jump to its fix
- 1
The compiler strikes through ViewModelProviders.of(...) or reports it deprecatedCode still calls ViewModelProviders.ofFix - 2
build.gradle or the version catalog lists androidx.lifecycle:lifecycle-extensionsThe old lifecycle-extensions dependency is still in the buildFix - 3
The ViewModel class has constructor parameters such as a repositoryThe ViewModel takes constructor parametersFix
- 1
What does the error look like?
ViewModelProviders is deprecatedThis class is deprecated. Use the constructors for ViewModelProvider directly.
Cause 1 of 3
Code still calls ViewModelProviders.of
1Check
The API reference marks the of(...) methods deprecated since 2.2.0 and points to the by viewModels() delegate or to ViewModelProvider with the activity or fragment passed in.
grep -rn "ViewModelProviders.of" app/src2Fix
In Kotlin activities use the delegate from the activity-ktx artifact. In Java create a ViewModelProvider and call get.
// Kotlin activity
import androidx.activity.viewModels
class DiceRollActivity : AppCompatActivity() {
private val viewModel: DiceRollViewModel by viewModels()
}
// Java activity
DiceRollViewModel model = new ViewModelProvider(this).get(DiceRollViewModel.class);3Verify
Rebuild: the deprecation warning on that line is gone and rotating the screen keeps the same ViewModel.
./gradlew :app:assembleDebugConfirmed by developer.android.com
Cause 2 of 3
The old lifecycle-extensions dependency is still in the build
1Check
The ViewModelProviders reference lists lifecycle-extensions as its artifact, and the Lifecycle release notes say its APIs are deprecated in favour of the specific Lifecycle artifacts.
grep -rn "lifecycle-extensions" --include=*.gradle --include=*.kts --include=*.toml .2Fix
Remove it and declare only the artifacts you use, with the coordinates from the Lifecycle release notes (the Kotlin delegate comes from activity-ktx).
dependencies {
// remove: implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
// add the artifacts you use, with versions from the Lifecycle release notes
}3Verify
A clean build finishes with no reference to lifecycle-extensions.
./gradlew :app:dependencies --configuration debugRuntimeClasspath | grep lifecycle-extensionsConfirmed by developer.android.com
Cause 3 of 3
The ViewModel takes constructor parameters
1Check
The framework creates ViewModels, so one with dependencies needs a ViewModelProvider.Factory. Without a factory the default creation path cannot supply those arguments. A ViewModel that takes nothing, or only a SavedStateHandle, needs no factory.
grep -rn "class .*ViewModel(" app/src2Fix
Define a factory with viewModelFactory and initializer, and pass it to the delegate or to ViewModelProvider.
class MyViewModel(private val repo: MyRepository) : ViewModel() {
companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
initializer {
val app = this[APPLICATION_KEY] as MyApplication
MyViewModel(app.myRepository)
}
}
}
}
// in the activity
private val viewModel: MyViewModel by viewModels { MyViewModel.Factory }3Verify
Launch the screen and rotate it: no crash, and the state survives.
./gradlew :app:installDebugConfirmed by developer.android.com
How do I stop it coming back?
- Read deprecation notes in the API reference before copying older sample code.
- Declare only the Lifecycle artifacts the module imports.
- Keep ViewModel factories in the ViewModel file so they are easy to find.
- Treat new deprecation warnings as to-do items, not noise.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
Android build fails because a product flavor has no flavor dimension
The flavor 'flavor_name' is not assigned to a flavor dimension.
Android Gradle plugin refuses to build when a product flavor is not assigned to a flavor dimension.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android build fails because two libraries ship the same META-INF file
More than one file was found with OS independent path
Two dependencies put a file at the same path inside the APK and the plugin will not guess which one wins.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix