cli icon indicating copy to clipboard operation
cli copied to clipboard

run-android not working

Open dougg0k opened this issue 5 months ago • 1 comments

Environment

info Fetching system and libraries information...
System:
  OS: Linux 6.15 Arch Linux
  CPU: (16) x64 AMD Ryzen 7 5800X3D 8-Core Processor
  Memory: 19.00 GB / 31.24 GB
  Shell:
    version: 0.104.1
    path: /home/user/.cargo/bin/nu
Binaries:
  Node:
    version: 22.16.0
    path: ~/.local/share/mise/installs/node/22.16.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.local/share/mise/installs/yarn/1.22.22/bin/yarn
  npm:
    version: 10.9.2
    path: ~/.local/share/mise/installs/node/22.16.0/bin/npm
  Watchman:
    version: 20250525.093350.0
    path: /usr/bin/watchman
SDKs:
  Android SDK:
    API Levels:
      - "35"
      - "36"
    Build Tools:
      - 35.0.1
      - 36.0.0
    System Images:
      - android-36 | Google APIs Intel x86_64 Atom
    Android NDK: 27.2.1247901
IDEs:
  Android Studio: AI-243.26053.27.2432.13536105
Languages:
  Java:
    version: 21.0.2
    path: /home/user/.local/share/mise/installs/java/21.0.2/bin/javac
  Ruby:
    version: 3.4.4
    path: /home/user/.local/share/mise/installs/ruby/3.4.4/bin/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 18.0.0
    wanted: ^18.0.0
  react:
    installed: 19.1.0
    wanted: 19.1.0
  react-native:
    installed: 0.79.3
    wanted: 0.79.3
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Emulator is not open and jdk version 21 should still work.

Edit: I just tried with JDK 20, same thing. Just didnt get the error in the react-native doctor output.

❯ pnpm dlx react-native doctor                                                                                                       12:29:29
Common
 ✓ Node.js - Required to execute JavaScript code
 ✓ yarn - Required to install NPM dependencies
 ✓ npm - Required to install NPM dependencies
 ✓ Metro - Required for bundling the JavaScript code

Android
 ✖ Adb - No devices and/or emulators connected. Please create emulator with Android Studio or connect Android device.
 ✖ JDK - Required to compile Java code
   - Version found: 21.0.2
   - Version supported: >= 17 <= 20
 ✓ Android Studio - Required for building and installing your app on Android
 ✓ ANDROID_HOME - Environment variable that points to your Android SDK installation
 ✓ Gradlew - Build tool required for Android builds
 ✓ Android SDK - Required for building and installing your app on Android

Errors:   2
Warnings: 0

Description

I recently did a major upgrade to latest RN version in a somewhat older project. And I noticed in this system, when running react-native run-android, it hangs, it shows no message, no error.

I tried finding the cause, and nothing. I then tried in a empty project with npx @react-native-community/cli@latest init and the same behavior occured.

After that, I tried in a project bootstrapped by ignite-cli, which brought expo as the runner, even though I choose bare workflow. And that worked.

I have the project setup with pnpm. If I try with npm, it shows some logs, but nothing relevant. Even in the .log file generated, there is nothing indicating anything. I also tried with --verbose, same result.

I tried the CLI version 17,18 and 19. Same result.

✗ ll android/gradlew                                                                                
Octal Permissions Links Size User    Group   Date Modified Git Name
0755  .rwxr-xr-x      1 8,8k user user 10 jun 10:11   N-  android/gradlew*

If I run the command ps aux | grep react-native, this is the result.

user    28519  0.7  0.3 11657204 117296 pts/1 Sl+  12:35   0:00 node /mnt/NVME500GB/projects/project_app/node_modules/.bin/../.pnpm/[email protected]_@[email protected]_@[email protected]_typescript@5._131331164c62c60ee148ac328a34e83f/node_modules/react-native/cli.js run-android --verbose
user    28542  1.3  0.3 11777064 126228 pts/1 Sl+  12:35   0:00 node /mnt/NVME500GB/projects/project_app/node_modules/react-native/cli.js start --port 8081

I tried run-ios on linux, which is obviously not supported, it went through.

> react-native run-ios

✔ Installing Ruby Gems
...

Where android

> react-native run-android --verbose


How to get this working and/or better debug it? I'd rather use it instead of expo-cli, in non-expo project.

Reproducible Demo

I mean, run react-native run-android --verbose in a project from scratch. Since it gave the same behavior.

dougg0k avatar Jun 10 '25 15:06 dougg0k

I did some progress.

I found that by including --no-packager, the prompt continued.

Then another error happened. This being in the empty project bootstraped by the init command.

> Included build '/some_project/node_modules/@react-native/gradle-plugin' does not exist.

Even though I saw that there was a '@react-native/[email protected]': in the pnpm-lock.yaml, it didnt make a difference.

I then installed the package manually, since it didnt come included directly into devDependencies. And it continued.


It went through, it opened the emulator and installed the app there.

After, I had to use the start manually.

dougg0k avatar Jun 10 '25 16:06 dougg0k

There hasn't been any activity on this issue in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

github-actions[bot] avatar Sep 09 '25 03:09 github-actions[bot]

That stale action does not make sense. Since there was no answer.

dougg0k avatar Sep 09 '25 04:09 dougg0k

Emulator is not open and jdk version 21 should still work.

I haven't read your whole ticket but the setup docs on the RN website recommed JDK 17 and state you may have issues with newer versions:

React Native currently recommends version 17 of the Java SE Development Kit (JDK). You may encounter problems using higher JDK versions. You may download and install OpenJDK from AdoptOpenJDK or your system packager.

Have you confirmed it's also failing on 17?

liamjones avatar Sep 11 '25 13:09 liamjones

It does work fine with version 21, like I mentioned in the comment, as long as I include --no-packager and run react-native start as a second command.

Where just run-android seem to run both at the same time. So, should be unrelated to java version.

dougg0k avatar Sep 11 '25 13:09 dougg0k

I'm also using Arch Linux and getting this error. I'm using JDK 17. The start and --no-package workaround works for me as well. But just npx react-native run-android hangs eternally. Adding the verbose flag does not log anything. doctor says nothing is wrong.

Unit2795 avatar Nov 10 '25 08:11 Unit2795

I did a quick check, they even added a warning about the issue saying to be related to async, in the fn that probably does the packager run.

Many of the packages seem outdated, including the one who executes commands (execa).

I see a bunch of PRs ignored, I dont even see the point in trying to contribute. Same for issues dating back 2019.

dougg0k avatar Nov 10 '25 12:11 dougg0k

I end up checking and may have found the reason, what shell are you using @Unit2795 ?

Because it works fine in bash, so it might be shell related. They seem to export env variables and execute commands bash/sh based, that could be why. In this case, I am using nushell and it doesnt work the same.


I found where, at least here, the issue is the exported variables, as it works differently in nushell. https://github.com/react-native-community/cli/blob/main/packages/cli-tools/src/startServerInNewWindow.ts#L34 where they generate the .packager.env file.

node_modules/.generated/.packager.env

For nushell, it would be something like.

export-env { $env.RCT_METRO_PORT = 8081; $env.PROJECT_ROOT = "/app"; $env.REACT_NATIVE_PATH = "/app/node_modules/react-native" }

https://www.nushell.sh/commands/docs/export-env.html


Actually I understood a bit more about the issue, it's not that by exporting like that in nushell would make it work.

When I tested in bash, it worked and it generated a bunch of files in the node_modules/.generated folder, then when I tried again on nushell, it worked, probably not because of the env vars, it was because of the files were already generated in that folder.

Where when the folder were empty and the command hanged on nushell, only two files were generated. Even if I tried exporting the vars in nushell before running the command.

❯ ll node_modules/.generated/
Octal Permissions Links Size User    Group   Date Modified Git Name
0644  .rw-r--r--      1  229 user user 10 nov 12:44   -I  .packager.env
0755  .rwxr-xr-x      1  324 user user 10 nov 12:44   -I  launchPackager.command*

I checked after a reboot.

So the issue is something else. But could still be related to the shell.


After trying to replicate again, it didnt work with nothing that I mentioned, not even in bash, it doesnt make any sense. But I will leave the message as some feedback.

Perhaps was due to some race condition when running the CLI.

dougg0k avatar Nov 10 '25 15:11 dougg0k

I'm using bash.

This may be related:

  • https://github.com/facebook/react-native/issues/28807?

Might be more issues like this. They seem to go stale and just wind up closed.

The problematic code seems to be here: https://github.com/react-native-community/cli/blob/dc848fd90842beb68e92f6ab7c9a00458a151b03/packages/cli-tools/src/getDefaultUserTerminal.ts#L3-L4

