Skip to content

Set View, Text and Image props on Android without reflection - #60570

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/android-core-view-manager-delegates
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/android-core-view-manager-delegates

Conversation

@ide

@ide ide commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

ReactViewManager, ReactTextViewManager and ReactImageManager have no codegen delegate, so GenericViewManagerDelegate sets their props: it finds each prop's annotated setter in ViewManagersPropertyCache and calls it with Method.invoke, boxing primitive values. Each of these managers now returns a delegate that calls its setters directly, with the defaults and conversions of the reflective setters, and passes the props of BaseViewManager to BaseViewManagerDelegate. SelectableTextViewManager, which is final and declares no props, gets the direct delegate of ReactTextViewManager. Other subclasses of ReactViewManager and ReactTextViewManager keep the reflective delegate, because it also finds the props they declare.

BaseViewManagerDelegate now sets onStartShouldSetResponder, whose setter marks a view as interactable when enableInteractableResponderViews is on. It ignored the prop, so the direct delegates would have dropped that behavior; components with codegen delegates now get it too.

The direct delegates keep two behaviors of the reflective one. When a setter fails, for example because a value has the wrong type or is null where the setter needs a value, they throw the JSApplicationIllegalArgumentException that names the prop and the view manager, with the message ViewManagersPropertyCache builds. And native view configs do not change: when skipNativePropsForViewManagerWithDelegateAndroid is on, getNativeProps now returns an empty map only for managers whose delegate is not an AnnotatedPropsDelegate. The reflective delegate and the three direct delegates carry this internal marker, because each sets exactly the props its manager declares with @ReactProp and @ReactPropGroup, so RCTView, RCTText, RCTSelectableText and RCTImageView keep reporting their native props.

Changelog:

[ANDROID] [CHANGED] - Set the props of View, Text and Image with direct calls instead of reflection.

Test Plan:

ViewManagerDelegatesTest sets every prop that the reflective setters of View, Text, SelectableText and Image know, first to null and then to a value, through the reflective and the direct delegate on separate mock managers, and checks that both make the same calls with the same arguments. It fails when a prop is missing from a delegate, when an index or a default value differs, or when BaseViewManagerDelegate ignores onStartShouldSetResponder. It checks that both delegates throw the same JSApplicationIllegalArgumentException for a value of the wrong type, for a null backfaceVisibility and for a wrong opacity, which BaseViewManagerDelegate sets. It also checks which managers get the direct delegate, and that a subclass of ReactViewManager still receives the props it declares. UIManagerModuleConstantsHelperTest checks that the four core managers report their native props with skipNativePropsForViewManagerWithDelegateAndroid on. Each new assertion fails when the behavior it checks is removed. The ReactAndroid unit tests pass with -PenableWarningsAsErrors=true, yarn android-api-validate passes, and RNTester's Release build mounts its home, Text and Image screens on an arm64 Android 16 emulator.

On that emulator, a benchmark in RNTester's Release build applied each set of props through each delegate in one process, rotating which ran first, in five launches of three rounds of 21 samples. The table gives the median of the round medians with the interquartile range:

Props Reflective Direct
View: 10 style and accessibility props, 4 layout props 1,362 ns (1,237–1,482), 803 B 1,024 ns (909–1,054), 524 B
Text: 8 props 438 ns (432–443), 197 B 243 ns (240–245), 49 B
Image: 8 props, without a source 442 ns (432–452), 328 B 237 ns (232–238), 180 B

The bytes are Java heap allocated per set of props. The try/catch in the direct delegates costs nothing measurable: over 30 alternating rounds per component against the same delegates without it, the median difference was −2 ns for View, 0 ns for Text and +2 ns for Image, and neither variant was faster in a clear majority of rounds.

@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 Dec 4, 2026
@ide
ide force-pushed the @ide/android-core-view-manager-delegates branch from 2574c3a to 04606d3 Compare December 4, 2026 19:18
@ide
ide marked this pull request as ready for review December 4, 2026 19:18
@ide
ide marked this pull request as draft December 4, 2026 19:18
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at bea849762c06.

ios: no capture

@ide
ide marked this pull request as ready for review December 4, 2026 20:49

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

I checked every prop in the three new delegates against the reflective setters, including the names, prop group indices, defaults, color conversion and Dynamic wrapping. They match, and the new test compares both paths prop by prop. One question about the feature flag: the earlier test asserted that core managers keep their native prop metadata when skipNativePropsForViewManagerWithDelegateAndroid is on, and this change reverses that. With the flag on, RCTView, RCTText and RCTImageView report empty NativeProps. Anything that still reads their native view config then sees no props: a NativeComponentRegistry setup with native: true or verify: true, or JS code that reads UIManager.getViewManagerConfig('RCTView').NativeProps. RCTSelectableText also keeps reporting its props while RCTText does not. Can you confirm that every app that enables the flag uses static view configs for these three components with verification off?

