react-native: PlatformColor does not work properly on Android

Description

PlatformColor documentation clearly says that if I want to use the values of R.attr in Android, I have to use ?attr/VALUE, but when I use for example ?attr/textColorPrimary, the app crashes on Android or throws an error.

https://developer.android.com/reference/android/R.attr#textColorPrimary

React Native version:

System:
    OS: macOS 10.15.5
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 789.87 MB / 32.00 GB
    Shell: 5.8 - /usr/local/bin/zsh
  Binaries:
    Node: 12.14.1 - /usr/local/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.9.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 23, 26, 27, 28, 29
      Build Tools: 23.0.1, 27.0.3, 28.0.1, 28.0.3, 29.0.2
      System Images: android-25 | Google APIs ARM 64 v8a, android-25 | Google APIs ARM EABI v7a, android-27 | Google Play Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom, android-30 | Google APIs Intel x86 Atom
      Android NDK: 20.0.5594570
  IDEs:
    Android Studio: 4.0 AI-193.6911.18.40.6514223
    Xcode: 11.6/11E708 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_232 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.2 => 0.63.2 
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

  1. Paste this into your code: <Text style={{color: PlatformColor('?attr/textColorPrimary')}}>Test</Text>
  2. Run your code on Android

Expected Results

The app should not crash.

Snack, code example, screenshot, or link to a repository:

image

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

This issue seems to cause by getIdentifier("textColorPrimary", ATRR, packageName) return 0. At here: https://github.com/facebook/react-native/blob/a65cd683aee40fbb0433f43e15b8366dc1fa02a4/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java#L113

This needs further investigation to identify why getIdentifier("textColorPrimary", ATRR, packageName) will return 0. 😢

@safaiyeh exactly. If the error happens on first render, the app crashes completely and exits. I had to film the screen and slow it down to see the error because it exits so fast.

@cortinico Fix for this has been merged. We can close this ticket now. Thanks everyone!

@sallar thanks for the repro. The crash is reproduced on my end.

Two things:

  1. It should not crash when there is an invalid color attribute
  2. textColorPrimary should be valid.

Narrowed it down to this file https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.java

Maybe @tom-un could take a look?

@safaiyeh I’ve created a repo for this. Uncomment line 21-23 of App.js and the app crashes on Android. https://github.com/sallar/PlatformColorBugReproduce

?attr/colorAccent and ?attr/colorPrimary work, but not ?attr/textColorPrimary even though all of these are part of R.attr. Is it because ?attr/textColorPrimary might be a reference to another value?