question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] Unable to recreateReactContextInBackground

See original GitHub issue

Hey guys,

Description

There is an issue with recreateReactContextInBackground() method after upgrading React Native version from 0.44.3 to 0.45.1:

06-08 13:19:29.400  2262  2262 E AndroidRuntime: FATAL EXCEPTION: main
06-08 13:19:29.400  2262  2262 E AndroidRuntime: Process: com.testapp_rn, PID: 2262
06-08 13:19:29.400  2262  2262 E AndroidRuntime: com.facebook.react.uimanager.IllegalViewOperationException: Trying to add a root view with an explicit id already set. React Native uses the id field to track react tags and will overwrite this field. If that is fine, explicitly overwrite the id field to View.NO_ID before calling addMeasuredRootView.
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.uimanager.NativeViewHierarchyManager.addRootViewGroup(NativeViewHierarchyManager.java:504)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.uimanager.NativeViewHierarchyManager.addRootView(NativeViewHierarchyManager.java:496)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.uimanager.UIViewOperationQueue.addRootView(UIViewOperationQueue.java:570)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.uimanager.UIImplementation.registerRootView(UIImplementation.java:129)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.uimanager.UIManagerModule.addMeasuredRootView(UIManagerModule.java:210)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager.attachMeasuredRootViewToInstance(ReactInstanceManager.java:861)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager.setupReactContext(ReactInstanceManager.java:816)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager.access$1100(ReactInstanceManager.java:109)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager$4$2.run(ReactInstanceManager.java:771)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:739)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:95)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:148)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:5417)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
06-08 13:19:29.400  2262  2262 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-08 13:19:29.401   652   784 W ActivityManager:   Force finishing activity com.testapp_rn/.MainActivity
06-08 13:19:29.516   346   346 E EGL_emulation: tid 346: eglCreateSyncKHR(1215): error 0x3004 (EGL_BAD_ATTRIBUTE)

I have RN project that uses CodePush SDK and I want to call CodePush.restartApp method, but I’m receiving the error above. To restart an app (reload js bundle) CodePush.restartApp method uses loadBundle method and loadBundle method uses recreateReactContextInBackground() to recreate context and load new js bundle: restartApp -> loadBundle -> recreateReactContextInBackground

I’m expecting that react context will be recreated successfully without no errors.

Reproduction Steps

  1. unzip archive that I’ve provided below
  2. npm i
  3. gradlew assembleDebug (I’m using bundleInDebug: true)
  4. install apk
  5. open app
  6. click Restart App

Sample Code

Here is app sample. rncp210release.zip

Source code:

import CodePush from "react-native-code-push";
...
restartApp() {
    CodePush.restartApp(false);
}
...
<TouchableOpacity onPress={this.restartApp.bind(this)}>
    <Text style={styles.syncButton}>Restart App</Text>
</TouchableOpacity>

Additional Information

  • React Native version: 0.45.1
  • Platform: Android
  • Development Operating System: Windows
  • Build tools: Android Studio 2.3.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:17

github_iconTop GitHub Comments

3reactions
sergey-akhalkovcommented, Jun 23, 2017

Hey guys, I’ve figured out what goes wrong here - we need to perform the following commands on attached root views in instanceManager:

reactRootView.removeAllViews();
reactRootView.setId(View.NO_ID);

The details are here: https://github.com/Microsoft/react-native-code-push/pull/901/files#diff-99865b122cb5308ab005ac99ccb55da0R185

So could someone please tell me why those lines that fixes the exception has been removed from RN 0.45? - I’m going to create PR that will fix it in RN itself, but good to know what was wrong with those lines and why they have been removed.

1reaction
gentleecommented, Feb 28, 2018

Still have this issue:

com.facebook.react.uimanager.IllegalViewOperationException: Trying to add a root view with an explicit id already set. React Native uses the id field to track react tags and will overwrite this field. If that is fine, explicitly overwrite the id field to View.NO_ID before calling addRootView.
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.uimanager.NativeViewHierarchyManager.addRootViewGroup(NativeViewHierarchyManager.java:510)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.uimanager.NativeViewHierarchyManager.addRootView(NativeViewHierarchyManager.java:502)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.uimanager.UIViewOperationQueue.addRootView(UIViewOperationQueue.java:605)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.uimanager.UIImplementation.registerRootView(UIImplementation.java:211)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.uimanager.UIManagerModule.addRootView(UIManagerModule.java:302)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.ReactInstanceManager.attachRootViewToInstance(ReactInstanceManager.java:1021)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.ReactInstanceManager.setupReactContext(ReactInstanceManager.java:978)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.ReactInstanceManager.access$1300(ReactInstanceManager.java:113)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.ReactInstanceManager$5$2.run(ReactInstanceManager.java:941)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at android.os.Handler.handleCallback(Handler.java:790)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at android.os.Looper.loop(Looper.java:164)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
02-27 17:55:33.392 11588 11711 E unknown:ReactNative: 	at java.lang.Thread.run(Thread.java:764)

Using react-native-restart library that calls recreateReactContextInBackground, for changing app’s language.

RN 0.53.3 (and 0.51.0)

UPDATE: This was probably because it was called multiple times, so fixed it by calling once. But still this is a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to generate signed apk with react native
By following command mentioned on documentation, I'm trying to generate signed apk for android and getting following error
Read more >
React Native on Android Troubleshooting - Tilo Mitra
When trying to run a React Native Android application on a real device, you may get the following error: Unable to download JS...
Read more >
Stuck on upgrading sdk 24 for android - #7 by dooboolab
I've just succeed in running my android app with sdk 24 and below error appears. ' log file is large so I can't...
Read more >
rnmapbox/Lobby - Gitter
Whenever I'm trying to run the command react-native run-android I'm getting ... 18:23:18.060 8608 8608 W unknown:ReactNative: Unable to dispatch touch to JS ......
Read more >
Dealing With Problems With Building the App & Detox
Execution failed for task ':app:processReleaseAndroidTestResources'. > A failure occurred while executing com.android.build.gradle.internal.res.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found