Search code examples
androidandroid-studioresourcesinstrumentation

Why do I get a Resources$NotFoundException when using InstrumentationRegistry.getContext().getString()?


I am running Android Instrumentation tests and when I use the Context returned by InstrumentationRegistry.getContext() to retrieve a string given an id I get the message:

android.content.res.Resources$NotFoundException: String resource ID #0x7f060020

I've tried ids from other string resources but to no avail.

I've looked through several questions about this same exception on stackoverflow but all of them involve passing an the string id to View.setText. This is not relevant for me since I am creating a library and there are no Views at all.


Solution

  • I figured this out for myself. The module under test was set up to be an application project but it should have been set up as a library project. In the module's build.gradle file I changed this:

    apply plugin: 'com.android.application'

    to this:

    apply plugin: 'com.android.library'

    I also had to remove the applicationId line from the build.gradle file because it is not applicable for library projects.

    Once I made these changes the string's resource id could be found.