gpt4 book ai didi

java - 安卓工作室 : Espresso dependencies in gradle

转载 作者:行者123 更新时间:2023-11-30 00:25:50 25 4
gpt4 key购买 nike

我的 Android 项目中有以下 Gradle 配置:

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
apply plugin: 'com.android.application'

dependencies {
repositories {
jcenter()
google()
}
compile fileTree(include: '*.jar', dir: 'libs')
compile 'com.android.support:support-v4:25.4.0'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'

}

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"

buildTypes {
debug {
debuggable true
applicationIdSuffix ".debug"
}
}

defaultConfig {
jackOptions {
enabled true
}
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

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

instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}

我想开始进行 UI 测试,所以我通过 Android Studio 的录制功能录制了一个 Espresso 测试。之后,在保存测试类文件时,我注意到一些导入没有解析。

Error:(17, 8) The import org.junit cannot be resolved
Error:(19, 15) The import android.support.test cannot be resolved

如你所见,我直接添加了junit依赖,为什么android studio或gradle看不到junit库呢?

这是我的部门树:

androidTestCompile - Classpath for compiling the androidTest sources.
+--- junit:junit:4.12
| \--- org.hamcrest:hamcrest-core:1.3
\--- com.android.support.test.espresso:espresso-core:3.0.0
+--- com.android.support.test:runner:1.0.0
| +--- com.android.support:support-annotations:25.4.0
| +--- junit:junit:4.12 (*)
| \--- net.sf.kxml:kxml2:2.3.0
+--- com.android.support.test:rules:1.0.0
| \--- com.android.support.test:runner:1.0.0 (*)
+--- com.android.support.test.espresso:espresso-idling-resource:3.0.0
+--- com.squareup:javawriter:2.1.1
+--- javax.inject:javax.inject:1
+--- org.hamcrest:hamcrest-library:1.3
| \--- org.hamcrest:hamcrest-core:1.3
+--- org.hamcrest:hamcrest-integration:1.3
| \--- org.hamcrest:hamcrest-library:1.3 (*)
\--- com.google.code.findbugs:jsr305:2.0.1

我的源文件夹是这样组织的:

└── src
├── androidTest
│   └── java
│   └── pt
│   └── joaomneto
│   └── titancompanion
│ └── (expresso tests source code)
└── pt
└── joaomneto
└── titancompanion
└── (app source code)

谢谢你的帮助

最佳答案

正如我发现的那样,问题不在于依赖关系树,而在于我使用的文件夹结构。将文件夹结构更改为更标准的 src/main/java 和 src/androidTest/java 并相应地更改 gradle.build 之后

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

androidTest.java.srcDir 'src/androidTest/java'
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}

Android studio 现在能够在正确的位置看到测试文件,并将良好的依赖关系树范围应用于它们。

关于java - 安卓工作室 : Espresso dependencies in gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45393341/

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