移除未引用类触发Resources$NotFoundException的原因排查
问题:移除未引用类后触发Resources$NotFoundException异常
我使用以下Compose代码获取资源:
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) }
但移除两个看似未被其他类引用的类时,会抛出如下异常:
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)
这两个被移除的类代码如下:
@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 ), ... ) }
我的项目配置如下:
[versions] agp = "8.4.0" kotlin = "1.9.24" ksp = "1.9.24-1.0.20" coreKtx = "1.13.1" hilt = "2.51.1"
我想了解:为何移除这两个看似未被其他类引用的类,会触发该资源找不到异常?
内容的提问来源于stack exchange,提问作者Connor
相关产品推荐
相关产品推荐

