android - 使用来自 kotlinx-coroutine-test 的类时出错

标签 android kotlin kotlin-coroutines

似乎 kotlinx-coroutines-test 依赖项对我不起作用,因为我无法访问依赖项的成员,例如 TestCoroutineDispatcher , setMain() , resetMain()等等。我在关注这个 doc但尽管添加了 gradle 依赖项,但无法访问成员。我尝试重建项目并使缓存无效并重新启动,但似乎没有任何效果。我也试过做androidExtensions {experimental = true}但仍然没有运气。

构建.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.android.kotlincoroutines"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
androidExtensions {
    experimental = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'


    implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
    implementation 'com.google.android.material:material:1.1.0-alpha03'

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // ViewModel and LiveData
    def lifecycle_version = '2.0.0-beta01'
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha02"
    testImplementation "androidx.arch.core:core-testing:$lifecycle_version"

    // Room for database
    def room_version = '2.0.0'
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    def work_version = "1.0.0-rc01"
    implementation "android.arch.work:work-runtime:$work_version"

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation "com.google.truth:truth:0.42"
    androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"

    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-M1'
}

我在哪里错了?

最佳答案

将 testImplementation 替换为 implementation 对我有用。

关于android - 使用来自 kotlinx-coroutine-test 的类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56738023/

相关文章:

kotlin-coroutines - koltin,直接使用CoroutineScope和从CoroutineScope派生类有什么区别

android - 如何使用 Kotlin Coroutines 正确地拥有待处理操作队列?

android - 在Android中将图像裁剪为给定的正方形大小?

android - 将 Twilio Video 和 Twilio Chat 合而为一

Java/Android 正则表达式允许可能包含撇号和/或连字符的名称

java - '签名:Array <(out)签名!>!'不推荐使用。 Java中不推荐使用

android - Kotlin 流 : unsubscribe from SharedFlow when Fragment becomes invisible

android - 冷启动直接弹出状态

kotlin - 使用 kotlin 每 3 位添加逗号或点

android - 通过 `Dispatchers.IO` 启动协程时出现 NetworkOnMainThreadException