9/06/2018

Get string values from Android strings.xml in React Native

Problem

I am working on a mobile app project that contains multiple app builds in React Native. Whenever I build a specific app, the string called "app_name" inside my

/android/app/src/main/res/values/strings.xml 

keeps on changing. I intended to do this so that the native part of my code knows what app I was building.

My goal here is to get the value of the string "app_name" so that can be able to render a specific asset(s) or use a specific java function intended for that app.

Solution

I have my strings.xml looks like this:


All I did was create a String function and inside that function I use getResources() and getString() to get the string value of app_name inside strings.xml;

Here is the code:


Using this approach, I am now able to pass the values inside strings.xml to my react native app. I was able to compare the value of the function appName() using appName().equals("appone") that return either true if match or false if not match.

Hope it helps.