I've created an Android project from scratch, with package name com.example.myapplication
I've a strings.xml
file in the main
package (app module), with a String called yyy
.
I've another strings.xml
file in the androidTest
package, with a String called xxx
.
I can use both strings without problem in a instrumented test, simply using the correct R file:
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val stringFromApp = appContext.getString(com.example.myapplication.R.string.yyy)
val stringFromTest = appContext.getString(com.example.myapplication.test.R.string.xxx)
assertEquals("com.example.myapplication", appContext.packageName)
}
}
But, if I add an applicationIdSuffix
in debug variant, then androidTest's R file is not generated correctly. It seems a bug on the system:
debug {
applicationIdSuffix '.imasuffix'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val stringFromApp = appContext.getString(com.example.myapplication.R.string.yyy)
val stringFromTest = appContext.getString(com.example.myapplication.test.R.string.xxx)
val stringFromNewSuffix = appContext.getString(com.example.myapplication.imasuffix.test.R.string.xxx)
assertEquals("com.example.myapplication", appContext.packageName)
}
}
As you can see, Android Studio suggests me to use the com.example.myapplication.imasuffix.test.R
file but is not working, even Rebuilding the project or invalidating caches.
I've seen a similar issue related with Roboelectric, but I'm using the standard AndroidJUnit4 tests.
Thanks.