16

After upgrade to Android Studio Chipmunk, my test failed because I can't access file inside shared folder that defined in build.gradle like this.

sourceSets {
    androidTest.java.srcDirs += "src/sharedTest/java"
    test.java.srcDirs += "src/sharedTest/java" }

It show warning pop up with message "Duplicate content root detected". Path [sharedTest] of module [unitTest] was removed from modules [androidTest]. Anyone can resolve this?

4 Answers4

11

According to https://issuetracker.google.com/issues/232007221 ("Duplicate content roots detected" with Android Gradle plugin 7.2.0) Google no longer supports this construct in Android Studio Chipmunk 2021.2.1.

https://issuetracker.google.com/issues/232007221#comment17 says "Source sets can no longer contain shared roots as this is impossible to represent in the IDE."

To follow the on-going discussions, subscribe to https://issuetracker.google.com/232007221 and
https://issuetracker.google.com/232420188

MikeMcC399
  • 173
  • 1
  • 10
  • Anyone have sample to use test fixtures? I have tried it here https://medium.com/easyread/sourceset-sharedtest-not-working-in-android-studio-chipmunk-use-testfixtures-as-solution-b3d150b41f08. It working well in compile time, but in runtime it say “Unresolved Reference". – Ahmad Arif Faizin Jun 06 '22 at 01:59
  • @AhmadArifFaizin Sorry about the misinformation regarding test fixtures, as this was copied from Google. In the meantime this information has been updated. See above. Also there is a related discussion in https://stackoverflow.com/questions/72218645/shared-srcdirs-between-test-and-androidtest-unresolved-references-after-upgrade – MikeMcC399 Jun 07 '22 at 14:54
  • The discussions about the best way forward are still ongoing in the two Google issues I quoted, so I removed the recommendations from the answer. Please check in the issues for the current state of the discussion. – MikeMcC399 Jun 10 '22 at 05:32
  • 1
    Check one of my questions and answers (shared srcDirs), you will find a working solution to this problem. – Sean Blahovici Aug 06 '22 at 19:51
  • @SeanBlahovici Could you give a link to "one of my questions and answers (shared srcDirs)" that you refer to? – MikeMcC399 Aug 07 '22 at 06:51
  • 1
    @MikeMcC399 sure : https://stackoverflow.com/questions/72218645/shared-srcdirs-between-test-and-androidtest-unresolved-references-after-upgrade/72377150#72377150 – Sean Blahovici Aug 07 '22 at 16:45
1

According to (@kreker thx for the hint): https://issuetracker.google.com/issues/232420188#comment19

The current recommendation is to use a separate com.android.library Gradle project to store any shared code required across test and androidTest.

But often (at least for me) it is enough just to create a separate java project, move the shared test code into this new project and create two additional testImplementation and androidTestImplementation project dependencies to the new shared project.

Step-by-step (maybe it helps) I did it as follows: 1. next to the app folder create a new folder called sharedTest (or something similar). 2. create the sub-directories sharedTest/src/main. 3. Move (or rather git mv in order not to loose the version history) the shared test code: git mv app/src/sharedTest/java sharedTest/src/main/ (and do not forget to check-in). 3. in sharedTest create a new (minimal) sharedTest/build.gradle.kts file:

plugins {
    java
}

dependencies {
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

4.Edit the settings.gradle.kts file and add the new shared project: include(":sharedTest"). 5. Edit the app/build.gradle.kts file: remove the conflicting shared source-set section android{...} and add 2 new dependencies:

dependencies {
    //Share Code between androidTest and test
    //https://stackoverflow.com/questions/72358843/sharedtest-got-warning-duplicate-content-root-detected-on-android-studio-chipm
    testImplementation(project(path = ":sharedTest"))
    androidTestImplementation(project(path = ":sharedTest"))
}
becke-ch
  • 413
  • 4
  • 8
0

https://issuetracker.google.com/issues/232420188#comment19

The current recommendation is to use a separate com.android.library Gradle project to store any shared code required across test and androidTest.

k4dima
  • 6,070
  • 5
  • 41
  • 39
0

If you are now getting this error, its probably not the fixed bug described above (in Gradle 8.2.1 anyway). Instead, you may be actually including the same source files in multiple targets, using something like:

sourceSets {
   val AndroidMain by getting {
      dependencies {
          srcDirs("src/your_common_files")
       }
  }
   val jvmMain by getting {
      dependencies {
          srcDirs("src/your_common_files")
       }
  }

Gradle does not like this extremely common code reuse technique, possibly because gradle was designed to frustrate innocent developers :-).

Instead, gradle wants you to make a separate "sourceset" and then depend on it. Almost but not quite like this:

sourceSets {
        create("commonJvm") {
            kotlin.srcDirs("src/commonJvm")
        }
        
        val androidMain by getting {
            dependencies {
                sourceSets.named("commonJvm")
            }
        }
       val jvmMain by getting {
            dependencies {
                sourceSets.named("commonJvm")
            }
        }
}

Instead, use this:

sourceSets {
        create("commonJvm") {
            kotlin.srcDirs("src/commonJvm")
        }
        
        val androidMain by getting {
            dependsOn(sourceSets.named("commonJvm").get())
            dependencies {
            }
        }
       val jvmMain by getting {
            dependsOn(sourceSets.named("commonJvm").get())
            dependencies {
            }
        }
}

I guess its not a "dependency" but it just "depends on" the common source set! /s
Makes sense to someone I guess?!?!

Personally, I think that all 3 of these ways ought to work, so I'm leaving this cookie crumb to save you 4 hours of head banging pain.

Note that you could also "cheat" and just use symlinks. But mswindows does not like that.

AndrewStone
  • 517
  • 4
  • 14