Android: Have separate resources for tests only

This is more of a tip, than a question, for those how may want to achieve the same thing. I am building an Android app and have created some unit test that require access to some Android string resources (those that are built at compile time via aapt2 and accessed via the R class). At the same time, I don’t want to have these strings bundled in the released Android app (or Android library).

I placed my resources in the following path:


and enabled Android resources for the unit tests:

    testOptions {
        unitTests.includeAndroidResources = true

Even though sourceSets.test.res.srcDirs automatically contained the src/test/res directory, aapt2 probably didn’t pick it up and the strings resources were not available in the project’s R class.

It seems that the resources should be placed in the main source set. This did the trick:

tasks.withType(Test) {
    android.sourceSets.main.res.srcDirs += 'src/test/res'

Perhaps there’s a more elegant way than this.