Generate keep.xml to prevent resource shrinking on Android #50620
+46
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
On Android, when resource shrinking is enabled, all resources added by Metro may be removed as react-native is accessing resources based on strings rather than references, so AGP can't see its usage.
Example output of
android/app/build/outputs/mapping/release/resources.txt
whenshrinkResources
is enabled.It’s a coincidence that most of the resources are currently working, as many file names begin with strings that already exist in the String Pool. For example,
node_modules...
is flagged as used because 'node' is present in the String Pool, causing it to be whitelisted. However, this does not guarantee that the same will apply to all files - especially in a monorepo setup, where paths are significantly different. For example__node_modules_expo_vectoricons_build_vendor_reactnativevectoricons_fonts_materialcommunityicons
__common_assets_haptics_icon
To prevent that behavior, metro during assets export should create
keep.xml
listing all resources generated by metro.https://developer.android.com/build/shrink-code#keep-resources
We have already made a similar change in expo cli: expo/expo#35465
Changelog:
[ANDROID][ADDED] - Generate keep.xml to prevent resource shrinking
Test Plan:
android.buildTypes.release
topackages/rn-tester/android/app/build.gradle.kts
hermesRelease
variantpackages/rn-tester/android/app/build/outputs/mapping/hermesRelease/resources.txt
@drawable/_reactnative_libraries_newappscreen_components_logo : reachable=false
@drawable/_reactnative_libraries_newappscreen_components_logo : reachable=true