转自: http://www.goland.org/nodejsonandroid/

Building and running Node.js for Android

October 14, 2014 / By Administrator / In Thali / 19 Comments

The good news is that Node.js does run on Android. The bad news is that at least at the time I’m writing this the build process requires a few extra steps. Nothing too scary though. See below for details.

1 Building Node.js for Android

  1. Go find a Linux machine or maybe a Mac.
    1. These instructions don’t currently work on Windows due to issues with the sh scripts being used. Yes, I did try the scripts in MINGW32 and no it didn’t work.
  2. Go download the Android NDK.
    1. Which NDK to download does take a bit of attention. Most Android devices today are 32 bit so I want the Platform (32-bit target). But my Linux OS (Elementary OS) is 64 bit so I want Linux 64-bit (x86) under Platform (32-bit target).
  3. After downloading the NDK unzip it.
    1. Let’s assume you put the NDK into ~/android-ndk-r10b.
  4. Go clone node.
    1. Let’s assume you put that into ~/node. I am running these instructions off master branch.
  5. Check that you have all of node’s dependencies as listed here
    1. I believe any modern Linux distro will have all of these already but just in case I decided to include the link.
  6. Go edit ~/node/android-configure and change ’arm-linux-androideabi-4.7’ to instead be ’arm-linux-androideabi-4.8.
    1. This is the pull request that added basic Android support to Node. It contains some instructions. The first instruction will set up the build environment for Android. But the set up script is designed for an older version of the Android NDK. So we need to update it. Specifically 4.7 is apparently not supported by NDK 10 so I switched it to 4.8 which is. I decided to leave platform=android-9 for no particularly good reason.
  7. Run from inside of ~/node directory the command “source ./android-configure ~/android-ndk-r10b”
  8. Now go to ~/node/android-toolchain/bin and issue the command “mv python2.7 oldpython2.7 && ln -s /usr/bin/python2.7 python2.7”
    1. The NDK appears to ship with its own version of Python 2.7 that doesn’t support a library (bz2) that is needed by files in the NDK. In any sane world this just means that the NDK is broken but I’m sure there is some logic here. This bug was reported to Node (since it breaks Node’s support of Android) but they responded that this is an NDK issue so Google should deal with it. But if we want to build we have to get connected to a version of Python that does support bz2. That’s what we did above. We linked the main version of Python (which any sane Linux distro will use) with the NDK so it will use that and hence support bz2.
  9. Now go to ~/node and issue ’make’
    1. The actual instructions from the checkin say to run ’make -j8’ which enables parallel capabilities in Make. Apparently the rule of thumb is to set the value after j to 2x the number of hardware threads available on the machine.
    2. On my Linux VM the build takes forever so now is a good time to get some fresh tea.

2 Using Node.js on Android via ADB

Eventually I’ll write up an AAR that just wraps all the Node stuff and provides a standard API for launching node and feeding it a script. But that isn’t my current priority so instead I need to just get node onto my device and play with it.
  1. Issue the command “adb push ~/node/out/Release /data/local/tmp/Release”
    1. There is a step I’m skipping here. I actually do my development on Windows. So I copy the Release folder from my Linux VM (via Virtualbox) and then use the linked drive to move it to my Windows box. So in fact my adb push command above isn’t from the Linux location but my Windows location.
    2. The out/Release folder contains all the build artifacts for Node. Of this mess I suspect only the node executable is actually needed. But for the moment I’m going to play it safe and just move everything over.
    3. The reason for putting the node materials into /data/local/tmp/Release is because /data/local/tmp is one of the few areas where we can execute the chmod command in the next step and make Node executable. But when we wrap this thing up in an AAR we can actually use the setExecutable function instead.
  2. Issue “adb shell”. Once in the shell issue “chmod 700 /data/local/tmp/Release/node”
  3. I then issued an ’adb push’ for a simple hello world node program I have that I put inside of /data/local/tmp
    1. I used “Hello HTTP” from http://howtonode.org/hello-node
  4. Then I went in via “adb shell” and ran “/data/local/tmp/Release/node helloworld.js”
    1. And yes, it worked! I even tested it by going to the browser on the phone and navigating to http://localhost:8000.
  5. To kill things I just ctrl-c which does kill the adb shell but also the node app. Good enough for now.

