-1

i am using

  val name = if (!formula.productName?.nameRes.isNullOrBlank()) {
      stringResource(getStringResId(formula.productName?.nameRes!!))
  } else if (!formula.productName?.name.isNullOrBlank()) {
      formula.productName?.name
  } else {
      stringResource(R.string.common_empty_string)
  }
  Text(
     text = name!!,
     fontSize = textSize.nsp(),
     color = Color.White,
     textAlign = TextAlign.Center,
     modifier = Modifier
             .align(alignment = Alignment.TopCenter)
             .offset(y = 124.dp)
  )

@Composable
fun getImageResId(imageName: String): Int {
    val context = LocalContext.current
    return context.resources.getIdentifier(imageName, "drawable", context.packageName)
}

@Composable
fun getStringResId(stringName: String): Int {
    val context = LocalContext.current
    return context.resources.getIdentifier(stringName, "string", context.packageName)
}

to find resource.

but when i remove two classes from my project, it will cause this exception

 android.content.res.Resources$NotFoundException: String resource ID #0x0
     at android.content.res.Resources.getText(Resources.java:444)
     at android.content.res.Resources.getString(Resources.java:537)
     at 
androidx.compose.ui.res.StringResources_androidKt.stringResource(Unknown Source:20)

These are those two classes, I'm pretty sure they are not referenced by other classes. but when i put them back to my project, it goes well.

@Immutable
data class DrinksModel(
    val productId: Int,
    var type: ProductType,
    @StringRes val name: Int,
    @DrawableRes val imageRes: Int,
)

@Singleton
class HomeLocalDataSource @Inject constructor() {
    val specialItem = mutableListOf(1, 3, 4, 11, 12)

    val drinksTypes = listOf(
        DrinksModel(
            productId = 1,
            type = ProductType.STEAM,
            name = R.string.home_item_stop,
            imageRes = R.drawable.home_stop_normal_ic
        ),
        ...
    )
}

this is my project settings

[versions]
agp = "8.4.0"
kotlin = "1.9.24"
ksp = "1.9.24-1.0.20"
coreKtx = "1.13.1"
hilt = "2.51.1"

i just want to konw why when i remove those two classes, my app will get resourcenotfoundexception.

1
  • i finally found the reason. because i load some drawble and string resources dynamically from local text file using context.resources.getIdentifier(). when i build a release apk, when the isShrinkResources variable is set to true will remove all resources not directly referenced in my code. so it will throw exception. The reason why HomeLocalDataSource can help is that it directly referenced all my drawble and string resources, this is why when remove this class, it goes wrong. Commented Jan 17 at 7:10

1 Answer 1

1

There's an error in logic somewhere. The following function returns 0 if the resource was not found:

@Composable
fun getStringResId(stringName: String): Int {
    val context = LocalContext.current
    return context.resources.getIdentifier(stringName, "string", context.packageName)
}

Therefore, using stringResource(0) results in the above error:

android.content.res.Resources$NotFoundException: String resource ID #0x0 error
Sign up to request clarification or add additional context in comments.

8 Comments

the wired part is i did't change any code, just put those two classes back. then it's fine. and i delete them, it throws exception again.
@Connor I think this is a floating error that does not depend on the appearance or disappearance of these particular classes (DrinksModel, HomeLocalDataSource), they have nothing to do with the code where the error occurs.
@Connor After deleting the classes, do you delete the resources R.drawable.home_stop_normal_ic, R.string.home_item_stop?
no, just delete the classes. at first I thought the same thing as you, how could it be that unrelated classes. but after i tried for serveral times, it must be those two classes. I'm really confused
@Connor Perhaps your project has strict resource optimisation enabled or R8 fails to detect that these resources should be saved. Try to use tools:keep.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.