Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Android warns that ViewModelProviders is deprecated

ErrorViewModelProviders is deprecated

Short 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.

  • Applies to Android Gradle Plugin 8.x, Lifecycle 2.x, Kotlin 2.x, activity-ktx
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 .
  2. 2Match what you see, then jump to its fix

    1. 1The compiler strikes through ViewModelProviders.of(...) or reports it deprecatedCode still calls ViewModelProviders.ofFix
    2. 2build.gradle or the version catalog lists androidx.lifecycle:lifecycle-extensionsThe old lifecycle-extensions dependency is still in the buildFix
    3. 3The ViewModel class has constructor parameters such as a repositoryThe ViewModel takes constructor parametersFix

What does the error look like?

  • ViewModelProviders is deprecated
  • This 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/src

2Fix

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:assembleDebug

Confirmed by developer.android.com

Did this fix it?

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-extensions

Confirmed by developer.android.com

Did this fix it?

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/src

2Fix

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:installDebug

Confirmed by developer.android.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.