SharedTest got warning "Duplicate content root detected" on Android Studio Chipmunk
Asked Answered
F

5

17

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?

Forkey answered 24/5, 2022 at 7:31 Comment(0)
H
13

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

Hydrocortisone answered 2/6, 2022 at 4:55 Comment(6)
Anyone have sample to use test fixtures? I have tried it here medium.com/easyread/…. It working well in compile time, but in runtime it say “Unresolved Reference".Forkey
@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 #72219145Hydrocortisone
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.Hydrocortisone
Check one of my questions and answers (shared srcDirs), you will find a working solution to this problem.Twister
@SeanBlahovici Could you give a link to "one of my questions and answers (shared srcDirs)" that you refer to?Hydrocortisone
@Hydrocortisone sure : #72219145Twister
C
2

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.

Cy answered 11/8, 2023 at 15:35 Comment(0)
C
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://mcmap.net/q/534419/-sharedtest-got-warning-quot-duplicate-content-root-detected-quot-on-android-studio-chipmunk
    testImplementation(project(path = ":sharedTest"))
    androidTestImplementation(project(path = ":sharedTest"))
}
Criminology answered 2/1, 2023 at 10:56 Comment(0)
A
1

You can use symbolic links to get around the problem.

Execute the following command in app/src directory:

ln -s sharedTest sharedAndroidTest

Edit the build.gradle file:

sourceSets {
    androidTest.java.srcDirs += "src/sharedAndroidTest/java"
    test.java.srcDirs += "src/sharedTest/java" 
}
Approver answered 2/11, 2023 at 8:4 Comment(0)
C
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.

Cycloparaffin answered 11/12, 2022 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.