Skip to content

Generate keep.xml to prevent resource shrinking on Android #50620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

jakex7
Copy link
Contributor

@jakex7 jakex7 commented Apr 10, 2025

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 when shrinkResources is enabled.

@raw/__node_modules_expo_vectoricons_build_vendor_reactnativevectoricons_fonts_materialcommunityicons : reachable=false
@drawable/__common_assets_haptics_icon : reachable=false

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:

  1. Enable resource shrinking in RNTester by adding this to android.buildTypes.release to packages/rn-tester/android/app/build.gradle.kts
isMinifyEnabled = true
isShrinkResources = true
  1. Use some resources in playground, for example:
diff --git a/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js b/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js
index 9dbacb99701..9ac9c231f3f 100644
--- a/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js
+++ b/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js
@@ -11,16 +11,14 @@
 
 import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
 
-import RNTesterText from '../../components/RNTesterText';
 import * as React from 'react';
 import {StyleSheet, View} from 'react-native';
+import {Header} from "react-native/Libraries/NewAppScreen";
 
 function Playground() {
   return (
     <View style={styles.container}>
-      <RNTesterText>
-        Edit "RNTesterPlayground.js" to change this file
-      </RNTesterText>
+      <Header />
     </View>
   );
 }
  1. Build app using hermesRelease variant
  2. See Playground screen
Before After
Zrzut ekranu 2025-04-10 o 12 17 53 Zrzut ekranu 2025-04-10 o 12 15 58
  1. Inspect packages/rn-tester/android/app/build/outputs/mapping/hermesRelease/resources.txt
Before After
@drawable/_reactnative_libraries_newappscreen_components_logo : reachable=false @drawable/_reactnative_libraries_newappscreen_components_logo : reachable=true

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 10, 2025
@cortinico cortinico requested review from huntie and robhogan April 10, 2025 10:33
@cortinico
Copy link
Contributor

@robhogan @huntie can you take this one? I can help on the Android side of things if needed

@facebook-github-bot
Copy link
Contributor

@huntie has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 14, 2025
@facebook-github-bot
Copy link
Contributor

@huntie merged this pull request in 864833f.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @jakex7 in 864833f

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

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

4 participants