How do I open react-native's dev menu on real device
Asked Answered
L

8

65

I have seen already a number of border cases and strange developer interface.

From the "shake your device", which is really impractical, specially with a tablet

To working around Android studio to simulate a button press.

Is there a consistent way to do it? Can't we use a intern API to have a debug button within our app to launch such menu as navigator.popUpDevMenu()?

And if not, how do you shake your tablet to get it working. This is intended to solve How To configure HMR on a real device, too. React native does improve the development experience, but I'd say that particular thing slows it a little bit.

Lithe answered 15/6, 2016 at 15:27 Comment(0)
E
119

If you are on a mac, there is a handy tool called Frappe. https://github.com/niftylettuce/frappe

You can use this command from the shell

adb shell input keyevent 82

if haven't run react-native run-android or if the device gets disconnected after you have run the react-native run-android. You need to re-enable the development server port. You can run this command and try again with the previous command

adb reverse tcp:8081 tcp:8081

EDIT: this solution only works for android devices and is among the hacks proposed in the question above. So it is improvable. However It is selected as valid answer until this happens.

Extenuatory answered 15/6, 2016 at 18:9 Comment(9)
These are the kind of workarounds, I mention. They are more refined than those I portrayed, though.Lithe
Frappe does not work on mac devices (ipads, iphones)Lithe
This command also works on Windows 10 and Android 6 Marshmallow.Genni
" adb shell input keyevent 82 " This helped me . Thanks alot !!Jp
This doesn't work for me. I'm using Xiaomi Redmi pro 3 with 5.1.1. The keyevent only bringing up the list of running applications. Any help ?Trescott
Have you tried this ? github.com/facebook/react-native/issues/…Extenuatory
Same for me, my Xiaomi doesn't do anything with adb shell input keyevent 82 :(Hyperextension
this no longer works on newer Android versions (not sure from which starts not working) the most solid solution for this is the answer by @Yogesh AgrawalAcrostic
shake the device, the menu should appearCyclotron
S
39

Here is what I do:

Android:

Add a script to your package.json:

"android-shake": "adb shell input keyevent 82"

You will then be able to call yarn android-shake to have the menu popup on Android (as long as the device is connected to the computer that is).

iOS

Settings -> Accessibility -> AssistiveTouch

  1. Turn it on.
  2. Tap on Customize Top Level Menu...
  3. Remove all icons but one, and set it to shake.

That will give you a button that you can tap instead of shaking the device.

enter image description here

I hope this can help others.

Sparrow answered 22/11, 2018 at 18:23 Comment(1)
Thanks, The IOs AssistiveTouch tip, really helpful.Vexation
L
9

I add a simple absolutely positioned button and this onClick handler to it

imoprt { NativeModules } from 'react-native';
...
onMenuButtonClick(){
 NativeModules.DevMenu.show();
}
Laevogyrate answered 26/4, 2019 at 12:30 Comment(2)
That is actually a good idea. You can also add the button only on DEV so that it is usable only then.Sparrow
In RN 0.66, this module is removed. There are only several available APIs provided by NativeModules.DevSettings.Doting
G
8

I've created a lib that allows you to use 3 fingers touch instead of shake to open dev menu, when in development mode

https://github.com/pie6k/react-native-dev-menu-on-touch

You only have to wrap your app inside:

import DevMenuOnTouch from 'react-native-dev-menu-on-touch';
// or:  import { DevMenuOnTouch } from 'react-native-dev-menu-on-touch'

class YourRootApp extends Component {
  render() {
    return (
      <DevMenuOnTouch>
        <YourApp />
      </DevMenuOnTouch>
    );
  }
}

It's really useful when you have to debug on real device and you have co-workers sitting next to you.

Grisaille answered 21/1, 2019 at 11:12 Comment(0)
K
6

On windows Just click on metro bundler and press 'D' for developer menu, 'R' to reload

Koeppel answered 19/7, 2021 at 1:51 Comment(1)
By far the easiest and simplest solution! This should be on top.Vitrify
H
3

In the case you have a Xiaomi phone, the given answer will not work, because there is a security option prevented popup window to open and you need to allow it for your app :

Go to Settings > Installed apps > [Your App Name] > Permission manager, and enable “Display pop-up window”.

Shake again. The developer menu should pop up as expected with just a little shaking.

Source: https://matthewphiong.com/debugging-react-native-app-on-a-xiaomi-phone

Hyperextension answered 26/6, 2018 at 6:29 Comment(4)
#51036905 please answer my question in that linkOfay
@Mr_Perfect your question is not related to react-native but reactjs.Hyperextension
I want some response. No one is replying. I got stuck :(Ofay
Come one @Mr_Perfect, it's just about an hour you opened your question :). Actually you have already comments and an answer.Hyperextension
F
3

Shaking phone in Android is annoying sometimes. Making below changes will open the Developer Menu by pressing the Volume down / Up key.

Insert the following code in android/../MainActivity.java

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && this.getReactInstanceManager() != null) {
          this.getReactInstanceManager().showDevOptionsDialog();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

With React Native Navigation V3 following code will work in MainActivity.java.

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && this.getReactGateway().getReactNativeHost().getReactInstanceManager() != null) {
          this.getReactGateway().getReactNativeHost().getReactInstanceManager().showDevOptionsDialog();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

After this change, run the app again to deploy the above code by react-native run-android.

Tested with RN 0.59+.

Reference: https://facebook.github.io/react-native/docs/integration-with-existing-apps

Edit: Include KeyEvent import as well with other imports in the file.

import android.view.KeyEvent;

Fleur answered 30/12, 2019 at 7:4 Comment(1)
adb shell input keyevent 82 not working on newer Android versions (not sure which one) so this is the best solution for android, not loving to add code only for development purpose but its not so bad and seems as the only solid solution. Should be the selected answerAcrostic
T
0

If it not works with all solutions look in to MainActivity.java and remove or comment out import com.facebook.react.buildconfig

Twain answered 13/4, 2023 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.