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
2
Answers
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
Second edit: Make sure that any module that depends on the :app project also defines productFlavors and flavorDimensions, matching the build.gradle configuration of the app's build.gradle file.
So for example, I had to add this code to my sharedTestModule's build.gradle file:
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:
To solve this, move your shared test dependencies into a separate module. Here’s how:
shared-test
Move your shared code inside the
:shared-test
module.Now, in your app’s
build.gradle
file add a dependency on the:shared-test
module for the unit and instrumented test build configurations:If your shared test code is dependent on code in your app (for example, you have Hilt modules which reference app interfaces), add a dependency in your shared-test module’s
build.gradle
file on the app module:Example on GitHub here: https://github.com/android/architecture-samples