EventEmitter is not working in Release apk
See original GitHub issueDescription
I’m using the react native’s event emitter to send events from native to JS. This thing is working absolutely fine on my DEBUG version. I’m able to send events with data from native side to JS, but this is not working in the final RELEASE APK!
To further test out things, I changed my build variant to “release” version. To my surprise it works fine even in release variant. The problem is I’m unable to receive native events in the final release build APK! (gradlew assmbleRelease)
React Native version:
System: OS: Windows 10 10.0.19041 CPU: (8) x64 Intel® Core™ i5-8250U CPU @ 1.60GHz Memory: 450.34 MB / 7.86 GB Binaries: Node: 10.16.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: Not Found IDEs: Android Studio: Version 4.0.0.0 AI-193.6911.18.40.6626763 Languages: Java: 13.0.2 Python: 3.8.0 npmPackages: @react-native-community/cli: Not Found react: 16.11.0 => 16.11.0 react-native: 0.62.2 => 0.62.2 npmGlobalPackages: react-native: Not Found
Steps To Reproduce
Provide a detailed list of steps that reproduce the issue.
- Send an event with some data from the native side using event emitter.
- Add listener in JS side and try to fetch the results from this native event.
- Works fine in DEBUG and RELEASE build variants, but now try generating an APK(RELEASE)
We don’t get events data in release APK!
Expected Results
I’m expecting that my listener at the JS side should listen to the event and get data!
Snack, code example, screenshot, or link to a repository:
In Java (Native) side:
SomeActivity.java (onCreate Method)
Basically, I’m trying to send a JSON object from the native side to JS which I’ve already converted to a NativeMap. Also, the reactContext was coming as null in onCreate method, so I added this check to make sure it’s not null.
if(getIntent().hasExtra("pendingNoonReportDoc")) {
ReactInstanceManager reactInstanceManager = getReactNativeHost().getReactInstanceManager();
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
if (reactContext != null) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("hasPendingDoc", pendingNoonReportWritableMap);
} else {
reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext context) {
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("hasPendingDoc", pendingNoonReportWritableMap);
reactInstanceManager.removeReactInstanceEventListener(this);
}
});
}
}
In JS-Side
const eventEmitter = new NativeEventEmitter(NativeModules.ClickNativeEg);
const MyApp = () => {
....
useEffect(() => {
eventEmitter.addListener("hasPendingDoc", (event) => {
console.log(event);
console.log("hasPendingDoc receved event!");
});
}, [eventEmitter]);
....
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8
did anyone manage to find the problem and how to solve it?
If you’re on fire, a desperate solution is to add some delay somewhere before you access context, with
TimeUnit.SECONDS.sleep(1);
. Adding 1 second delay works for me. It’s just a dirty way to resolve the race condition, and to make sure thecontext
is not null.