11
22package com .csath ;
33
4- import java .lang .reflect .*;
4+ import android .content .Context ;
5+ import android .content .res .Resources ;
6+ import android .util .Log ;
7+
8+ import java .lang .ClassNotFoundException ;
9+ import java .lang .IllegalAccessException ;
10+ import java .lang .reflect .Field ;
11+
512import com .facebook .react .bridge .ReactApplicationContext ;
613import com .facebook .react .bridge .ReactContextBaseJavaModule ;
714import com .facebook .react .bridge .ReactMethod ;
1320import java .util .HashMap ;
1421
1522public class RNConfigReaderModule extends ReactContextBaseJavaModule {
16-
17- private final ReactApplicationContext reactContext ;
18- private final Class buildConfigClass ;
19-
20- public RNConfigReaderModule (ReactApplicationContext reactContext , Class buildConfigClass ) {
23+ public RNConfigReaderModule (ReactApplicationContext reactContext ) {
2124 super (reactContext );
22- this .reactContext = reactContext ;
23- this .buildConfigClass = buildConfigClass ;
2425 }
2526
2627 @ Override
@@ -31,20 +32,28 @@ public String getName() {
3132 @ Override
3233 public Map <String , Object > getConstants () {
3334 final Map <String , Object > constants = new HashMap <>();
34- Field [] fields = this .buildConfigClass .getDeclaredFields ();
35- for (Field f : fields ) {
36- if (Modifier .isStatic (f .getModifiers ())) {
37- Object value = null ;
38- try {
39- value = f .get (null );
40- }
41- catch (Exception e ){
42-
43- }
44- finally {
45- constants .put (f .getName (), value );
46- }
47- }
35+ try {
36+ Context context = getReactApplicationContext ();
37+ int resId = context .getResources ().getIdentifier ("rn_config_reader_custom_package" , "string" , context .getPackageName ());
38+ String className ;
39+ try {
40+ className = context .getString (resId );
41+ } catch (Resources .NotFoundException e ) {
42+ className = getReactApplicationContext ().getApplicationContext ().getPackageName ();
43+ }
44+ Class clazz = Class .forName (className + ".BuildConfig" );
45+ Field [] fields = clazz .getDeclaredFields ();
46+ for (Field f : fields ) {
47+ try {
48+ constants .put (f .getName (), f .get (null ));
49+ }
50+ catch (IllegalAccessException e ) {
51+ Log .d ("ReactNative" , "RNConfigReader: Could not access BuildConfig field " + f .getName ());
52+ }
53+ }
54+ }
55+ catch (ClassNotFoundException e ) {
56+ Log .d ("ReactNative" , "RNConfigReader: Could not find BuildConfig class" );
4857 }
4958 return constants ;
5059 }
0 commit comments