'Shared srcDirs between test and androidTest, unresolved references after upgrade to Android Studio Chipmunk (IntelliJ 2021.2.1)

I just upgraded from Bumblebee to Chipmunk, and I am having multiple dependency-resolution issues in my instrumented androidTests.

These are what my source sets look like:

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'
}

The idea is to share test data between unit tests and instrumented tests. Keep in mind, the tests can build and execute, but the dependencies show up as red in the IDE. Also, it may not be related, but string resources I reference (that are in a resource module) are also red in the IDE.

Also, when I comment out either the unit test sourceSets that point to testShared, the IDE errors dissapear in my AndroidTests

This was not an issue in the previous version of Android Studio. I think the issue is related to the base IntelliJ IDE platform. Does anyone have any ideas how to fix this issue, or have any workarounds?

Edit: Here is a basic sample project that demonstrates the issue, when running in Chipmunk and above. In bumblebee, there are no unresolved reference errors in androidTest. Also, you may have to tweak the AGP version, as I built this project using Dolphin beta01, but the issue is the same in Chipmunk https://drive.google.com/file/d/1ZCcEwuqM-m4E5qe94vCn52yWMON8rd5P/view?usp=sharing



Solution 1:[1]

Looks like this feature is no longer supported in new iterations of the Android Studio IDE and IntelliJ platforms.

Edit: Sharing code this way no longer works. BUT, there is another method to make it work:

Basically, create an android library (sharedTestCode), depend on it in your app via testImplementation and androidTestImplementation. In the sharedTestCode build.gradle file, depend on the app. You should now be able to create shared test data and reference them in both types of tests.

Here is a sample project with this setup working:

https://drive.google.com/file/d/1I2CZhTxHGRgCN9UCEjIuWUfFnGxTF_Cv/view?usp=sharing

Previous response:

I created a support thread: https://issuetracker.google.com/issues/232420188

and here is the answer from Google about this old way of referencing test data:

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

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1