简体   繁体   中英

Cannot resolve symbol when accessing resources from library module in androidTest of application

I am writing an android test for an android application, and I need to access resource id's that are inside a dependent library project. But the id's cannot be resolved. I'm unable to access the resources inside the library module from my test in the app module.

Here's an example that demonstrate the problem:

Library

I define a string resource inside my Library com.example.loginlibrary :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="location_string">Qatar</string>
</resources>

Application

android test in Application com.example.app , located in androidTest folder:

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)
    }
}

This results in Unresolved reference: location_string .

I tried referencing the R file from the library project with com.example.loginlibrary.R but it results in Unresolved reference: R Is there any way to reference resources in a library project from tests that are located in another module than the resource?

The issue turned out to be that there was a compilation error in the module where the resource was defined. I didn't see this when running the test in Android Studio, so I didn't notice it until I tried building from the command line. When fixing the compilation error, the R file in the module was generated and I was able to access it from the test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM