Android Studio showing unresolved reference in shared test directory, but executing tasks works as expected

Viewed 486

I have a multi-module app in which test source files are shared between modules. For each module that accesses the shared files, we add the following to our build.gradle

sourceSets {
    String testDir = '../shared/src/test/java'
    test {
        java.srcDir testDir
    }
}

and add the shared module as a dependency using implementation project(path: ':shared')

When running tests using gradle everything works correctly, but when viewing the source files Android Studio is incorrectly showing Unresolved Reference errors.

Oddly, one of our modules named "feature" doesn't exhibit this behaviour. All the other modules do though, despite them having identical build.gradle files.

While investigating the issue I have noticed that when in the Project view the test directory in the shared module is labelled [feature] as if it belongs to the feature module.

Furthermore, I have found the following in the Event Log:

Duplicate content roots detected: Path [/project/shared/src/test/java] of module [project.feature was removed from modules [project.app, project.network project.wrapper]

It's as if Android Studio is deliberately ignoring the sources.

I have tried all combinations of resetting cache, deleting .idea and .gradle directories, cleaning etc. I have tried deleting and recreating the modules, and I can't find anything that's causing the issue.

All team members are having the same issue.

I can also see that the following appears in the .idea/modules/feature/project.feature.iml file, but not in any of the .iml files for the other modules.

<content url="file://$MODULE_DIR$/../../../shared/src/test/java">
    <sourceFolder url="file://$MODULE_DIR$/../../../shared/src/test/java" isTestSource="true" />
</content>
1 Answers

It is a limitation of the IDE: IDEA-214527 Two modules in a project cannot share the same content root.

If you have a code which is common to multiply modules, it is better to extract it into a separate Gradle module and use it as a dependency.

Related