From 09abb1e95fbce07ee8382ab883fd1d4647e30162 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:36:25 -0800 Subject: [PATCH] Package HEIC and HEIF image assets as Android raw resources The bundle command copies a HEIC or HEIF asset into a `drawable-*` resource folder. The Android Gradle plugin accepts only XML, PNG, GIF, JPEG, BMP, WebP and AVIF files there, and fails the build on any other file with "The file name must end with .xml or .png", so a release build of an app that requires a HEIC image fails. Remove both types from `drawableFileTypes` so the assets go to `raw`, where any file is accepted. So that the image still resolves, `ResourceDrawableIdHelper.getResourceDrawableUri`, which turns an image source's resource name into a `res:` URI for Fresco, looks the name up among the raw resources when no drawable has it. A drawable keeps precedence over a raw resource with the same name. `getResourceDrawableId` and `getResourceDrawable` still resolve drawables only: their callers load the id as a drawable, which a raw resource is not. Co-Authored-By: Claude Opus 5.5 --- packages/asset-utils/src/AndroidPathUtils.js | 5 +-- .../src/__tests__/AndroidPathUtils-test.js | 6 ++++ .../imagehelper/ResourceDrawableIdHelper.kt | 28 +++++++++++----- .../ResourceDrawableIdHelperTest.kt | 33 +++++++++++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/packages/asset-utils/src/AndroidPathUtils.js b/packages/asset-utils/src/AndroidPathUtils.js index 5eb21cff8a60..a8d3125afd0d 100644 --- a/packages/asset-utils/src/AndroidPathUtils.js +++ b/packages/asset-utils/src/AndroidPathUtils.js @@ -48,10 +48,11 @@ function getAndroidAssetSuffix(scale /*: number */) /*: string */ { } // See https://developer.android.com/guide/topics/resources/drawable-resource.html +// HEIC and HEIF are absent because the Android Gradle plugin fails the build on +// such a file in a drawable folder, so those images are packaged as raw +// resources. const drawableFileTypes /*: Set */ = new Set([ 'gif', - 'heic', - 'heif', 'jpeg', 'jpg', 'ktx', diff --git a/packages/asset-utils/src/__tests__/AndroidPathUtils-test.js b/packages/asset-utils/src/__tests__/AndroidPathUtils-test.js index fafed0cd4d5c..bbf583dd9cee 100644 --- a/packages/asset-utils/src/__tests__/AndroidPathUtils-test.js +++ b/packages/asset-utils/src/__tests__/AndroidPathUtils-test.js @@ -72,6 +72,12 @@ describe('getAndroidResourceFolderName', () => { expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1)).toBe('raw'); expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1.25)).toBe('raw'); }); + + test.each(['heic', 'heif'])('returns "raw" for %s images', type => { + expect(getAndroidResourceFolderName({...DRAWABLE_ASSET, type}, 1)).toBe( + 'raw', + ); + }); }); describe('getAndroidResourceIdentifier', () => { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt index 1cb91f513f6a..77c9e7f44ef8 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt @@ -17,15 +17,25 @@ import javax.annotation.concurrent.ThreadSafe @ThreadSafe public object ResourceDrawableIdHelper { private val resourceDrawableIdMap: MutableMap = HashMap() + private val rawResourceIdMap: MutableMap = HashMap() @Synchronized @JvmStatic public fun clear() { resourceDrawableIdMap.clear() + rawResourceIdMap.clear() } @JvmStatic - public fun getResourceDrawableId(context: Context, name: String?): Int { + public fun getResourceDrawableId(context: Context, name: String?): Int = + getResourceId(context, name, "drawable", resourceDrawableIdMap) + + private fun getResourceId( + context: Context, + name: String?, + type: String, + idMap: MutableMap, + ): Int { if (name.isNullOrEmpty()) { return 0 } @@ -38,16 +48,12 @@ public object ResourceDrawableIdHelper { } synchronized(this) { - return resourceDrawableIdMap.get(normalizedName) ?: addDrawableId(context, normalizedName) + return idMap.getOrPut(normalizedName) { + context.resources.getIdentifier(normalizedName, type, context.packageName) + } } } - private fun addDrawableId(context: Context, normalizedName: String): Int { - val newId = context.resources.getIdentifier(normalizedName, "drawable", context.packageName) - resourceDrawableIdMap[normalizedName] = newId - return newId - } - @JvmStatic public fun getResourceDrawable(context: Context, name: String?): Drawable? { val resId = getResourceDrawableId(context, name) @@ -56,7 +62,11 @@ public object ResourceDrawableIdHelper { @JvmStatic public fun getResourceDrawableUri(context: Context, name: String?): Uri { - val resId = getResourceDrawableId(context, name) + val drawableId = getResourceDrawableId(context, name) + // An image in a format that cannot be packaged as a drawable, such as HEIC, is bundled as a + // raw resource. + val resId = + if (drawableId > 0) drawableId else getResourceId(context, name, "raw", rawResourceIdMap) return if (resId > 0) { Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(resId.toString()).build() } else { diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelperTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelperTest.kt index 1c301ae1a4c6..82edfb7d03b2 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelperTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelperTest.kt @@ -8,11 +8,15 @@ package com.facebook.react.views.imagehelper import android.content.Context +import android.content.res.Resources +import android.net.Uri import org.assertj.core.api.Assertions.assertThat import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment @@ -20,11 +24,15 @@ import org.robolectric.RuntimeEnvironment class ResourceDrawableIdHelperTest { private lateinit var context: Context + private val mockContext = mock() + private val resources = mock() @Before fun setUp() { context = RuntimeEnvironment.getApplication() ResourceDrawableIdHelper.clear() + whenever(mockContext.resources).thenReturn(resources) + whenever(mockContext.packageName).thenReturn("test.images") } @After @@ -72,4 +80,29 @@ class ResourceDrawableIdHelperTest { assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "unknown_image")) .isEqualTo(0) } + + @Test + fun uriFallsBackToRawResourceForNameWithoutADrawable() { + whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42) + + assertThat(ResourceDrawableIdHelper.getResourceDrawableUri(mockContext, "photo")) + .isEqualTo(Uri.parse("res:/42")) + } + + @Test + fun uriPrefersDrawableOverRawResourceWithTheSameName() { + whenever(resources.getIdentifier("photo", "drawable", "test.images")).thenReturn(23) + whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42) + + assertThat(ResourceDrawableIdHelper.getResourceDrawableUri(mockContext, "photo")) + .isEqualTo(Uri.parse("res:/23")) + } + + @Test + fun drawableIdIgnoresRawResources() { + whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42) + + assertThat(ResourceDrawableIdHelper.getResourceDrawableId(mockContext, "photo")).isEqualTo(0) + assertThat(ResourceDrawableIdHelper.getResourceDrawable(mockContext, "photo")).isNull() + } }