3 What about NPM?

In theory one should be able to use NPM on the Linux box and then just move the whole thing over to Android and run it there. But this only works if none of the dependencies use an add-on. An add-on requires compiling C code into a form Android can handle. It looks like NPM wants to support making this happen but so far I haven’t found the right voodoo. So I’m still investigating.

19 Responses to Building and running Node.js for Android

    1. Pingback: Making HTML 6 Packaged Apps Happen | Stuff Yaron Finds Interesting

    2. Pingback: Looking at JXCore’s perf |

    3.  carlos

      quisiera utlizar node.is end mi tablet con android.

      •  Administrator

        If Bing Translate is handling things correctly then you want to use node.js with an Android tablet. The instructions I posted should work just fine with an Android tablet. But at this point I would take a look at JXCore. They run node just fine on Android and are more mature than the instructions I provided.

    4. Pingback: raspberry pi 에서 iBeacon 찾기 관련 글 | 토니네@제주온라인

    5.  Ismael Olea

      Great great post.

      FYI: I’ve realised this method doesn’t work in Android 5.* due a broken ABI, so I had to research how to solve it. I finally got it running. This is written here: http://olea.org/diario/archive/2015/Abr.-11-1.html

      Thanks!

      •  Administrator

        Yup, I wrote these instructions using KitKat so I’m not surprised that the PIE fun broke things on 5.0. Thanks for documenting the fix. Right now we are mostly focusing on just using JXCore and letting them figure it all out for us. :)

    6.  Ismael Olea

      BTW, any experience on populating npm dependencies for running node.js apps in Android? I’m getting a little bit mad with this.

      •  Administrator

        It depends on the dependencies. If you are using pure Javascript dependencies (which as I’ve previously explored is most of them) then you can just run NPM on the parent OS and copy the node_module sub-directory into /data/local/tmp. If you are running node.js from there then it should just work (for non-native add-ons).

        But seriously, give JXCore a try. They have solved a bunch of these issues and are completely open source.

    7. Has there been a solution for Building and running Node.js for Android using Windows?

      1. Go find a Linux machine or maybe a Mac.
      These instructions don’t currently work on Windows due to issues with
      the sh scripts being used. Yes, I did try the scripts in MINGW32 and no
      it didn’t work.

      •  
        Administrator

        I have to admit that since discovering JXCore I haven’t really followed up on this approach. I’d rather they deal with it. :)

    8.  
      Ritesh Kumar

      Hi
      I am trying to create and run node.js app in android and following the
      steps as discussed by you in this post but i am facing the issue
      “Node.js configure error: No acceptable C compiler found!
      Please make sure you have a C compiler installed on your system and/or
      consider adjusting the CC environment variable if you installed
      it in a non-standard prefix.”
      when i am trying to run this command “source ./android-configure ~/android-ndk-r10b”.
      I installed all the required software :-
      * GCC 4.2 or newer
      * G++ 4.2 or newer
      * Python 2.6 or 2.7
      * GNU Make 3.81 or newer
      * libexecinfo (FreeBSD and OpenBSD only)

      Please let me know what can be the issue for this.Thanks in advance.

      •  
        Administrator

        I vaguely remember running into this as
        well. I think node.js wanted a very specific version of C. What OS are
        running on? You listed libexecinfo so are you on FreeBSD or OpenBSD?

    9.  
      Ori

      Have you succeeded initializing the
      JXcore engine in 1 thread but then calling a JS funciton from another?
      I’m having difficulties accomplish this and they are not answering…

    10.  
      Cristian

      Hi! I’m wondering if it’s any chance to have this native library compiled in Android for node:
      https://github.com/voodootikigod/node-serialport

      •  
        Administrator

        I never could figure out how to get
        node-gyp to compile to Android. I eventually gave up when we moved
        completely to JXCore. If node-serialport will compile with JXCore on the
        desktop (make sure that pre-build is off or you will get the wrong
        binary) then you have some hope of getting it to work with JXCore on
        Android. No guarantees, it depends on if the C libraries it is using are
        supported on Android.

