在Android上运行NodeJS的可行选项(2017年8月) [英] Viable options for running NodeJS on Android (Aug 2017)

查看:177
本文介绍了在Android上运行NodeJS的可行选项(2017年8月)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多旧的SO线程处理在Android上运行NodeJS。其中大部分都不再可行(JXCore)和/或提供令人困惑,过时,不完整或错误的信息。

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.

因此我调查了目前似乎是什么(如2017年8月)可行的方法,并找到了三个可能的候选人。

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:


  • 这些方法之间的主要区别

  • 每种方法的特定专业人员和骗局

  • 可能出现障碍,挑战和缺点

  • 您知道其他可行的替代方案吗?

  • the primary differences between these approaches
  • specific pro's and con's on each approach
  • likely hurdles, challenges and shortcomings
  • do you know of other viable alternatives?

可行方法


  1. 运行包含NodeJS的V8 javascript引擎( J2V8

  2. 直接使用NodeJS,作为本机库嵌入( node-on-android

  3. 将React Native与NodeJS app-as-a-service结合使用( react-native-node

  1. Running V8 javascript engine which includes NodeJS (J2V8)
  2. Use NodeJS directly, embedded as native library (node-on-android)
  3. Combining React Native with NodeJS app-as-a-service (react-native-node)

除此之外,我找到了许多相关的有趣资源:

Besides that I have found a number of related interesting resources:


  • NPM直接使用Termux安装NodeJS而不需要生根(不适用于最终用户)

  • LiquidCore - 原生移动微应用程序devenv(未调查,有趣的概念)

  • dna2oslab - 有一个有效的NodeJS版本节点可执行文件的脚本

  • 构建NodeJS for Android - 包含有用编译技巧的博客和示例项目

  • NPM install NodeJS directly using Termux without rooting (not for end-users)
  • LiquidCore - native mobile micro-app devenv (not investigated, interesting concepts)
  • dna2oslab - has a working NodeJS build script for node executables
  • Building NodeJS for Android - blog with useful compilation tips and example project

推荐答案

调查可行的选项



[ 注意这个答案包含原始问题中的调查结果]

Investigating viable 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.

每个选项都使用为Android编译的某种形式的NodeJS。但是要使用任何选项,您可能希望编译到不同的Node,Android和架构(x86,ARM,ARM64等)版本。

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.

这是有问题的。 NodeJS有一个 android-configure 脚本,但这会导致我尝试过的大多数组合出错。我为工作的构建脚本创建了许多github问题。在此问题中收集结果:

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:

  • Working build script for Android ARM Node 7.x or 8.x shared library

To总结:


  • 共享库构建全部失败(除非在你的android上实际构建,见下文)

  • J2V8与NodeJS( libnode.a )静态链接在 libj2v8.so 适用于7.x至 7.9.0

  • build-as-node-executable适用于7.x(使用 dna2oslab 构建脚本)

  • shared library builds all fail (except when building physically on your android, see below)
  • J2V8 with NodeJS (libnode.a) statically linked in libj2v8.so works for 7.x up to 7.9.0
  • build-as-node-executable works for 7.x (using dna2oslab build script)

@mafintosh 使用了一个有趣的解决方法:使用节点转移到设备Termux和在那里编辑(需要很多空间和时间,但有效)。

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是V8的一组Java绑定。 J2V8专注于性能和与V8的紧密集成。 [...] [which]强制在JS和Java代码之间建立一个更静态的类型系统,但它也提高了性能,因为没有创建中间对象。 [...]

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. [...]

构建J2V8需要构建本机部分和Java库(.jar / .aar文件)。要构建本机部分,我们首先将node.js构建为库,然后将J2V8静态链接到该库。 [...]

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. [...]

对于交叉编译,J2V8使用Docker(android,linux,windows)和Vagrant(macos)。

For cross-compiling J2V8 uses Docker (android, linux, windows) and Vagrant (macos).

参见slideshare:在Java世界中运行NodeJS (或参见 InfoQ视频, 32分钟。)

See slideshare: Running NodeJS in a Java World (or see InfoQ video, 32min.)

功能


  • 替换具有更强大的v8(带有NodeJS)的JavaScriptCore引擎

  • 通过添加的J2V8 JNI / Java层支持多线程(线程/工作者)


    • 每个线程都可以有自己的隔离V8实例

    特征


    • 指定要在 build_system / build_settings.py中编译的版本

    • 使用启动构建python build.py --interactive ,选择构建:

    [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
    


  • 选择构建步骤(或全部):

    NodeJS --> CMake --> JNI --> Optimize --> Java/Android --> JUnit
    


  • 将V8编译为共享库 libj2v8_ {platform} _ { abi}。{ext}


    • 注意 nodejs 构建步骤无法构建节点共享库(错误),创建要在 libj2v8.so中链接的静态 libnode.a

    • Note: nodejs build step cannot build Node shared library (errors), creates static libnode.a to be linked in libj2v8.so

    优点


    • 相对活跃的项目

    • 包括Java单元测试在内的高质量代码

    • 为您的Java添加全部功能应用程序设计工具包

    • 优秀,直观的构建系统(一旦完成)

    • Relatively active project
    • Good quality code including Java unit tests
    • Adds full power of Java to your app design toolkit
    • Great, intuitive build system (once finished)

    缺点


    • 很少,大部分过时的使用文档ent


      • 特别是未记录的用于大型(r)规模的JS项目


      • 有些公关人员连续两年都没有回复。不好


      Android上的节点通过使用共享库在Android应用程序内运行Node.js来工作。然后它捆绑一个托管你的UI代码的 WebView 。所有UI都只是经典的html / css / js。

      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.

      在节点应用程序中,您可以要求 node-on-android 访问WebView。您可以使用它来加载 WebView 中的html页面。

      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 the WebView.

      根据到 node-on-android 创建者( @mafintosh )这比J2V8更容易和更好,因为它直接编译V8 作为真实的东西

      According to node-on-android creator (@mafintosh) this is easier and better than J2V8 as it compiles V8 directly as the real thing.

      功能


      • 构建完整的NodeJS应用程序,包括UI(通过本机WebView)

      特征


      • gradle中的相关目录/文件 app 项目:


        • app / src / main / include / node 节点 .h 标题

        • app / src / main / jniLibs / arm64-v8a with libc ++ _ shared.so libnode.so

        • app / src / main / cpp with native-lib.cpp (包括节点。 h

        • Java代码,只需旋转 S ervice 节点在一个单独的线程中运行

        • Relevant directories / files in gradle app project:
          • app/src/main/include/node with node .h headers
          • app/src/main/jniLibs/arm64-v8a with libc++_shared.so and libnode.so
          • app/src/main/cpp with native-lib.cpp (includes node.h)
          • Java code, just spins up a Service with node running in a separate thread

          • 可通过NPM包访问的节点服务 node-on-android

          • Node service accessible via NPM package node-on-android

          优点


          • 简单的项目,没有多少管道代码

          • 附带最新的v8.x节点版本开箱即用

          • 基于HTML的简单应用程序UI编程(例如使用 choo

          • 开箱即用: )

          • Simple project, not much plumbing code
          • Comes with a recent v8.x Node version out-of-the-box
          • Simple HTML-based app UI programming (e.g. using choo)
          • Works out-of-the-box :)

          缺点


          • 非常新的项目,只有实验代码

          • 仅适用于 arm64 架构(计划提供完整的移动支持,或DIY) build)

            • Very new project, only experimental code still
            • Comes just for arm64 architecture (full mobile support planned, or DIY build)
              • Note: 64-bit cannot be combined with React Native (no 64-bit support)!

              在React Native应用程序后面的后台运行真正的 Node.js进程。

              Run a real Node.js process in the background, behind a React Native app.

              使用这个软件包你可以:在Android中运行http服务器,使用节点流,与文件系统接口,从React中的JS线程卸载一些繁重的处理原生,还有更多!在Android中运行真正的Node.js,您可以完成桌面上Node.js的所有操作。

              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.

              功能


              • 使用React Native作为UI,NodeJS作为后台服务

              特征


              • 来自 NodeBase

              • 非常类似于node-on-android(运行服务在单独的线程上使用Node)


                • 但是 node 被编译/使用为应用程序,而不是嵌入式共享库

                • NodeJS应用程序代码位于 {projectRoot} / background

                • NodeJS可执行文件位于 / android / src / main / res / raw / bin_node_v710

                • 在构建时节点应用程序是tarballed ,解压缩在`/ android / src / main / res / raw / {appName}

                • 调用NodeJS服务,好像从命令行运行,传递args

                • Derived from NodeBase
                • Very similar to node-on-android (run Service with Node on separate thread)
                  • But node is compiled/used as application, not an embedded shared lib
                  • NodeJS app code is located in {projectRoot}/background
                  • NodeJS executable is in /android/src/main/res/raw/bin_node_v710
                  • At build time Node app is tarballed, unpacked at `/android/src/main/res/raw/{appName}
                  • NodeJS service is invoked as if run from the command-line, passing args

                  • react-native-node 还包含传递Node的CLI构建时的代码

                  • react-native-node also contains CLI that transfers Node code at build time

                  • 在节点端 http:// localhost:5000 上运行 express 服务器

                  • Running an express server on http://localhost:5000 at Node side

                  优点


                  • 简单项目,管道代码不多

                  • 明显:在Android上使用NodeJS反应Native支持!

                  • Node-as-executable可能适用于 64位设备+ react-native

                  • Simple project, not much plumbing code
                  • Obvious: React Native support with NodeJS on android!
                  • Node-as-executable will probably work with 64-bit devices + react-native

                  缺点


                  • 非常新项目,只有实验代码仍然

                  • 附带旧的NodeJS 7.1.0 版本(但是 DIY build 新的)

                  • RN和Node应用之间没有简单的通信方式(基于REST)


                    • 需要扩展REST API或滚动您自己的机制

                    • Very new project, only experimental code still
                    • Comes with old NodeJS 7.1.0 version (but DIY build newer ones)
                    • No easy way to communicate between RN and Node apps (REST-based)
                      • Need to extend REST API or roll your own mechanism

                      我的目标是React Native + NodeJS。这是我的活动状态:

                      My goal is React Native + NodeJS. This is the status of my activities:


                      • 将NodeJS v7.x版本编译为可执行文件

                      • 编译NodeJS v7.4.0至v7.9.0适用于新的J2V8构建系统

                      • 编译NodeJS v8.1.2将很快与J2v8一起使用(针对 libc ++编译

                      • react-native-node 确实编译,但尽管尝试了很多次但仍无法运行

                      • node-on-android 有效,但仅限节点的应用开发和64位与RN不兼容

                      • Compiling NodeJS v7.x versions as executable works
                      • Compiling NodeJS v7.4.0 up to v7.9.0 works with new J2V8 build system
                      • Compiling NodeJS v8.1.2 will soon work with J2v8 (compiled against libc++)
                      • react-native-node does compile, but does not operate despite many tries
                      • node-on-android works, but node-only app development and 64-bit incompatible with RN

                      我决定将 react-native-node J2V8 结合使用,因为:

                      I decided to combine react-native-node with J2V8 because of:

                      • Great cross-compile build PR: https://github.com/eclipsesource/J2V8/pull/327
                      • Builds into a nice J2V8 .aar to be easily included in Gradle

                      React Native 0.46.4 + NodeJS 7.9.0 现在工作!请参阅:

                      React Native 0.46.4 + NodeJS 7.9.0 is now working! See:

                      • https://github.com/staltz/react-native-node/issues/5#issuecomment-323049897

                      我的使用案例: 使用P2P分散式网络的胖客户端

                      My use case: fat client with P2P decentralized networking

                      我正在考虑CQRS(命令查询 - 责任 - 隔离)设计:

                      I am thinking of a CQRS (command-query-responsibility-segregation) design:


                      • 反应 - 本机UI是从节点服务查询的视图构建的。

                      • react-native UI actions触发节点后台服务上的命令

                      • 后台服务处理网络消息,传入命令,触发事件

                      • 事件存储在Realm DB中,形成前后桥梁

                      • react-native UI is constructed from view queried from the node service
                      • react-native UI actions trigger commands on the node background service
                      • background service processes network messages, incoming commands, triggers events
                      • events are stored in Realm DB that forms the bridge between front and back

                      详细信息: Realm.io在Android胖客户端应用程序(CQRS样式)中桥接本机NodeJS + React Native

                      即使多年后人们试图将NodeJS移植到Android那里仍然没有真正好的解决方案,它是开拓性的。

                      Even after years of people trying to port NodeJS to Android there are still no real good solutions, it is pioneering.

                      在设置项目和构建环境时会遇到许多障碍和错误,但一旦设置完毕,您就可以在手机上享受Node的全部功能。

                      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.

                      这篇关于在Android上运行NodeJS的可行选项(2017年8月)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