`ReactViewManager`, `ReactTextViewManager` and `ReactImageManager` have no codegen delegate, so `GenericViewManagerDelegate` sets their props: it finds each prop's annotated setter in `ViewManagersPropertyCache` and calls it with `Method.invoke`, boxing primitive values. Each of these managers now returns a delegate that calls its setters directly, with the defaults and conversions of the reflective setters, and passes the props of `BaseViewManager` to `BaseViewManagerDelegate`. `SelectableTextViewManager`, which is final and declares no props, gets the direct delegate of `ReactTextViewManager`. Other subclasses of `ReactViewManager` and `ReactTextViewManager` keep the reflective delegate, because it also finds the props they declare.

`BaseViewManagerDelegate` now sets `onStartShouldSetResponder`, whose setter marks a view as interactable when `enableInteractableResponderViews` is on. It ignored the prop, so the direct delegates would have dropped that behavior; components with codegen delegates now get it too.

The direct delegates keep two behaviors of the reflective one. When a setter fails, for example because a value has the wrong type or is null where the setter needs a value, they throw the `JSApplicationIllegalArgumentException` that names the prop and the view manager, with the message `ViewManagersPropertyCache` builds. And native view configs do not change: when `skipNativePropsForViewManagerWithDelegateAndroid` is on, `getNativeProps` now returns an empty map only for managers whose delegate is not an `AnnotatedPropsDelegate`. The reflective delegate and the three direct delegates carry this internal marker, because each sets exactly the props its manager declares with `@ReactProp` and `@ReactPropGroup`, so RCTView, RCTText, RCTSelectableText and RCTImageView keep reporting their native props.

`ViewManagerDelegatesTest` sets every prop that the reflective setters of View, Text, SelectableText and Image know, first to null and then to a value, through the reflective and the direct delegate on separate mock managers, and checks that both make the same calls with the same arguments. It fails when a prop is missing from a delegate, when an index or a default value differs, or when `BaseViewManagerDelegate` ignores `onStartShouldSetResponder`. It checks that both delegates throw the same `JSApplicationIllegalArgumentException` for a value of the wrong type, for a null `backfaceVisibility` and for a wrong `opacity`, which `BaseViewManagerDelegate` sets. It also checks which managers get the direct delegate, and that a subclass of `ReactViewManager` still receives the props it declares. `UIManagerModuleConstantsHelperTest` checks that the four core managers report their native props with `skipNativePropsForViewManagerWithDelegateAndroid` on. Each new assertion fails when the behavior it checks is removed. The ReactAndroid unit tests pass with `-PenableWarningsAsErrors=true`, `yarn android-api-validate` passes, and RNTester's Release build mounts its home, Text and Image screens on an arm64 Android 16 emulator.

On that emulator, a benchmark in RNTester's Release build applied each set of props through each delegate in one process, rotating which ran first, in five launches of three rounds of 21 samples. The table gives the median of the round medians with the interquartile range:

| Props | Reflective | Direct |
| --- | --- | --- |
| View: 10 style and accessibility props, 4 layout props | 1,362 ns (1,237–1,482), 803 B | 1,024 ns (909–1,054), 524 B |
| Text: 8 props | 438 ns (432–443), 197 B | 243 ns (240–245), 49 B |
| Image: 8 props, without a source | 442 ns (432–452), 328 B | 237 ns (232–238), 180 B |

The bytes are Java heap allocated per set of props. The try/catch in the direct delegates costs nothing measurable: over 30 alternating rounds per component against the same delegates without it, the median difference was −2 ns for View, 0 ns for Text and +2 ns for Image, and neither variant was faster in a clear majority of rounds.

Signed-off-by: James Ide <ide@expo.dev>
@ide
ide force-pushed the @ide/android-core-view-manager-delegates branch from 04606d3 to bea8497 Compare December 5, 2026 09:59
@ide

ide commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking every prop. You're right that the flag would have changed what these three components report, and I can't confirm that every app that enables it uses static view configs with verification off. So the PR no longer changes native view configs: with skipNativePropsForViewManagerWithDelegateAndroid on, RCTView, RCTText, RCTSelectableText and RCTImageView report their native props exactly as before. The flag now skips only managers whose delegate is not an AnnotatedPropsDelegate. That internal marker is carried by the reflective delegate and by the three direct delegates, because each of them sets exactly the props the manager declares with @ReactProp and @ReactPropGroup. UIManagerModuleConstantsHelperTest again asserts that the core managers keep their native props with the flag on, now for all four.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants