76

I'd like to manually force a Reload of my React Native app on demand without physically shaking the device. (I'm getting carpal tunnel.)

I know that Live Reload / Hot Reload are available. I'm looking for a on-demand command line solution.

brettlaforge
  • 2,971
  • 2
  • 16
  • 13

8 Answers8

192

Using the cmd line you can send a command to the Android device.

adb shell input text "RR"

This command tells the Android device to type the character "R" twice which is the React Native command to Reload on Android.

The adb shell command has many useful features many of which are described here:

ADB Shell Input Events

To open the developer menu:

adb shell input keyevent 82
Yang
  • 13
  • 2
brettlaforge
  • 2,971
  • 2
  • 16
  • 13
  • 4
    For anyone who is having issues with the first command (in cases like a text input which cannot be unfocused), an alternative is `adb shell "input keyevent 82 && input keyevent 66 && input keyevent 66"`. It manually hits 'Reload' in the developer menu – Jonny Jul 10 '19 at 19:31
  • Incase you are running 2 devices at the same time and it doesn't let you run any `adb` commands you can simply do: `adb -s shell input text "RR"` – Rondev Nov 04 '20 at 12:10
26

Just posting it here in case you didn't know this trick

long press the menu button in your android device. Then you'll get this menu

tap the reload option and you are good to go

Virb
  • 1,639
  • 1
  • 16
  • 25
vighnesh s
  • 269
  • 3
  • 3
15

I use the following command. It doesn't reload the app, but it brings up the developper menu on the device, so I can then press the "Reload" option:

adb shell input keyevent KEYCODE_MENU

I develop with a real device (not the emulator) and sending the "double-R" through adb doesn't work (it just shows the keyboard and types 2 Rs).

Xavier
  • 441
  • 3
  • 7
  • 2
    That's interesting, because I develop on a real device too and the "double-R" does reload the app. If the keyboard is already open it will print "RR", but if the keyboard is closed it will reload. Maybe my answer was too general and doesn't work on all devices? – brettlaforge Oct 20 '17 at 19:33
  • @brettlaforge I'm a newb at native but I might guess that certain apps may be greedy about keypresses, not just different devices. Your answer's working for me on Android so thanks! – Reed Spool Mar 09 '18 at 03:46
15

Add the following script to your package.json:

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

Then you will be able to call

yarn android-shake

If you are looking for ios then checkout my answer on this link

Francois Nadeau
  • 7,023
  • 2
  • 49
  • 58
5

One trick would be to add this command on ~/.bashrc profile in the case you're using unix.

  1. use your favorite editor (ex: nano on Ubuntu) and type nano ~/.bashrc
  2. on the end of file write alias rnreload='adb shell input text "RR"'
  3. save it and run source ~/.bashrc in order to active it.
  4. Now whenever you need, just type rnreload on a terminal.

Next time you enter your computer it should be already done.

Also, there's the possibility to add an other alias as well: alias rnshake='adb shell input keyevent 82' which "shakes" android. You can use it to access other commands like Hot Reloading, Debugger, Inspector, etc.

Filipe
  • 149
  • 1
  • 7
1

Made an autohotkey script to reload and open the menu with keyboard shortcuts.

   ^+r::  run, %comspec% /c adb shell input text "RR",,hide  
   ^+e::  run, %comspec% /c adb shell input keyevent 82,,hide

ctrl+shift+r to reload ctrl+shift+e to open dev menu

1

If you're on a Mac and using Hammerspoon, you can put the following bit of code in your ~/.hammerspoon/init.lua file:

hyper = {'ctrl', 'alt', 'cmd'}
placid = {'ctrl', 'cmd'}

-- React native reload JS on connected device
hs.hotkey.bind(placid, 'R', function()
  os.execute('/Users/peter/Library/Android/sdk/platform-tools/adb shell input text "RR"')
end)

-- React native show dev menu on connected device
hs.hotkey.bind(hyper, 'R', function()
  os.execute('/Users/peter/Library/Android/sdk/platform-tools/adb shell input keyevent 82')
end)

the os.execute command doesn't load your ENV (doing so would make running commands really slow), so make sure to run which adb in your terminal to figure out what the exact path to adb is. (in my case it was /Users/peter/Library/Android/sdk/platform-tools/adb).

Now you can reload your app using ctrl+cmd+R and show the dev menu using ctrl+option+cmd+R from anywhere and without even bothering to cmd-tab out of your favorite editor!

-1

For device you have just to shake your device than a menu appears so click on Reload

Asma_Kh
  • 93
  • 2
  • 15