【问题标题】:Unresolved Reference: mockwebserver未解决的参考:mockwebserver
【发布时间】:2021-12-08 00:01:37
【问题描述】:

我正在用mockwebserver 编写一个测试套件。我从官方 github 页面输入了所有依赖项。每次我尝试运行测试时,我都会收到Unresolved reference: mockwebserver

没有导入选项。进口上的字母不是红色的。 Android Studio 不提示任何内容。除了与未解决的引用相关的错误之外,没有其他错误。

截图:


build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id("org.jetbrains.kotlin.android.extensions")
}

android {
    compileSdk 31

    useLibrary("android.test.runner")
    useLibrary("android.test.base")
    useLibrary("android.test.mock")

    defaultConfig {
        applicationId "com.example.moviespotter"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    buildFeatures {
        viewBinding = true
        compose true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.5.21'
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation 'androidx.test.espresso:espresso-idling-resource:3.1.1'
    testImplementation 'junit:junit:4.13'
    testImplementation 'com.squareup.okhttp3:mockwebserver3:5.0.0-alpha.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'com.squareup.retrofit2:retrofit:2.7.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.0'
    implementation 'com.squareup.okhttp3:okhttp:4.3.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.github.bumptech.glide:glide:4.10.0' //Glide
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    implementation "io.coil-kt:coil-compose:1.3.1"
    annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
    androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'
}

【问题讨论】:

    标签: android-studio junit4 okhttp mockwebserver


    【解决方案1】:

    既然这是在androidTest中,应该是

    androidTestImplementation 'com.squareup.okhttp3:mockwebserver3:5.0.0-alpha.2'
    

    同时混合 5.0.0-alpha.2 和 4.3.1 也是一个痛苦的世界。

    【讨论】:

    • 您会推荐哪些版本?我一直在尝试不同的组合,直到找到一个有效的组合。
    • 一切都使用 4.9.2,它是最新的稳定版本。
    【解决方案2】:

    我找到了罪魁祸首。

    将依赖项添加到 build.gradle 文件时,它应为 implementation

    更改自: testImplementation 'com.squareup.okhttp3:mockwebserver3:5.0.0-alpha.2'

    到: implementation 'com.squareup.okhttp3:mockwebserver3:5.0.0-alpha.2'

    【讨论】:

    • 实现太高了,它通过使其成为 src/main 的依赖项来意外修复它,而您希望它仅包含在测试中。
    猜你喜欢
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2021-08-17
    • 2016-12-14
    • 2020-05-27
    • 2019-11-16
    • 2021-01-24
    • 2023-03-25
    相关资源
    最近更新 更多