簡體   English   中英

從應用程序的 androidTest 中的庫模塊訪問資源時無法解析符號

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM