未解决的参考:viewModelScope

时间:2019-08-03 04:11:26

标签: android kotlin android-jetpack

我正在尝试在我的android项目android studio中使用viewModelScope.launch(Dispatchers.IO)显示错误“未解决的引用:viewModelScope”,我从官方站点复制了该依赖项。我还尝试了迁移到android x。

我添加了来自官方网站的最新依赖项,还尝试了Invalide缓存并重新启动,但没有用。

我的模块级别gradle文件依赖性

 dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.0.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

        def room_version = "2.2.0-alpha01" // 2.1.0 for latest stable version

        implementation "androidx.room:room-runtime:$room_version"
        annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor

        // optional - Kotlin Extensions and Coroutines support for Room
        implementation "androidx.room:room-ktx:$room_version"

        // optional - RxJava support for Room
        implementation "androidx.room:room-rxjava2:$room_version"

        // optional - Guava support for Room, including Optional and ListenableFuture
        implementation "androidx.room:room-guava:$room_version"

        // Test helpers
        testImplementation "androidx.room:room-testing:$room_version"



        def lifecycle_version = "2.0.0"

        // ViewModel and LiveData
        implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
        // alternatively - just ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
        // alternatively - just LiveData
        implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
        // alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
        //     AndroidX libraries use this lightweight import for Lifecycle
        implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

        annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
        // alternately - if using Java8, use the following instead of lifecycle-compiler
        implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

        // optional - ReactiveStreams support for LiveData
        implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx

        // optional - Test helpers for LiveData
        testImplementation "androidx.arch.core:core-testing:$lifecycle_version"




    }

2 个答案:

答案 0 :(得分:1)

根据Lifecycle Release notes,包含lifecycle-viewmodel-ktx的{​​{1}}(与ViewModel相关的Kotlin扩展的正确工件)的第一个版本是viewModelScope。因此,您应该升级到最新版本:2.1.0-alpha012.1.0-rc01,它们都包含该API。

答案 1 :(得分:0)

ViewModel KTX的正确依存关系应为androidx.lifecycle:lifecycle-viewmodel-ktx:$version