gpt4 book ai didi

android - test 和 androidTest 之间共享 srcDirs,升级到 Android Studio Chipmunk (IntelliJ 2021.2.1) 后 Unresolved 引用

转载 作者:行者123 更新时间:2023-12-04 23:36:30 24 4
gpt4 key购买 nike

我刚刚从 Bumblebee 升级到 Chipmunk,并且在我的仪器化 androidTests 中遇到了多个依赖关系解析问题。
这些是我的源集的样子:

sourceSets {
test.java.srcDirs += 'src/testShared/kotlin'
test.resources.srcDirs += 'src/testShared/resources'
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
androidTest.java.srcDirs += 'src/testShared/kotlin'
androidTest.resources.srcDirs += 'src/testShared/resources'
}
这个想法是在单元测试和仪器测试之间共享测试数据。请记住,测试可以构建和执行,但依赖项在 IDE 中显示为红色。此外,它可能不相关,但我引用的字符串资源(位于资源模块中)在 IDE 中也是红色的。
此外,当我注释掉指向 testShared 的单元测试源集时,IDE 错误在我的 AndroidTests 中消失
这在以前版本的 Android Studio 中不是问题。我认为这个问题与基础 IntelliJ IDE 平台有关。有没有人有任何想法如何解决这个问题,或者有任何解决方法?
编辑:这是一个在 Chipmunk 及更高版本中运行时演示该问题的基本示例项目。在 bumblebee 中,androidTest 中没有 Unresolved 引用错误。此外,您可能需要调整 AGP 版本,因为我使用 Dolphin beta01 构建了这个项目,但在 Chipmunk 中的问题是相同的
https://drive.google.com/file/d/1ZCcEwuqM-m4E5qe94vCn52yWMON8rd5P/view?usp=sharing

最佳答案

Android Studio IDE 和 IntelliJ 平台的新迭代似乎不再支持此功能。
编辑:以这种方式共享代码不再有效。但是,还有另一种方法可以使它起作用:
基本上,创建一个 android 库(sharedTestCode),通过 testImplementation 和 androidTestImplementation 在您的应用程序中依赖它。在 sharedTestCode build.gradle 文件中,取决于应用程序。您现在应该能够创建共享测试数据并在两种类型的测试中引用它们。
这是一个使用此设置的示例项目:
https://drive.google.com/file/d/1I2CZhTxHGRgCN9UCEjIuWUfFnGxTF_Cv/view?usp=sharing
第二次编辑 : 确保任何依赖于 :app 项目的模块 还定义了 productFlavors 和 flavorDimensions,匹配
应用程序的 build.gradle 文件的 build.gradle 配置。

例如,我必须将此代码添加到我的 sharedTestModule 的 build.gradle 文件中:

flavorDimensions "environment"

productFlavors {
local {
dimension "environment"
}
gae {
dimension "environment"
}
}
上一个回复:
我创建了一个支持线程:
https://issuetracker.google.com/issues/232420188
这是谷歌关于这种引用测试数据的旧方式的答案:

Sharing code in this way is not longer supported in the IDE. Thereason this was working before is that in pre-chipmunk we ran Androidstudio import with the intellij options set to create a single moduleper Gradle project. This option has been removed from IDEA for a goodnumber of years. Importing this way comes with a lot of issues due tothe fact that intellij modules can only be set up with two scopes fordependencies and other information, these are compile and test. Bydefault Android modules effectively have three different scopes (whichroughly correspond to Gradle source sets per variant) main, unitTestand androidTest. The mapping of these to intellij modules with thisoption require us to merge the dependencies of unitTest andandroidTest. Also any custom source set's need to be merged as well.This caused a lot of incorrect symbol resolution within the editorsalong with a host of other issues.

In chipmunk we switch to creating a module per (roughly) Gradle sourceset. This allows us to map the information correctly but unfortunatelydoes result in sharing information between source sets such as thisbecoming unsupported by the IDE.

To summarize, in order to be correct each source file must only bepresent in one module otherwise the IDE has no way of knowing whichcontext to use. This can also sometimes result in subtle issues withbuild. To share sources between test modules you should be able to useput the code in a separate project and consume it as a dependency withboth testImplementation and androidTestImplementation.

关于android - test 和 androidTest 之间共享 srcDirs,升级到 Android Studio Chipmunk (IntelliJ 2021.2.1) 后 Unresolved 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72218645/

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