Status Update
Comments
ku...@google.com <ku...@google.com> #2
Thanks for the feedback. To help us troubleshoot this issue further, we will need more information. Please share detailed reproduction steps and as much as possible of the following information when it applies if you haven’t already:
- Android Studio version and OS type and version
- Screenshots or videos of the issue
- idea.log file (select Help > Show Log in Finder / Explorer)
- Thread dumps if the issue is a freezing or non-responding UI.
For more information on what’s needed and how to obtain this information please read the guide at
se...@gmail.com <se...@gmail.com> #3
So I made a sample project that demonstrates the issue. You can download it here:
If you load the project into Android Studio Chipmunk and above, you will see that the reference to the shared reference in the ExampleIntegrationTest is unresolved. If you comment out the srcDirs line that exposes the shared resource to the unit test, then the androidTest finds the reference. 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
Please note that in Bumblebee, both test files don't have an issue sharing the resource.
One last thing, sharing test resources is important to avoid duplicate code. Please investigate, you should have enough here to debug the issue.
se...@gmail.com <se...@gmail.com> #4
Any news on this? My StackOverflow question already has 6 upvotes. Looks like this is affecting a decent amount of people (basically anyone with shared resources between unit and instrumented tests).
Here is my StackOverflow post:
sm...@google.com <sm...@google.com>
sm...@google.com <sm...@google.com> #5
(slightly adapted from the comments on 232007221)
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
.
se...@gmail.com <se...@gmail.com> #6
Thank you for the detailed response.
I also tried the solution you provided at the end of your explanation, but at least for me it didn't work, because my sharedDir test directory code depends on inner app dependencies. I tried creating a test-specific module that depended on my app's module and adding this module as my app's test dependencies (testImplementation & androidTestImplementation) but again I had issues with unresolved references.
ba...@gmail.com <ba...@gmail.com> #7
I've the same problem as mentioned.
But there is something I don't understand.
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.
I try to open my project in IntelliJ IDEA 2021.2.4 (Community Edition) I don't have any problem. If this way of Sharing code is no longer supported and removed from IDEA for a good number of years shouldn't I get the same errors as Android Studio?
so...@google.com <so...@google.com> #8
If it is a Java/Kotlin module and you share between main
and test
it pretends to work by silently ignoring your directory in test
source set and it usually works because it depend son main
. Try adding more source sets.
If it is an Android module, IDEA bundles some old Android plugin which works the old way for Android regardless of how your project is configured.
sm...@google.com <sm...@google.com> #9
Just to clarify, the option has been removed from IDEA in that you can no longer toggle it on and off. The option is still forced on if any Android Gradle project is present as described in #8.
For #6, you should be able to have the following structure of :app
<--(implementation) :shared
(com.android.library/test) <--((androidT/t)estImplementation) :app
. If you are still getting issues with this there may be another problem, which dependency has symbols that aren't been resolved? It might also be related to
se...@gmail.com <se...@gmail.com> #10
^^^responding to #9^^^
I will try this again, maybe I made a mistake in my configuration. Thanks for the help
se...@gmail.com <se...@gmail.com> #11
So I tried referencing my app from an android module. Unfortunately, it does not look like you can reference an app as a dependency. I could not use any of the app's inner dependencies.
The solution I came up with is not as nice, but here it is:
- Place your shared test data in the production code
- Annotate all files/objects with @VisibleForTesting
Your production code will show a warning if you try referencing the test data, and R8 should remove any code that isn't referenced in production code.
sm...@google.com <sm...@google.com> #12
I believe you should be able to add a dependency on the app in another library or test module. See attached project for a simple example. Unfortunately this isn't perfect as current Android studio does not class sources in test modules as test sources. So you might not get symbols showing up when you search test sources only in the IDE.
You will need to switch the version of the Android Gradle plugin used in the attached project to 7.2.0
, apologies for this.
ba...@gmail.com <ba...@gmail.com> #13
Does seem to work with productFlavors...
I've created a com.android.test
module with same productFlavors as the app
since it was not compiling and the in the app added dependency:
testImplementation project(":sharedTestCode")
androidTestImplementation project(":sharedTestCode")
But none of the code in sharedTestCode
is recognized in test
and androidTest
se...@gmail.com <se...@gmail.com> #14
I also tried the supplied project example, but as mentioned in #13, the unit tests and integration tests cannot see the shared test class (SharedTestStringClass)
(Keep in mind I am still on Bumblebee testing this)
sm...@google.com <sm...@google.com> #15
Thanks for trying, can you see if changing the sharedTestCode project from a com.android.test
to a com.android.library
and removing the testProjectPath
property from the android { ... }
configuration helps.
It appears like the dependencies on test modules don't work in version 7.2.0 of the Android Gradle plugin.
se...@gmail.com <se...@gmail.com> #16
#15
It works this way! Tested on Bumblebee and Dolphin beta01. Not sure the exact difference with what I tried before, but now I know it works.
Thanks for your help!
sa...@gmail.com <sa...@gmail.com> #17
It appears like the dependencies on test modules don't work in version 7.2.0 of the Android Gradle plugin.
I have a question regarding this. I have imported the TestAppDependency.zip
project shared in sharedTestCode
to a com.android.library
module it works fine. Does AGP doesn't support adding test
modules as dependency yet or is the issue related to a specific version of AGP?
sm...@google.com <sm...@google.com> #18
There seems to currently be an limitation with dependencies on com.android.test
modules. I believe this is present in all current versions of AGP.
sm...@google.com <sm...@google.com> #19
The current recommendation is to use a separate com.android.library
Gradle project to store any shared code required across test
and androidTest
.
Note: You will not be able to access things marked internal
which would have been accessible before.
I have attached a slightly modified version of the project in #12 which shows the suggested setup for this.
se...@gmail.com <se...@gmail.com> #20
It is also important to mention that the sharedTest module that depends on the :app project MUST define the same flavourDimensions & product flavours as the :app, otherwise it won’t work correctly.
pm...@gmail.com <pm...@gmail.com> #21
androidTest
-----------
Compile configuration: androidTestCompile
build.gradle name: android.sourceSets.androidTest
Java sources: [app/src/androidTest/java]
Any idea why androidTest is not anymore overriding the code from main source set?
sh...@gmail.com <sh...@gmail.com> #22
I've checked the attached project. our situation is the shared code is in test folder instead of in the main folder in sharedTestCode module. Could you help provide suggestions?
ar...@gmail.com <ar...@gmail.com> #23
ut...@gmail.com <ut...@gmail.com> #24
Re #23, you can check RobolectricTestRunner
or AndroidJUnit4ClassRunner
to run tests based on environment. And Robolectric supports Espresso API from 4.x release, so we can run many Espresso tests on Robolectric environment. The sharedTest pattern is a very often used pattern to share UI related test code.
Description
Build: AI-212.5712.43.2112.8512546, 202204281627,
AI-212.5712.43.2112.8512546, JRE 11.0.12+0-b1504.28-7817840x64 JetBrains s.r.o., OS Mac OS X(aarch64) v12.3.1, screens 3456.0x2234.0, 6016.0x3384.0, 2880.0x5120.0; Retina
AS: Chipmunk | 2021.2.1; Kotlin plugin: 212-1.6.21-release-334-AS5457.46; Android Gradle Plugin: 7.2.0; Gradle: 7.4.2; Gradle JDK: Azul Zulu Community™ version 11.0.14; NDK: from local.properties: (not specified), latest from SDK: 22.1.7171670; LLDB: pinned revision 3.1 not found, latest from SDK: (package not found); CMake: from local.properties: (not specified), latest from SDK: 3.18.1-g262b901, from PATH: (not found)
IMPORTANT: Please readhttps://developer.android.com/studio/report-bugs.html carefully and supply all required information.
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:
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 separate 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 disappear 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.