Could not get unknown property 'lifecycle_version' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

Issue

I want to create a ViewModel for my project,I also added the dependencies but it shows this error, how can I fix it?Also I am using the latest version of Android Studio and Java

Could not get unknown property ‘lifecycle_version’ for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

 dependencies {
    //ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}```

Solution

You have to define your lifecycle_version.

dependencies {
        //Define your version here
        def lifecycle_version = "2.5.1"

        implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
        annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    //Other Dependencies

}

Check here: https://developer.android.com/jetpack/androidx/releases/lifecycle#groovy

Answered By – Nilupul Sandeepa

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published