18

With the first patch for AS Arctic Fox Jetpack Compose previews stopped working.

I'm getting this error for all previews - even older ones, which worked fine a while back:

android.content.res.Resources$NotFoundException: Could not resolve resource value: [some hex value]

Are here any quick fixes for this? Clearing caches and the usual stuff did not work.


EDIT:
Looks like the problem is not always present. Some preview started working, while other are still failing.

EDIT 2:
This is happening in dynamic feature modules, when there's a need for resources from the main module or painterResource() is being used (even is resource from the same module is to be displayed).

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29

4 Answers4

5

This got fixed in AS Bumblebee, patch 2.

Edit (copied from comments): It then got broken again in Electric Eel | 2022.1.1 and fixed again in Flamingo | 2022.2.1 Beta 4.

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29
  • 6
    maybe but it is present in Android Studio Electric Eel | 2022.1.1 Patch 1 or very similar issue where you need to refresh manually preview again after each change for this error to dissapear. – Renetik Feb 14 '23 at 13:31
  • 1
    Fixed in Android Studio Flamingo | 2022.2.1 Beta 4 – Timur Gilfanov Mar 07 '23 at 10:13
3

Same problem here with dynamic-modules project. Inspired by above answer, I've made another temporary workaround while waiting for Compose team to fix this.

import androidx.compose.ui.res.stringResource as originalStringResource

@Composable
@ReadOnlyComposable
fun stringResourceSafe(@StringRes id: Int): String =
    if (BuildConfig.DEBUG) {
        val resources = LocalContext.current.resources
        try {
            resources.getString(id)
        } catch (e: Resources.NotFoundException) {
            "missing res."
        }
    } else {
        originalStringResource(id)
    }
Damiancioo
  • 462
  • 3
  • 13
0

For me it starts working after run the application on a simulator (2022.1.1)

Andrei Veshtard
  • 528
  • 9
  • 12
-1

As a temporary hack workaround I did this to get past the error and preview the UI elements.

//import androidx.compose.ui.res.stringResource

fun stringResource(id: Int): String {
    when (id) {
        R.string.res_id -> return "Foo"
        ...
    }
    return "missing res_id"
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Brian
  • 57
  • 3