0

Android Studio (4.1.3) 似乎一直在添加声明为compileOnly执行类路径的一部分的依赖项,用于运行单元测试(不是检测测试)。

我在一个示例项目中看到了这一点,只是让 Android Studio 创建了一个新的“基本活动”项目,然后添加任何compileOnly依赖项,运行示例单元测试,并检查 IDE 使用的命令行。依赖项显示在该命令行中。

这对我来说搞砸了一些在这里并不重要的原因,我们只是说我将某些依赖项声明为“仅编译”的原因,我真的不希望它们在我尝试运行时出现。

那么,问题 #1 - 这是 Android Studio 中的错误吗?

我想我可以理解它是否不是(错误)。原因可能是-“好吧,您没有在实际设备上运行此代码,因此没有任何东西可以提供Android平台将提供的任何类”。这在某些情况下可能很有用,但恕我直言,应该让想要类似东西的人声明与 相同的依赖项testImplementation,并获得该行为。就我而言,我想将其关闭,但不知道如何。另一方面,直接从 Gradle 运行单元测试不使用该类路径,Android Studio 可能应该以相同的方式工作。

所以,问题 #2 - 我可以让 Android Studio 不这样做吗?如果是的话 - 怎么做?

样品build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "org.vps.tests.cotest"
        minSdkVersion 28
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.3.3'
    implementation 'androidx.navigation:navigation-ui:2.3.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    compileOnly 'com.github.houbb:deep-copy-test:0.0.1' // random pick

}
4

0 回答 0