I'm trying to display launcher icon of the app:

Image(
    modifier = Modifier.size(24.dp),
    painter = adaptiveIconPainterResource(id = R.mipmap.ic_launcher_round),
    contentDescription = stringResource(id = R.string.app_name),
)

adaptiveIconPainterResource():

@Composable
fun adaptiveIconPainterResource(@DrawableRes id: Int): Painter {
    val context = LocalContext.current
    val res = context.resources
    val theme = context.theme

    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Android O supports adaptive icons, try loading this first (even though this is least likely to be the format).
        val adaptiveIcon = remember(id) {
            ResourcesCompat.getDrawable(res, id, theme) as? AdaptiveIconDrawable
        }
        if (adaptiveIcon != null) {
            remember(id) {
                BitmapPainter(adaptiveIcon.toBitmap().asImageBitmap())
            }
        } else {
            // We couldn't load the drawable as an Adaptive Icon, just use painterResource
            painterResource(id)
        }
    } else {
        // We're not on Android O or later, just use painterResource
        painterResource(id)
    }
}

It seems ResourcesCompat.getDrawable(res, id, theme) as? AdaptiveIconDrawable fails for some reason on this device so it just returns null and that's why it tries to use painterResource instead and crashes there because it's not for adaptive resources, only for vector drawable or usual image formats like (png, webp)

I tested it on real devices Android 6, 12-14

But at Firebase Crashlytics I saw a crash for Android 8.1:

enter image description here

Fatal Exception: java.lang.IllegalArgumentException
Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG, WEBP
androidx.compose.ui.res.PainterResources_androidKt.loadVectorResource (PainterResources_android.kt:1299)

androidx.compose.ui.res.PainterResources_androidKt.painterResource (PainterResources_android.kt:1299)
com.hidden.design.components.ImagesKt.adaptiveIconPainterResource (Images.kt:150)
com.hidden.main.ComposableSingletons$MainScreenKt$lambda-1$1.invoke (MainScreen.kt:440)

I don't have a real device with Android 8.1 but I tested it on Pixel 2 Emulator with Android 8.1 and it works fine.

So what's exactly can be wrong here?

Mipmap:

enter image description here

Android Studio generates webp now instead of png, don't know if it can be an issue, but it works for Samsung S5 on Android 6....

Update

I tried to use Glide Compose lib and it works fine for the same my devices and emulators:

GlideImage(
    modifier = Modifier.size(24.dp),
    model = R.mipmap.ic_launcher_round,
    transition = CrossFade,
    contentDescription = stringResource(id = R.string.app_name),
)

I will try to use this code for release build and hope I won't get crashes as it was with adaptiveIconPainterResource function

0

There are 0 best solutions below