Skip to content

Commit cb85e02

Browse files
ideclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 94aee19 commit cb85e02

4 files changed

Lines changed: 62 additions & 12 deletions

File tree

‎packages/asset-utils/src/AndroidPathUtils.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ function getAndroidAssetSuffix(scale /*: number */) /*: string */ {
4848
}
4949

5050
// See https://developer.android.com/guide/topics/resources/drawable-resource.html
51+
// HEIC and HEIF are absent because the Android Gradle plugin fails the build on
52+
// such a file in a drawable folder, so those images are packaged as raw
53+
// resources.
5154
const drawableFileTypes /*: Set<string> */ = new Set([
5255
'gif',
53-
'heic',
54-
'heif',
5556
'jpeg',
5657
'jpg',
5758
'ktx',

‎packages/asset-utils/src/__tests__/AndroidPathUtils-test.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ describe('getAndroidResourceFolderName', () => {
7272
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1)).toBe('raw');
7373
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1.25)).toBe('raw');
7474
});
75+
76+
test.each(['heic', 'heif'])('returns "raw" for %s images', type => {
77+
expect(getAndroidResourceFolderName({...DRAWABLE_ASSET, type}, 1)).toBe(
78+
'raw',
79+
);
80+
});
7581
});
7682

7783
describe('getAndroidResourceIdentifier', () => {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@ import javax.annotation.concurrent.ThreadSafe
1717
@ThreadSafe
1818
public object ResourceDrawableIdHelper {
1919
private val resourceDrawableIdMap: MutableMap<String, Int> = HashMap()
20+
private val rawResourceIdMap: MutableMap<String, Int> = HashMap()
2021

2122
@Synchronized
2223
@JvmStatic
2324
public fun clear() {
2425
resourceDrawableIdMap.clear()
26+
rawResourceIdMap.clear()
2527
}
2628

2729
@JvmStatic
28-
public fun getResourceDrawableId(context: Context, name: String?): Int {
30+
public fun getResourceDrawableId(context: Context, name: String?): Int =
31+
getResourceId(context, name, "drawable", resourceDrawableIdMap)
32+
33+
private fun getResourceId(
34+
context: Context,
35+
name: String?,
36+
type: String,
37+
idMap: MutableMap<String, Int>,
38+
): Int {
2939
if (name.isNullOrEmpty()) {
3040
return 0
3141
}
@@ -37,17 +47,13 @@ public object ResourceDrawableIdHelper {
3747
return it
3848
}
3949

40-
synchronized(this) {
41-
return resourceDrawableIdMap.get(normalizedName) ?: addDrawableId(context, normalizedName)
50+
synchronized(ResourceDrawableIdHelper::class.java) {
51+
return idMap.getOrPut(normalizedName) {
52+
context.resources.getIdentifier(normalizedName, type, context.packageName)
53+
}
4254
}
4355
}
4456

45-
private fun addDrawableId(context: Context, normalizedName: String): Int {
46-
val newId = context.resources.getIdentifier(normalizedName, "drawable", context.packageName)
47-
resourceDrawableIdMap[normalizedName] = newId
48-
return newId
49-
}
50-
5157
@JvmStatic
5258
public fun getResourceDrawable(context: Context, name: String?): Drawable? {
5359
val resId = getResourceDrawableId(context, name)
@@ -56,7 +62,11 @@ public object ResourceDrawableIdHelper {
5662

5763
@JvmStatic
5864
public fun getResourceDrawableUri(context: Context, name: String?): Uri {
59-
val resId = getResourceDrawableId(context, name)
65+
val drawableId = getResourceDrawableId(context, name)
66+
// An image in a format that cannot be packaged as a drawable, such as HEIC, is bundled as a
67+
// raw resource.
68+
val resId =
69+
if (drawableId > 0) drawableId else getResourceId(context, name, "raw", rawResourceIdMap)
6070
return if (resId > 0) {
6171
Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(resId.toString()).build()
6272
} else {

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelperTest.kt‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,31 @@
88
package com.facebook.react.views.imagehelper
99

1010
import android.content.Context
11+
import android.content.res.Resources
12+
import android.net.Uri
1113
import org.assertj.core.api.Assertions.assertThat
1214
import org.junit.After
1315
import org.junit.Before
1416
import org.junit.Test
1517
import org.junit.runner.RunWith
18+
import org.mockito.kotlin.mock
19+
import org.mockito.kotlin.whenever
1620
import org.robolectric.RobolectricTestRunner
1721
import org.robolectric.RuntimeEnvironment
1822

1923
@RunWith(RobolectricTestRunner::class)
2024
class ResourceDrawableIdHelperTest {
2125

2226
private lateinit var context: Context
27+
private val mockContext = mock<Context>()
28+
private val resources = mock<Resources>()
2329

2430
@Before
2531
fun setUp() {
2632
context = RuntimeEnvironment.getApplication()
2733
ResourceDrawableIdHelper.clear()
34+
whenever(mockContext.resources).thenReturn(resources)
35+
whenever(mockContext.packageName).thenReturn("test.images")
2836
}
2937

3038
@After
@@ -72,4 +80,29 @@ class ResourceDrawableIdHelperTest {
7280
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "unknown_image"))
7381
.isEqualTo(0)
7482
}
83+
84+
@Test
85+
fun uriFallsBackToRawResourceForNameWithoutADrawable() {
86+
whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42)
87+
88+
assertThat(ResourceDrawableIdHelper.getResourceDrawableUri(mockContext, "photo"))
89+
.isEqualTo(Uri.parse("res:/42"))
90+
}
91+
92+
@Test
93+
fun uriPrefersDrawableOverRawResourceWithTheSameName() {
94+
whenever(resources.getIdentifier("photo", "drawable", "test.images")).thenReturn(23)
95+
whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42)
96+
97+
assertThat(ResourceDrawableIdHelper.getResourceDrawableUri(mockContext, "photo"))
98+
.isEqualTo(Uri.parse("res:/23"))
99+
}
100+
101+
@Test
102+
fun drawableIdIgnoresRawResources() {
103+
whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42)
104+
105+
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(mockContext, "photo")).isEqualTo(0)
106+
assertThat(ResourceDrawableIdHelper.getResourceDrawable(mockContext, "photo")).isNull()
107+
}
75108
}

0 commit comments

Comments
 (0)