There are a bunch of old SO threads dealing with running NodeJS on Android. Most of these are no longer viable (JXCore) and/or provide confusing, outdated, incomplete, or erroneous information.
Therefore I have investigated what seems to be currently (as of August 2017) viable approaches and found three likely candidates.
To decide between them I would like to know:
Viable approaches are:
Besides that I have found a number of related interesting resources:
js receives a CPU bound task: Whenever a heavy request comes to the event loop, Node. js would set all the CPU available to process it first, and then answer other requests queued. That results in slow processing and overall delay in the event loop, which is why Node. js is not recommended for heavy computation.
You can use Node. js for Mobile Apps. It works on Android devices and simulators, with pre-built binaries for armeabi-v7a, x86, arm64-v8a, x86_64. It also works on iOS, though that's outside the scope of this question.
The short answer is “NO.” The long answer is, “NO, it's not dead, and it probably will never die.” Node. js is just as relevant to coding in 2021 and beyond, even if the hype around it has stabilized slightly.
Node. js, introduced back in 2009, is not one of these. Node. js development has become very popular over the last four years and continues to stand the competition in 2022 making startups worldwide choose it over other available options.
[NOTE This answer contains findings that were in the original question]
I have investigated the various options a bit more and here are some preliminary findings.
Each of the options uses some form of NodeJS compiled for Android. But to use any option you would probably want to compile to different Node, Android and architecture (x86, ARM, ARM64, etc.) versions.
This is problematic. NodeJS has an android-configure
script, but this results in errors in most combinations I've tried. I created a number of github issues for a working build script. In this issue results are collected:
To summarize:
libnode.a
) statically linked in libj2v8.so
works for 7.x up to 7.9.0
One interesting workaround was used by @mafintosh: transfer Node to device using Termux and do the compilation there (needs much space and time, but works).
J2V8 is a set of Java bindings for V8. J2V8 focuses on performance and tight integration with V8. [...] [which] forces a more static type system between the JS and Java code, but it also improves the performance since intermediate Objects are not created. [...]
Building J2V8 requires building both the native parts and the Java library (.jar/.aar file). To build the native parts we first build node.js as a library and then statically link J2V8 to that. [...]
For cross-compiling J2V8 uses Docker (android, linux, windows) and Vagrant (macos).
See slideshare: Running NodeJS in a Java World (or see InfoQ video, 32min.)
Features:
Characteristics:
build_system/build_settings.py
Start a build simply with python build.py --interactive
, select build:
[0] Docker >> android-x86 >> NODE_ENABLED [1] Docker >> android-arm >> NODE_ENABLED [2] Docker >> alpine-linux-x64 >> NODE_ENABLED [3] Docker >> linux-x64 >> NODE_ENABLED [4] Docker >> linux-x86 >> NODE_ENABLED [5] Vagrant >> macosx-x64 >> NODE_ENABLED [6] Vagrant >> macosx-x86 >> NODE_ENABLED [7] Native >> windows-x64 >> NODE_ENABLED [8] Docker >> windows-x64 >> NODE_ENABLED [9] Vagrant >> windows-x64 >> NODE_ENABLED
Select build steps (or all
):
NodeJS --> CMake --> JNI --> Optimize --> Java/Android --> JUnit
Compiles V8 as shared library libj2v8_{platform}_{abi}.{ext}
nodejs
build step cannot build Node shared library (errors), creates static libnode.a
to be linked in libj2v8.so
.aar
to include as project dependencyPros:
Cons:
Node on android works by running your Node.js inside the android app using a shared library. It then bundles a
WebView
that hosts your UI code. All UI is just classic html/css/js.In the node app you can require
node-on-android
to get access to the WebView. You can use this to load an html page in theWebView
.
According to node-on-android
creator (@mafintosh) this is easier and better than J2V8 as it compiles V8 directly as the real thing.
Features:
Characteristics:
app
project: app/src/main/include/node
with node .h
headersapp/src/main/jniLibs/arm64-v8a
with libc++_shared.so
and libnode.so
app/src/main/cpp
with native-lib.cpp
(includes node.h
)Service
with node running in a separate threadlibnode.so
, so private native void startNode(String... app);
shows as error in IDE (but compiles)android/app/src/main/assets/node
loadUrl
function node-on-android
Pros:
Cons:
arm64
architecture (full mobile support planned, or DIY build) Run a real Node.js process in the background, behind a React Native app.
Using this package you can: run http servers in Android, use Node streams, interface with the filesystem, offload some heavy processing out of the JS thread in React Native, and more! Running the real Node.js in Android, you can do everything that Node.js on desktop can.
Features:
Characteristics:
Service
with Node on separate thread) node
is compiled/used as application, not an embedded shared lib{projectRoot}/background
/android/src/main/res/raw/bin_node_v710
RNNode
is available in RN by importing react-native-node
react-native-node
also contains CLI that transfers Node code at build timeexpress
server on http://localhost:5000
at Node sidePros:
Cons:
7.1.0
version (but DIY build newer ones)My goal is React Native + NodeJS. This is the status of my activities:
libc++
)react-native-node
does compile, but does not operate despite many triesnode-on-android
works, but node-only app development and 64-bit incompatible with RNI decided to combine react-native-node
with J2V8
because of:
.aar
to be easily included in GradleReact Native 0.46.4
+ NodeJS 7.9.0
is now working! See:
My use case: fat client with P2P decentralized networking
I am thinking of a CQRS (command-query-responsibility-segregation) design:
Details: Realm.io to bridge native NodeJS + React Native in Android fat client app (CQRS-style)
Even after years of people trying to port NodeJS to Android there are still no real good solutions, it is pioneering.
Expect many hurdles and errors as you set up your project and build environment, but once setup you could enjoy the full power of Node on your phone.
As of today (March 2018), there is another viable alternative not yet listed in the current answers: Node.js for Mobile Apps.
At its core, the project provides a native library for embedding Node.js into native Android and iOS applications; but it also comes with plugins for React Native and Cordova.
Pre-built binaries for the library are available for Android armeabi-v7a, x86, arm64-v8a, x86_64, and for iOS 64-bit.
The core library is a fork of nodejs/node-chakracore, which in turn is fork of nodejs/node. The Android version is pretty much regular Node.js built as a library, with a few portability fixes. The iOS version uses the ChakraCore engine instead of V8 (replacing V8 with ChakraCore is possible thanks to the changes in the nodejs/node-chakracore fork).
The React Native and Cordova plugins make it easier to add Node.js to applications built using those frameworks. The Node.js code runs in a separate engine and thread than the framework's (React Native / Cordova). Communication between the two JavaScript worlds is achieved via a messaging bridge provided by the plugins.
More information, including some documentation, is available on the project website.
(Full disclosure: I work for the company that develops Node.js for Mobile Apps.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With