[英]Cannot access own resources from androidTest after importing own library
[英]Cannot resolve symbol when accessing resources from library module in androidTest of application
我正在为 android 应用程序编写 android 测试,我需要访问依赖库项目中的资源 ID。 但是id无法解析。 我无法从我在应用程序模块中的测试访问库模块中的资源。
这是一个演示问题的示例:
我在我的库com.example.loginlibrary
中定义了一个字符串资源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="location_string">Qatar</string>
</resources>
android 应用程序com.example.app
中的测试,位于androidTest
文件夹中:
package com.example.app
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val foo = appContext.resources.getString(R.string.location_string)
assertEquals("Qatar", foo)
}
}
这导致Unresolved reference: location_string
。
我尝试使用 com.example.loginlibrary.R 从库项目中引用com.example.loginlibrary.R
文件,但它导致Unresolved reference: R
有没有办法从位于资源之外的另一个模块中的测试中引用库项目中的资源?
问题原来是在定义资源的模块中存在编译错误。 我在 Android Studio 中运行测试时没有看到这一点,所以直到我尝试从命令行构建时才注意到它。 在修复编译错误时,生成了模块中的R
文件,我可以从测试中访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.