Which is then used here: https://github.com/react-native-community/cli/blob/dc848fd90842beb68e92f6ab7c9a00458a151b03/packages/cli-platform-android/src/commands/runAndroid/index.ts#L319-L322

The default value of TERM in many terminal emulators seems to be xterm-256color, this can be checked with echo $TERM.

As a workaround, these also work (replace konsole with your desired terminal):

  • npx react-native run-android --terminal konsole
  • TERM=konsole npx react-native run-android

These aren't ideal. For me, the emulator doesn't start until I press "a" in the metro bundler terminal window.

Unit2795 avatar Nov 11 '25 01:11 Unit2795

This is my best temp fix for now, I placed the following envvar in my ~/.bashrc (which my ~/.profile also pulls in).

# Fix for "react-native run-android" command hanging indefinitely
# Set this to your desired terminal
export REACT_TERMINAL=konsole

Regarding some investigation, I confirmed Metro is running despite nothing happening in my terminal when running npx react-native run-android

[david@desktop ~]$ ss -lntp | grep ':8081'
LISTEN 0      511                     *:8081             *:*    users:(("MainThread",pid=156919,fd=22))

and

[david@desktop ~]$ pgrep -fa 'metro.*start|react-native.*start|node.*metro'
156919 node /home/david/Desktop/Repos/rn-test/node_modules/react-native/cli.js start --port 8081

I'm just not seeing the output in Arch. I noticed when I run react-native run-android on Ubuntu, it works. Though I don't ever see the Metro window. But I do see the build proceed as normal and the emulator pops up.

I'm noticing that this code doesn't ever actually hit the try block for me either (in Ubuntu OR Arch), it always catches, and so always calls sh: https://github.com/react-native-community/cli/blob/dc848fd90842beb68e92f6ab7c9a00458a151b03/packages/cli-tools/src/startServerInNewWindow.ts#L111-L119

Is the TERM envvar being misused in this library? It seems more like it's a description of terminal capabilities. Not something that points to an executable. But this library seems to treat it like something that can be invoked. (https://www.baeldung.com/linux/term-environment-variable) Though maybe it's just used as a last resort, and "sh" is actually what's intended to be used?

On Arch Linux, sh points to bash:

readlink -f /bin/sh

On Ubuntu it points to dash

I can confirm the same successful behavior mentioned above that I saw on Ubuntu with Arch by installing dash and pointing sh to it.

sudo pacman -S dash
sudo ln -sf /usr/bin/dash /usr/bin/sh
# Can revert this after
sudo ln -sf /usr/bin/bash /usr/bin/sh
# Can also remove dash
sudo pacman -Rns dash

So it's something to do with the sh symlink to bash.

Another interesting thing I noticed, this also worked for me

# Start sh first in your react-native project directory
sh
# Then start react native
npx react-native run-android

It starts in a new window for me then and works fine, though the emulator doesn't start automatically, same as when I set REACT_TERMINAL=konsole

I'll try some other things eventually when work slows down. Hard for me to say if this is user error (I'm newer to Arch), Arch weirdness, or React Native issues. Maybe a combo of all three! But I'd like to find a solution so I can develop my React Native code on Arch.

Unit2795 avatar Nov 11 '25 05:11 Unit2795

Here is the error when it tries.

> react-native run-android

error Error: Command failed with ENOENT: xterm-256color -e sh /app/node_modules/.generated/launchPackager.command
spawnSync xterm-256color ENOENT

Because they return TERM rather than TERM_PROGRAM or getting the actual shell https://github.com/react-native-community/cli/blob/main/packages/cli-tools/src/getDefaultUserTerminal.ts#L18


I opened a PR too hastly, it didnt fix it completely, it only made it move forward, it didnt actually finalized the starting of the server, as I didnt test in a project without issues.

Regardless, I've used a way to not have an additional terminal open (start server) or having to set a variable.

I've used this devDep https://github.com/open-cli-tools/concurrently to run both commands. Only issue is that there is no interactivity with the start server through the terminal.

"concurrently 'react-native run-android --no-packager' 'react-native start --reset-cache'"

There are two flags that may help, but at least here it didnt seem to work. --handle-input --default-input-target 1

[0] INFO Interactive mode is not supported in this environment


A nice CLI fix would be to have both running in the same level, while keeping the shortcuts always below the output, if ever interacted with it, it would then appear in the output. Both would work in the same panel/window.

dougg0k avatar Nov 12 '25 14:11 dougg0k