Search code examples
androidandroid-testing

Android Studio: Shared folder not working anymore


I've a shared test folder configured like this in my gradle file:

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

However, at startup I'm getting this error from Android Studio:

Duplicate content roots detected
            Path [/Users/fil/Documents/projects/deploy/app/build/generated/source/r/debug] of module [deploy.app.unitTest] was removed from modules 

Any idea on how to fix the issue? I've found that I might use test fixtures but I've not found a practical guide to follow :(


Solution

  • Update: I was able to make it work using the suggestion and the example project I found here: Shared srcDirs between test and androidTest, unresolved references after upgrade to Android Studio Chipmunk (IntelliJ 2021.2.1)

    The most difficult parts where:

    • match correctly productFlavors. Otherwise everything won't work correctly

    • I'm still importing shared tests from shared folder in order to launch them with the unit test suite (they are robolectric tests so I want also to launch them visually in case of need). Like this:

      test {
          java.srcDirs += "../shared/src/test/java"
      }
      
    • put the correct testInstrumentationRunner also in the shared gradle since it was a custom one

    • import all the shared code in the main package even if it's used in tests (beside actual tests that are in the test folder)

    I've updated gradle plugin to 7.2.2 only and I didn't update Android Studio to beta versions to make it work.