-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incompatibility with Custom React Native Integration with Existing Android Apps (Crash on launch) #2719
Comments
Issue validatorThe issue is valid! |
My app is also crashing on Android launch since the update to 2.3.0 from 2.2.4.
|
I also encountered the same issue. |
@t3chcrazy thanks for the reply. I use Proguard but only with this line: -keep class com.facebook.react.turbomodule.** { *; } that is mentioned in the reanimated docu. Will try it with your suggestion. |
This fixed it for me. Tried with reanimated 1.3.1 on android release build. Thanks again! |
Adding -keep to Proguard didn't fixed it for me. I downgraded to 2.2.4. |
This doesn't have anything to do with Proguard. It's an issue that exists whenever the android app uses |
@wfolini do you have any additional info or related tickets you could include? Been seeing issues like this and have had a hard time tracking it down. |
@tswicegood Here's a repo with a small android app to reproduce this issue |
You'll have to edit your Application file and implement ReactApplication class.
And link it in your AndroidManifest.xml file
|
Rn can be used to build libraries too. It might not be desirable / possible for certain apps to implement ReactApplication. |
## Description This PR adds a getter for `ReactInstanceManager`. The motivation for this PR is to fix issues #2719 and #2801 Those Android apps whose application class does not implement `ReactApplication` or simply have a different mechanism for storing a `ReactInstanceManager`, currently they have an incompatibility with `react-native-reanimated` ending in a crash when launching the app, as the issues indicates. Normally, those apps are where [React Native is integrated with existing Android apps](https://reactnative.dev/docs/integration-with-existing-apps) So, introducing this getter allows us to override this getter and implement a custom way to return the `ReactInstanceManager` to be used by `react-native-reanimated`. <!-- Description and motivation for this PR. Inlude Fixes #<number> if this is fixing some issue. Fixes # . --> ## Changes - Added `getReactInstanceManager` method for android. <!-- Please describe things you've changed here, make a **high level** overview, if change is simple you can omit this section. For example: - Added `foo` method which add bouncing animation - Updated `about.md` docs - Added caching in CI builds --> <!-- ## Screenshots / GIFs Here you can add screenshots / GIFs documenting your change. You can add before / after section if you're changing some behavior. ### Before ### After --> ## Test code and steps to reproduce This is an example of how to use and override this getter. It is necessary to manually link `react-native-reanimated` before to be able to do it. ```java public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { private static ReactInstanceManager mReactInstanceManager; ... @OverRide protected void onCreate(Bundle savedInstanceState) { ... List<ReactPackage> packages = new PackageList(getApplication()).getPackages(); // Adding manually Reanimated package here, with overriding getReactInstanceManager method packages.add(new ReanimatedPackage() { @OverRide public ReactInstanceManager getReactInstanceManager(ReactApplicationContext reactContext) { // Implement here your way to get the ReactInstanceManager return MainActivity.getReactInstanceManager(); } }); mReactInstanceManager = ReactInstanceManager.builder() .setApplication(getApplication()) .setCurrentActivity(this) .setBundleAssetName("index.android.bundle") .setJSMainModulePath("index") .setJSIModulesPackage(new ReanimatedJSIModulePackage()) // Adding ReanimatedJSIModulePackage here .addPackages(packages) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build(); ... } ... } ``` <!-- Please include code that can be used to test this change and short description how this example should work. This snippet should be as minimal as possible and ready to be pasted into editor (don't exclude exports or remove "not important" parts of reproduction example) --> ## Checklist - [x] Included code example that can be used to test this change - [ ] Updated TS types - [ ] Added TS types tests - [ ] Added unit / integration tests - [ ] Updated documentation - [ ] Ensured that CI passes
The downgrade is necessary in order to prevent a known issue in Reanimated (software-mansion/react-native-reanimated#2719).
* Bump react-native-gesture-handler to 2.2.0 * Bump react-native-reanimated to 2.4.1 * Update babel config to support reanimated lib * Update Android demo app to support new lib versions * Add patch for react-native-gesture-handler * Add patch for react-native-reanimated Hermes engine has to be manually set because the library is not able to detect it automatically. * Update iOS demo app to support new lib versions - It includes changing the Gutenberg class from NSObject to UIResponder * Add Reanimated library to WPAndroid glue code * Downgrade react-native-reanimated to 2.2.4 The downgrade is necessary in order to prevent a known issue in Reanimated (software-mansion/react-native-reanimated#2719). * Use absolute path for reanimated babel plugin Using the absolute path is required for Reanimated plugin when generating the JS bundle outside Gutenberg, like we do in Gutenberg Mobile. * Update Jest setup to support Reanimated2 * Add google repository to react-native-bridge build This is required to support the react-native-gesture-handler package. * Point dependencies to their forked repository * Update package-lock.json * Remove react-native-reanimated patch * Update react-native-editor Podfile.lock * Update package-lock.json * Silence react-native-gesture-handler warning * Update package-lock.json * Update gesture handler and reanimated package versions * Bump react-native-gesture-handler to 2.2.0-wp-2 * Revert adding google repository to react-native-bridge build * Update react-native-editor changelog * Update Podfile.lock * Bump react-native-gesture-handler to 2.2.0-wp-3 Co-authored-by: Gerardo <[email protected]>
## Description This PR adds a getter for `ReactInstanceManager`. The motivation for this PR is to fix issues software-mansion#2719 and software-mansion#2801 Those Android apps whose application class does not implement `ReactApplication` or simply have a different mechanism for storing a `ReactInstanceManager`, currently they have an incompatibility with `react-native-reanimated` ending in a crash when launching the app, as the issues indicates. Normally, those apps are where [React Native is integrated with existing Android apps](https://reactnative.dev/docs/integration-with-existing-apps) So, introducing this getter allows us to override this getter and implement a custom way to return the `ReactInstanceManager` to be used by `react-native-reanimated`. <!-- Description and motivation for this PR. Inlude Fixes #<number> if this is fixing some issue. Fixes # . --> ## Changes - Added `getReactInstanceManager` method for android. <!-- Please describe things you've changed here, make a **high level** overview, if change is simple you can omit this section. For example: - Added `foo` method which add bouncing animation - Updated `about.md` docs - Added caching in CI builds --> <!-- ## Screenshots / GIFs Here you can add screenshots / GIFs documenting your change. You can add before / after section if you're changing some behavior. ### Before ### After --> ## Test code and steps to reproduce This is an example of how to use and override this getter. It is necessary to manually link `react-native-reanimated` before to be able to do it. ```java public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { private static ReactInstanceManager mReactInstanceManager; ... @OverRide protected void onCreate(Bundle savedInstanceState) { ... List<ReactPackage> packages = new PackageList(getApplication()).getPackages(); // Adding manually Reanimated package here, with overriding getReactInstanceManager method packages.add(new ReanimatedPackage() { @OverRide public ReactInstanceManager getReactInstanceManager(ReactApplicationContext reactContext) { // Implement here your way to get the ReactInstanceManager return MainActivity.getReactInstanceManager(); } }); mReactInstanceManager = ReactInstanceManager.builder() .setApplication(getApplication()) .setCurrentActivity(this) .setBundleAssetName("index.android.bundle") .setJSMainModulePath("index") .setJSIModulesPackage(new ReanimatedJSIModulePackage()) // Adding ReanimatedJSIModulePackage here .addPackages(packages) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build(); ... } ... } ``` <!-- Please include code that can be used to test this change and short description how this example should work. This snippet should be as minimal as possible and ready to be pasted into editor (don't exclude exports or remove "not important" parts of reproduction example) --> ## Checklist - [x] Included code example that can be used to test this change - [ ] Updated TS types - [ ] Added TS types tests - [ ] Added unit / integration tests - [ ] Updated documentation - [ ] Ensured that CI passes
We build a library which exposes UI as RN Fragments, we not control the activity and application and can not force users of the fragment to implement ReactApplication. |
I also encountered the same issue,How to fix this error? |
It works if we change our mainApplication file accordingly, but how do we achieve this by only changing our Activity file ? |
In #2863 I explain the way that you can make it work by applying a different mechanism to infer the ReactInstanceManager instance needed for react-native-reanimated. In this example code below, on public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
private static ReactInstanceManager mReactInstanceManager;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
List<ReactPackage> packages = new PackageList(getApplication()).getPackages();
// Adding manually Reanimated package here, with overriding getReactInstanceManager method
packages.add(new ReanimatedPackage() {
@Override
public ReactInstanceManager getReactInstanceManager(ReactApplicationContext reactContext) {
// Implement here your way to get the ReactInstanceManager
return MainActivity.getReactInstanceManager();
}
});
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setCurrentActivity(this)
.setBundleAssetName("index.android.bundle")
.setJSMainModulePath("index")
.setJSIModulesPackage(new ReanimatedJSIModulePackage()) // Adding ReanimatedJSIModulePackage here
.addPackages(packages)
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
...
}
...
} |
@wfolini Thanks for this. Do you know what should replace the I'm trying to adapt this to work in a React Fragment in a custom integration with new architecture enabled, and it gives this error, which I think is another new instance of this underlying issue (Reanimated doing some brittle under the hood auto-apply magic that assumes the default React Native android native app is being used):
|
What worked for me here was, copying from the Reanimated / Fabric example repo - in particular, its MainActivity) which seems to include some changes since React Native 0.71 that are expected/essential but not mentioned in the out-of-date React Native app integration documentation? The crucial one for me was adding this (this is my Kotlin adaptation of the linked example's Java plus a few tweaks to make it fit): protected open fun createReactActivityDelegate(): ReactActivityDelegate? {
return DefaultReactActivityDelegate(
this as ReactActivity, // My activity class more complex than `extends ReactActivity` so needed `as`
"my-main-component-name", // Taken from the "name" field in `app.json` in react native app root dir
DefaultNewArchitectureEntryPoint.fabricEnabled,
DefaultNewArchitectureEntryPoint.concurrentReactEnabled
)
} This makes builds work, but I'm then getting errors caused by components missing |
I couldn't get this solution to work. Since
I'm trying with Any more advice? |
but i still this issues platform os == android error, but ios work how to fix? import com.facebook.react.ReactActivity class MainActivity : ReactActivity() { /**
/**
help me, please |
@Latropos Why isn't the solution to this part written in the document? |
@wfolini v3 module |
same issue on |
@ko-devHong i temporary fixed this, here is my code:
|
Please reopen the issue @Latropos The mentioned solution is with |
Thank you bro! We are using a hybrid app and I encountered this error when enabling Hermes on the Android side. Then I successfully resolved the issue by overriding getJavaScriptExecutorFactory when creating the Host. class ReactHostManager private constructor() {
//...
private fun createHost(name: String,path: String?): ReactNativeHost {
return CommonHost(name,path)
}
private inner class CommonHost(val name : String,val path: String?) : ReactNativeHost(App.instance) {
//...
override fun getJavaScriptExecutorFactory(): JavaScriptExecutorFactory? {
return HermesExecutorFactory()
}
}
}
|
Description
The last v2.3.0 version introduced an issue that affects Android apps that have a custom React Native integration (see integration-with-existing-apps)
Currently, there is no documentation about how to integrate react-native-reanimated for those kinds of apps. I managed to make it work doing what I show in the code example section, on the main React Native activity.
I was not having any kind of issue with this integration using v2.2.4. After I updated to v2.3.0 I started facing this issue. (See crash on launch capture below)
I also checked where the issue comes from, and as the capture shows, it seems like that it was introduced here.
https://github.com/software-mansion/react-native-reanimated/blob/main/android/src/main/java/com/swmansion/reanimated/ReanimatedPackage.java#L70
It can be seen there that is trying to cast to a
ReactApplication
when instead theReactInstanceManager
builder in this case handles the application context just asApplication
https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java#L155
Expected behavior
Android app should run correctly.
Actual behavior & steps to reproduce
Android app is crashing on launch.
Snack or minimal code example
Example for reproducing this issue
https://github.com/WFolini/custom-rn-with-reanimated
Package versions
Affected platforms
The text was updated successfully, but these errors were encountered: