Repository navigation
Package HEIC and HEIF image assets as Android raw resources - #60372
Conversation
chrfalch
left a comment
There was a problem hiding this comment.
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?
|
@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) { | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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>
09abb1e to
cb85e02
Compare
|
@chrfalch Thanks, all three are in the description now.
|
|
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? |
Summary:
drawableFileTypeslistsheicandheif, so the bundle command copies such an asset into adrawable-*folder. The Android Gradle plugin (FileResourceNameValidator) accepts only.xml,.png,.9.png,.gif,.jpeg,.jpg,.bmp,.webpand.aviffiles 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 toraw, which accepts any file.ResourceDrawableIdHelper.getResourceDrawableUri, which turns an image source's name into theres: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, anImagewhose source namesintroused to stay blank; Fresco now fails to decode the file and theImagecallsonError.getResourceDrawableUriis public, so other callers get the fallback too.A raw HEIC may be stored compressed in the APK:
.heicand.heifare not among the extensions AAPT2 leaves uncompressed (.jpg,.png,.webpand others;Link.cpp), so AAPT2 deflates such a file when that makes it smaller. Fresco reads it either way.LocalResourceFetchProducerreads the bytes withResources.openRawResource, which decompresses, and usesopenRawResourceFd, which works only on uncompressed data, just to learn the length: it catches theResources.NotFoundExceptionand reads the stream to its end.Decoding a HEIC also needs ExifInterface on the classpath, which #60371 added.
ResourceDrawableIdHelpernow guards its maps with one monitor, the class. Onmain,clear()locks the class (@Synchronizedon a@JvmStaticfunction) and the lookup locks the object, so the two do not exclude each other.Limitations:
getResourceDrawableIdandgetResourceDrawablestill resolve drawables only: their callers (defaultSourceandloadingIndicatorSourceonImage,inlineImageLeftonTextInput, andImageLoaderModule) load the id as a drawable, and a raw resource with the same name, such as a JSON file, would throwResources.NotFoundException. A HEIC image works as an imagesourcebut not in those props.photo@2x.heicandphoto@3x.heicare both written toraw/photo.heic, as for other raw asset types.ktxstays 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:
New
AndroidPathUtils-test.jscases check thatheicandheifassets go toraw; onmainthey fail (Expected: "raw",Received: "drawable-mdpi").New tests:
uriFallsBackToRawResourceForNameWithoutADrawable(fails onmainwith an empty URI),uriPrefersDrawableOverRawResourceWithTheSameName, anddrawableIdIgnoresRawResources.The compression behavior comes from the sources and from AAPT2 9.2.1 itself:
aapt2 linkon ares/rawfolder stores a compressible.heicfile deflated and a.jpgwith the same bytes uncompressed, andjavap -p -conLocalResourceFetchProducerinimagepipeline-3.9.0shows the calls and the exception table described above.The accepted extensions come from
javaponSdkUtilsandFileResourceNameValidatorin thecommonandsdk-common32.2.1 jars, which AGP 9.2.1 uses. ESLint, Prettier,yarn flow-checkand 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.