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

查看:48
本文介绍了在 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:

  • 这些方法之间的主要区别
  • 每种方法的具体优点和缺点
  • 可能的障碍、挑战和缺点
  • 您知道其他可行的替代方案吗?

可行的方法是:

  1. 运行包含 NodeJS 的 V8 javascript 引擎(J2V8)
  2. 直接使用 NodeJS,作为原生库嵌入(node-on-android)
  3. 将 React Native 与 NodeJS 应用即服务相结合(react-native-node)

除此之外,我还发现了许多相关的有趣资源:

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

  • NPM 直接使用 Termux 安装 NodeJS,无需 root(不适用于最终用户)
  • LiquidCore - 原生移动微应用 devenv(未调查,有趣的概念)
  • dna2oslab - 有用于节点可执行文件的工作 NodeJS 构建脚本
  • 为 Android 构建 NodeJS - 包含有用编译技巧的博客和 示例项目
  • 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:

总结:

  • 共享库构建全部失败(除非在您的 android 上进行物理构建,请参见下文)
  • J2V8 with 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).

参见幻灯片:在 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 开始构建,选择构建:

    • Specify the versions to compile in 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
    

  • 选择构建步骤(或all):

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

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

    • 注意:nodejs构建步骤无法构建Node共享库(错误),创建静态libnode.a以在中链接libj2v8.so
    • Note: nodejs build step cannot build Node shared library (errors), creates static libnode.a to be linked in libj2v8.so

    优点:

    • 相对活跃的项目
    • 优质代码,包括 Java 单元测试
    • 为您的应用设计工具包添加 Java 的全部功能
    • 出色、直观的构建系统(完成后)

    缺点:

    • 很少,大部分是过时的使用文档
      • 特别是在大型 (r) 规模 JS 项目中的使用没有记录
      • 有些 PR 挂了 2 年,甚至没有得到回应.不好

      Android 上的 Node 通过使用共享库在 android 应用程序中运行您的 Node.js 来工作.然后它捆绑了一个 WebView 来承载你的 UI 代码.所有 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 带有 libc++_shared.solibnode.so
        • app/src/main/cppnative-lib.cpp(包括 node.h)
        • Java 代码,只需启动一个 Service,节点在单独的线程中运行
        • 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

          优点:

          • 简单的项目,没有太多的管道代码
          • 随附开箱即用的最新 v8.x Node 版本
          • 简单的基于 HTML 的应用 UI 编程(例如使用 choo)
          • 开箱即用:)

          缺点:

          • 非常新的项目,只有实验性代码
          • 仅适用于 arm64 架构(计划提供全面的移动支持,或 DIY 构建)
            • 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)!

              在后台运行一个真实的 Node.js 进程,在 React Native 应用程序后面.

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

              使用此包,您可以:在 Android 中运行 http 服务器、使用 Node 流、与文件系统接口、从 React Native 中的 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.

              特点:

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

              特点:

              • 源自NodeBase
              • 非常类似于 node-on-android(在单独的线程上使用 Node 运行 Service)
                • 但是 node 被编译/用作应用程序,而不是嵌入式共享库
                • NodeJS 应用代码位于 {projectRoot}/background
                • NodeJS 可执行文件位于 /android/src/main/res/raw/bin_node_v710
                • 在构建时,Node 应用程序被压缩,解压到 `/android/src/main/res/raw/{appName}
                • NodeJS 服务就像从命令行运行一样被调用,传递参数
                • 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
                  • 在 Node 端的 http://localhost:5000 上运行 express 服务器
                  • Running an express server on http://localhost:5000 at Node side

                  优点:

                  • 简单的项目,没有太多的管道代码
                  • 显而易见:Android 上的 NodeJS 支持 React Native!
                  • Node-as-executable 可能适用于 64 位设备 + react-native

                  缺点:

                  • 非常新的项目,只有实验性代码
                  • 带有旧的 NodeJS 7.1.0 版本(但 DIY 构建 较新的)
                  • 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 版本编译为可执行文件
                      • 使用新的 J2V8 构建系统编译 NodeJS v7.4.0 到 v7.9.0
                      • 编译 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-nodeJ2V8 结合起来,因为:

                      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:

                      我的用例:具有 P2P 去中心化网络的胖客户端

                      My use case: fat client with P2P decentralized networking

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

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

                      • react-native UI 是根据从节点服务查询的视图构建的
                      • react-native UI 操作触发节点后台服务上的命令
                      • 后台服务处理网络消息、传入命令、触发事件
                      • 事件存储在 Realm DB 中,形成了前端和后端之间的桥梁

                      详细信息: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天全站免登陆