Skip to content

Package HEIC and HEIF image assets as Android raw resources - #60372

Merged
meta-codesync[bot] merged 1 commit into
react:mainfrom
expo:@ide/android-heic-raw-resources
Dec 1, 2026
Merged

meta-codesync[bot] merged 1 commit into
react:mainfrom
expo:@ide/android-heic-raw-resources

Conversation

@ide

@ide ide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

drawableFileTypes lists heic and heif, so the bundle command copies such an asset into a drawable-* folder. The Android Gradle plugin (FileResourceNameValidator) accepts only .xml, .png, .9.png, .gif, .jpeg, .jpg, .bmp, .webp and .avif files in those folders, and fails resource merging on any other file with "The file name must end with .xml or .png". A release build of an app that requires a HEIC image therefore fails.

This change removes both types from drawableFileTypes, so these assets go to raw, which accepts any file. ResourceDrawableIdHelper.getResourceDrawableUri, which turns an image source's name into the res: URI that Fresco loads, now looks among the raw resources when no drawable has the name. A drawable keeps precedence.

The fallback matches a raw resource by name alone, whatever its type. In an app that bundles intro.mp3, an Image whose source names intro used to stay blank; Fresco now fails to decode the file and the Image calls onError. getResourceDrawableUri is public, so other callers get the fallback too.

A raw HEIC may be stored compressed in the APK: .heic and .heif are not among the extensions AAPT2 leaves uncompressed (.jpg, .png, .webp and others; Link.cpp), so AAPT2 deflates such a file when that makes it smaller. Fresco reads it either way. LocalResourceFetchProducer reads the bytes with Resources.openRawResource, which decompresses, and uses openRawResourceFd, which works only on uncompressed data, just to learn the length: it catches the Resources.NotFoundException and reads the stream to its end.

Decoding a HEIC also needs ExifInterface on the classpath, which #60371 added.

ResourceDrawableIdHelper now guards its maps with one monitor, the class. On main, clear() locks the class (@Synchronized on a @JvmStatic function) and the lookup locks the object, so the two do not exclude each other.

Limitations:

  • getResourceDrawableId and getResourceDrawable still resolve drawables only: their callers (defaultSource and loadingIndicatorSource on Image, inlineImageLeft on TextInput, and ImageLoaderModule) load the id as a drawable, and a raw resource with the same name, such as a JSON file, would throw Resources.NotFoundException. A HEIC image works as an image source but not in those props.
  • Density variants of a raw asset are not kept: photo@2x.heic and photo@3x.heic are both written to raw/photo.heic, as for other raw asset types.
  • ktx stays in the list. AGP rejects it in the same way, but nothing in the open source image pipeline decodes it.

Changelog:

[ANDROID] [FIXED] - Package HEIC and HEIF image assets as raw resources so that release builds accept them

Test Plan:

yarn jest packages/asset-utils packages/community-cli-plugin/src/commands/bundle

New AndroidPathUtils-test.js cases check that heic and heif assets go to raw; on main they fail (Expected: "raw", Received: "drawable-mdpi").

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests com.facebook.react.views.imagehelper.ResourceDrawableIdHelperTest -Preact.internal.useHermesStable=true --console=plain

New tests: uriFallsBackToRawResourceForNameWithoutADrawable (fails on main with an empty URI), uriPrefersDrawableOverRawResourceWithTheSameName, and drawableIdIgnoresRawResources.

The compression behavior comes from the sources and from AAPT2 9.2.1 itself: aapt2 link on a res/raw folder stores a compressible .heic file deflated and a .jpg with the same bytes uncompressed, and javap -p -c on LocalResourceFetchProducer in imagepipeline-3.9.0 shows the calls and the exception table described above.

The accepted extensions come from javap on SdkUtils and FileResourceNameValidator in the common and sdk-common 32.2.1 jars, which AGP 9.2.1 uses. ESLint, Prettier, yarn flow-check and ktfmt are clean.

Not run: a release build of an app with a HEIC asset, and showing the image from a raw resource on a device.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 28, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 28, 2026
@ide
ide marked this pull request as ready for review November 29, 2026 12:02
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, and both new tests fail without the fix. (Commenting rather than approving since it's still a draft.) A few things for the description and test plan:

1. The raw fallback applies to every image name, not only HEIC. getResourceDrawableUri now falls back to any raw resource with a matching name, without checking that it's an image. If an app bundles intro.mp3 or config.json as raw resources and an Image source names intro by mistake, the image used to stay blank with a warnImageSource warning; now Fresco tries to decode the MP3 and reports a decode error. getResourceDrawableUri is public, so third-party callers get the new behavior too. Probably harmless, but worth stating in the description.

2. Compressed raw HEIC. As far as I can tell, .heic/.heif aren't in AAPT2's default no-compress list, so the raw resource may be stored compressed in the APK. Has a bundled HEIC been loaded on a device from a release build, to confirm Fresco's local resource fetcher reads it correctly?

3. Related PR. This needs #60371 to be useful end to end: with this PR alone, a bundled HEIC builds but crashes at decode unless the app already ships androidx.exifinterface. Could you link the two PRs in each description, and ideally check the full path (release build, bundled HEIC, displayed on device) with both applied?

@meta-codesync

meta-codesync Bot commented Nov 30, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D135070431.

@@ -38,16 +48,12 @@ public object ResourceDrawableIdHelper {
}

synchronized(this) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The internal linter is failing with this (pre-existing?) signal.

This locks ResourceDrawableIdHelper.INSTANCE, but this class also locks ResourceDrawableIdHelper::class.java (line 22), so the two do not exclude each other. Use synchronized(ResourceDrawableIdHelper::class.java) { ... } instead.
Static state guarded by two different monitors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing: on main, clear() is @Synchronized @JvmStatic, which locks the class, and the lookup uses synchronized(this), which locks the object; this PR only rewrote the body of that block. Changed it to synchronized(ResourceDrawableIdHelper::class.java), so both use the class monitor. The PR needs importing again for the internal check.

@Abbondanzo Abbondanzo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

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>
@ide

ide commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@chrfalch Thanks, all three are in the description now.

  1. The fallback matches any raw resource by name, and getResourceDrawableUri is public. An Image naming intro with intro.mp3 bundled now gets onError from a failed decode, where it stayed blank before.
  2. .heic and .heif aren't in AAPT2's no-compress list, so a HEIC in res/raw is stored deflated when that makes it smaller. Fresco reads it either way: LocalResourceFetchProducer gets the bytes from openRawResource, which decompresses, and calls openRawResourceFd only for the length; when that throws for a compressed entry, it reads the stream to its end. I checked this with the aapt2 that AGP 9.2.1 uses and in Fresco 3.9.0's bytecode, not on a device, and the description says so.
  3. Linked Ship ExifInterface so a HEIC image does not crash Android #60371, which has merged, in both descriptions. The end-to-end check on a device hasn't been run.

@meta-codesync
meta-codesync Bot merged commit 89446b2 into react:main Dec 1, 2026
99 of 100 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @ide in 89446b2

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Dec 1, 2026
@ide
ide deleted the @ide/android-heic-raw-resources branch December 1, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants