Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load jsc or hermes lib in static method (#30749)
Summary: Many have reported about the misguiding error `Fatal Exception: java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes.so` even though they don't use Hermes (for example issues #26075 #25923). **The current code does not handle errors correctly when loading JSC or Hermes in `ReactInstanceManagerBuilder`**. **ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java:** ```java try { return new HermesExecutorFactory(); } catch (UnsatisfiedLinkError hermesE) { // We never get here because "new HermesExecutorFactory()" does not throw an exception! hermesE.printStackTrace(); throw jscE; } ``` In Java, when an exception is thrown in static block, it will be RuntimeException and it can't be caught. For example the exception from `SoLoader.loadLibrary` can't be caught and it will crash the app. **ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.java:** ```java static { // Exception from this code block will be RuntimeException and it can't be caught! SoLoader.loadLibrary("hermes"); try { SoLoader.loadLibrary("hermes-executor-debug"); mode_ = "Debug"; } catch (UnsatisfiedLinkError e) { SoLoader.loadLibrary("hermes-executor-release"); mode_ = "Release"; } } ``` This PR fixes the code so that the original exception from failed JSC loading is not swallowed. It does not fix the original issue why JSC loading is failing with some devices, but it can be really helpful to know what the real error is. For example Firebase Crashlytics shows wrong stack trace with current code. I'm sure that this fix could have been written better. It feels wrong to import `JSCExecutor` and `HermesExecutor` in `ReactInstanceManagerBuilder.java`. However, the main point of this PR is to give the idea what is wrong with the current code. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Fixed] - Fix error handling when loading JSC or Hermes Pull Request resolved: #30749 Test Plan: * from this PR, modify `ReactAndroid/src/main/java/com/facebook/react/jscexecutor/JSCExecutor.java` so that JSC loading will fail: ```java // original SoLoader.loadLibrary("jscexecutor"); // changed SoLoader.loadLibrary("jscexecutor-does-not-exist"); ``` * Run `rn-tester` app * Check from Logcat that the app crashed with correct exception and stacktrace. It should **not** be `java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes.so` Tested with Hermes ``` SoLoader.loadLibrary("hermes-executor-test"); ``` Got this one in logcat ``` 09-24 20:12:39.552 6412 6455 E AndroidRuntime: java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes-executor-test.so ``` Reviewed By: cortinico Differential Revision: D30346032 Pulled By: sota000 fbshipit-source-id: 09b032a9e471af233b7ac90b571c311952ab6342
- Loading branch information