Repository navigation
Commit 89446b2
authored
Package HEIC and HEIF image assets as Android raw resources
## 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`](https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/tools/aapt2/cmd/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`](https://developer.android.com/reference/android/content/res/Resources#openRawResourceFd(int)), 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:
```sh
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"`).
```sh
./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.
Differential Revision: D135070431
Pull Request resolved: #603721 parent e9b0973 commit 89446b2
4 files changed
Lines changed: 62 additions & 12 deletions
File tree
- packages
- asset-utils/src
- __tests__
- react-native/ReactAndroid/src
- main/java/com/facebook/react/views/imagehelper
- test/java/com/facebook/react/views/imagehelper
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
51 | 54 | | |
52 | 55 | | |
53 | | - | |
54 | | - | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
75 | 81 | | |
76 | 82 | | |
77 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| |||
37 | 47 | | |
38 | 48 | | |
39 | 49 | | |
40 | | - | |
41 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
42 | 54 | | |
43 | 55 | | |
44 | 56 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | 57 | | |
52 | 58 | | |
53 | 59 | | |
| |||
56 | 62 | | |
57 | 63 | | |
58 | 64 | | |
59 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
60 | 70 | | |
61 | 71 | | |
62 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| 27 | + | |
| 28 | + | |
23 | 29 | | |
24 | 30 | | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
| 34 | + | |
| 35 | + | |
28 | 36 | | |
29 | 37 | | |
30 | 38 | | |
| |||
72 | 80 | | |
73 | 81 | | |
74 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
75 | 108 | | |
0 commit comments