From 6365df54dbd0a7e185cfe113a9cf0026b2bb12ef Mon Sep 17 00:00:00 2001 From: shubhamguptadream11 Date: Tue, 27 Aug 2024 09:35:27 -0700 Subject: [PATCH] fix(android): resolve crash by handling colour resource IDs with "android" package name fallback (#46202) Summary: Solves this issue: https://github.com/facebook/react-native/issues/29763 ## Changelog: [ANDROID] [ADDED] - Added a conditional check in the `resolveThemeAttribute` function to reattempt resource resolution with the "android" package name if the resource ID is 0. **Reason why app is getting crashes?** The crash was occurring due to an issue with resolving certain color attributes in the Android theme. Specifically, when attempting to resolve attributes like textColorPrimary, the getIdentifier method returned a resource ID of 0, indicating that the resource could not be found. This issue resulted in the resolveThemeAttribute function failing, as it attempted to resolve a non-existent resource ID, which led to a crash. **Key Points:** **Problem**: Resource ID returned as 0 for specific attributes like textColorPrimary. **Cause**: The resource ID of 0 indicates that the attribute was not found in the app's resources. **Impact**: The resolveThemeAttribute function attempted to resolve an invalid resource ID, leading to crash because of this line: https://github.com/facebook/react-native/blob/6cfe51ded006e55617a6f4f2587ca2026306c58d/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java#L227 The introduced fix includes a fallback mechanism to attempt resolution with the "android" package name when the initial lookup returns 0. This helps in correctly resolving theme attributes that might be part of the Android system's default resources, thereby preventing the crash. Pull Request resolved: https://github.com/facebook/react-native/pull/46202 Test Plan: - Tested the app with above colors mentioned. Ensured that app is not getting crashed. Reviewed By: cipolleschi Differential Revision: D61847357 Pulled By: cortinico fbshipit-source-id: 50895a8fd7956e001dbbad9a505ae65151209bd9 --- .../java/com/facebook/react/bridge/ColorPropConverter.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java index 6a0f147bf6b..36416d31866 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java @@ -217,6 +217,11 @@ public class ColorPropConverter { int resourceId = context.getResources().getIdentifier(resourceName, ATTR, packageName); + // If resourceId is 0, try resolving with the android package name + if (resourceId == 0) { + resourceId = context.getResources().getIdentifier(resourceName, ATTR, "android"); + } + TypedValue outValue = new TypedValue(); Resources.Theme theme = context.getTheme();