gpt4 book ai didi

java - 来自 Gradle : "package android.test does not exist" 的 Android 中的 JUnit 测试

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:33 68 4
gpt4 key购买 nike

我不确定如何让我的单元测试通过 Gradle 运行。我创建了一个 unitTest 任务,设置了适当的源集和适当的依赖项,但出现错误:

package android.test does not exist

我按照另一篇文章中的说明将以下内容添加到我的 build.gradle 中:

// Unit tests
sourceSets {
unitTest {
java.srcDir file('test')
resources.srcDir file('test/res')
}
}

dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'com.google.android:android:4.0.1.2'
}

configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}

下面是完整的 build.gradle:

buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}

configurations {
apt
}

apply plugin: 'android'

dependencies {
compile project(':PullToRefresh')
compile project(':simple-crop-image-lib')
compile project(':facebook')
compile project(':gridlayout')
compile project(':nineoldandroids')
compile project(':ViewPagerIndicator')
compile project(':volley')
compile fileTree(dir: 'libs', include: '*.jar')
apt files('compile-libs/androidannotations-2.7.1.jar')
}

android {
buildToolsVersion '17.0'
compileSdkVersion 17

signingConfigs {
debug {
storeFile file('keystores/debug.keystore')
}
}

buildTypes {
debug {}

ipconfigurable {
debuggable true
signingConfig signingConfigs.debug
}
}

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}

// Unit tests
sourceSets {
unitTest {
java.srcDir file('src/test')
resources.srcDir file('src/test/res')
}
}

dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'com.google.android:android:4.0.1.2'
}

configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}

目录结构:

enter image description here

最佳答案

感谢@PerrynFowler,解决方案是将以下行添加到我的 unitTestCompile 依赖项中:

unitTestCompile 'com.google.android:android-test:4.1.1.4'

关于java - 来自 Gradle : "package android.test does not exist" 的 Android 中的 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21866449/

68 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com