在检测测试期间,库gradle模块中的资源的ResourceNotFoundException

Zak*_*rdi 5 android android-testing android-gradle-plugin android-instrumentation

在仪器测试期间,我的应用程序抛出以下错误.我的非测试版本中没有抛出此错误.

android.content.res.Resources$NotFoundException: String resource ID #0x7f030002
Run Code Online (Sandbox Code Playgroud)

我有两个gradle模块

:app (android-application) //applicationId: com.app
:library (android-library) //applicationId: com.library
//:app includes :library as a dependency
Run Code Online (Sandbox Code Playgroud)

有问题的资源在:library模块中定义并抛出.

在仪器下,我的:library模块调用:

  • context.getResources().getString(com.library.R.string.pref_key) - 这会引发异常

如果我在检测期间调试断点并调用:

  • context.getResources().getString(com.app.R.string.pref_key) - 这个有效!但这是我的:library模块中的代码,它没有依赖性:app.

我怎样才能解决这个问题?注 - 错误仅发生在检测测试期间,而不是正常构建期间.

更新:在检测中,调用getString(com.library.R.string.pref_key):app模块中的类调用时起作用,但是从:library模块中的类抛出异常.从字面上看,Resources当从:appvs 调用时,在同一个实例上调用完全相同的方法会有不同的行为:library.

Wen*_*hao 0

不完全确定原因,但手动添加资源路径可能是可以尝试的,

android {
sourceSets {
    main {
        res.srcDirs = ['res1', 'res2']
    }
}
}
Run Code Online (Sandbox Code Playgroud)

请参阅此链接了解更多信息