Building and running Node.js for Android的更多相关文章

  1. [Server Running] [Node.js, PM2] Using PM2 To Keep Your Node Apps Alive

    PM2 is a production process manager for Node.js applications with a built-in load balancer. It allow ...

  2. 转 node.js和 android中java加密解密一致性问题;

    原文地址,请大家去原文博客了解; http://blog.csdn.net/linminqin/article/details/19972751 我保留一份,防止删除: var crypto = re ...

  3. [转]Node.js tutorial in Visual Studio Code

    本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...

  4. 在Window IIS中安装运行node.js应用—你疯了吗

    [原文发表地址]Installing and Running node.js applications within IIS on Windows - Are you mad? [原文发表时间]201 ...

  5. 一个完整的Node.js RESTful API

    前言 这篇文章算是对Building APIs with Node.js这本书的一个总结.用Node.js写接口对我来说是很有用的,比如在项目初始阶段,可以快速的模拟网络请求.正因为它用js写的,跟i ...

  6. how to updating Node.js and npm

    how to updating  Node.js and npm 1 Installing Node.js and updating npm How do I update Node.js ? Not ...

  7. [转]Building a REST-Backend for Angular with Node.js & Express

    本文转自:https://malcoded.com/posts/angular-backend-express Angular is a single page application framewo ...

  8. [译]Node.js : Building RESTful APIs using Loopback and MySQL

    国庆后可能就要使用StrongLoop那套东西来做项目了 原文:http://www.javabeat.net/loopback-mysql/ Loopback是什么? Loopback是一个开源的N ...

  9. Practical Node.js (2018版) 第8章:Building Node.js REST API Servers

    Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...

随机推荐

  1. xml布局内容总结(一)--Android

    关于安卓项目中xml的使用非常多.为了达到一些好的UI效果.须要对xml比較熟练.会使用非常多的小技巧,本人准备对这些小技巧进行整理和总结,希望进行分享和交流. 关于weight的使用,因为weigh ...

  2. 分享一个基于 Node.js 的 Web 开发框架 - Nokitjs

    简介 Nokit 是一个简单易用的基于 Nodejs 的 Web 开发框架,默认提供了 MVC / NSP / RESTful 等支持,并提供对应项目模板.管理工具. 资源 GitHub https: ...

  3. js 操作select和option常用代码整理

    1.获取选中select的value和text,html代码如下: <select id="mySelect"> <option value="1&qu ...

  4. Python学习(七)面向对象 ——封装

    Python 类的封装 承接上一节,学了Student类的定义及实例化,每个实例都拥有各自的name和score.现在若需要打印一个学生的成绩,可定义函数 print_score() 该函数为类外的函 ...

  5. 查找链表中倒数第k个结点

    题目:输入一个单向链表,输出该链表中倒数第k个结点.链表的倒数第0个结点为链表的尾指针.链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNex ...

  6. hello--GAN

    GAN系列学习(1)——前生今世 DCGAN.WGAN.WGAN-GP.LSGAN.BEGAN原理总结及对比 [Learning Notes]变分自编码器(Variational Auto-Encod ...

  7. Android -- 压缩与解压文件

    我在做一个项目中,工程文件中有一个功能需要很多图片,图片与app一起打包下来的话有30+M,那么我们就考虑另外下载压缩包,我们将图片取出,工程就只有4+M了,哈哈哈哈,呵呵,真恐怖.那么这样就涉及到另 ...

  8. 一个异常org.apache.jasper.JasperException: java.lang.IllegalStateException: No output folder:的解决

    今天对一个WebApp做完修改,导出成war包,再发布到Tomcat7中,居然访问不了了! 同样的问题一周前也出现过,后来一顿鼓捣,又莫名其妙好了,当时认为是Tomcat7闹点小毛病,也没多想. 但是 ...

  9. static对象

    1.static对象包括: global对象 namespace作用域下的对象 在class,function,file作用域内的static对象 2.在方法内的static对象成为 local st ...

  10. UNIX网络编程读书笔记:套接口地址结构

    前言 大多数套接口函数都需要一个指向套接口地址结构的指针作为参数.每个协议族都定义它自己的套接口地址结构.这些结构的名字均以"sockaddr_"开头,并以对应每个协议族的唯一后缀 ...