简体   繁体   English

Android Studio Chipmunk 自动创建模块后无法解析测试中的生产代码

[英]Cannot resolve production code in tests after Android Studio Chipmunk automatically created modules

I'm currently using Android Studio to develop a multi-module project by test-driven-development methods.我目前正在使用 Android Studio 通过测试驱动开发方法开发多模块项目。 It went well until I updated my IDE to Android Studio Chipmunk from previous Bumblebee.一切顺利,直到我将我的 IDE 从以前的 Bumblebee 更新为 Android Studio Chipmunk。 When I opened the project after the update, it seemed that AS automatically generated some modules for me.更新后打开工程,好像AS自动帮我生成了一些模块。 As you can see in the pic, those new names such as [main] and [unitTest] appeared in the project view automatically.正如您在图片中看到的,那些新名称(例如 [main] 和 [unitTest])自动出现在项目视图中。 modules generated automatically模块自动生成

After this, I found that the classes and methods of my production code could not be resolved in my test code.在此之后,我发现我的生产代码的类和方法无法在我的测试代码中解析。 AS told me after I pressed Alt+Enter that I should add dependency to the main module of my production code.在我按下 Alt+Enter 后,AS 告诉我应该向生产代码的主模块添加依赖项。 And it added one line in the build.gradle file: "testImplementation project(path: ':app')".并且它在 build.gradle 文件中添加了一行:“testImplementation project(path: ':app')”。 After syncing the gradle build, nothing had changed and it still could not resolve methods in my production code.同步 gradle 构建后,没有任何变化,它仍然无法解析我的生产代码中的方法。 I can still run the tests by executing gradle test tasks, but it is weird that I cannot use production code in my test code directly.我仍然可以通过执行 gradle 测试任务来运行测试,但很奇怪我不能直接在我的测试代码中使用生产代码。

UPDATE:更新:

For further information, I think this is a problem caused by the update from Intellij 2021.1 to 2021.2, since I re-installed AS Bumblebee and all seems worked fine.有关更多信息,我认为这是由 Intellij 2021.1 更新到 2021.2 引起的问题,因为我重新安装了 AS Bumblebee,一切似乎都正常。 I also found this issue in JetBrains YouTrack that has an exactly same problem with me.我还在 JetBrains YouTrack 中发现了这个问题,它与我有完全相同的问题。 Here is the link: https://youtrack.jetbrains.com/issue/IDEA-275566/Class-not-found-after-intellij-upgrade-2021-1-2-2021-2这是链接: https://youtrack.jetbrains.com/issue/IDEA-275566/Class-not-found-after-intellij-upgrade-2021-1-2-2021-2

This issue remains open and I've tried some other developers' reply in this issue to delete the.iml files, but the problem remains.这个问题仍然悬而未决,我已经尝试了一些其他开发者在这个问题上的回复来删除.iml 文件,但问题仍然存在。 I think Intellij 2021.1 and 2021.2 generate different.iml file in the.idea folder.我认为 Intellij 2021.1 和 2021.2 在 .idea 文件夹中生成了不同的 .iml 文件。

I was having the same issues in the first Android Studio Chipmunk release - all of my unit test source code was peppered with " Cannot resolve symbol " errors, but the code would still compile and run, and the tests would pass.我在第一个 Android Studio Chipmunk 版本中遇到了同样的问题——我所有的单元测试源代码都充满了“无法解析符号”错误,但代码仍然可以编译和运行,并且测试会通过。

I found that I could temporarily workaround (ie, "fix") the issue by adding a line to the build.gradle file like:我发现我可以通过向build.gradle文件添加一行来临时解决(即“修复”)问题,例如:

testImplementation project(path: ':shared:library:module')

But today I upgraded to the new Chipmunk release, with details:但今天我升级到新的 Chipmunk 版本,详细信息:

Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 18, 2022
Runtime version: 11.0.12+0-b1504.28-7817840 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.

And the issue no longer occurs.并且不再出现该问题。 I'm guessing the folks working on the IntelliJ platform found and patched the issue.我猜在 IntelliJ 平台上工作的人发现并修补了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM