'DeviceEventEmitter.addListener not working in Release apk in react native

I'm using RCTDeviceEventEmitter.class to send events from android native code to react native but DeviceEventEmitter.addListener is not listening in Release Apk. It is working perfectly fine in Debug Apk. I tested this on multiple devices. I'm using react-native v.0.66.3

private void sendEvent(String eventName, WritableMap params) {
        Log.v("Events", "DEBUG: Sending event from android to react" + ' ' + eventName + " " + params.toString());

        ReactInstanceManager reactInstanceManager = getReactNativeHost().getReactInstanceManager();
        ReactContext reactContext = reactInstanceManager.getCurrentReactContext();

        if(reactContext != null) {
            Log.v("Events", "DEBUG: not null" + reactContext);
            reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
        } else {
            Log.v("Events", "DEBUG: null: adding listened" + reactContext);
            reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
                @Override
                public void onReactContextInitialized(ReactContext context) {
                    Log.v("Events", "DEBUG: inside listener" + context);
                    context
                        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                        .emit(eventName, params);
                    reactInstanceManager.removeReactInstanceEventListener(this);
                }
            });
        }
    }

And in React Native app.js file

DeviceEventEmitter.addListener('accept', (event) => {
      //Do something
    });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source