转自: 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
    March 28, 2015 at 9:00 pm

    Reply

    quisiera utlizar node.is end mi tablet con android.

    • Administrator
      March 29, 2015 at 3:12 pm

      Reply

      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
    April 11, 2015 at 1:50 am

    Reply

    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
      April 11, 2015 at 10:31 am

      Reply

      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
    April 11, 2015 at 10:50 am

    Reply

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

    • Administrator
      April 11, 2015 at 12:54 pm

      Reply

      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. Khotso
    April 23, 2015 at 9:28 pm

    Reply

    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
      April 29, 2015 at 7:03 pm

      Reply

      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
    April 24, 2015 at 4:46 am

    Reply

    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
      April 29, 2015 at 7:04 pm

      Reply

      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
    June 13, 2015 at 1:58 pm

    Reply

    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…

    • Administrator
      June 13, 2015 at 6:53 pm

      Reply

      I looked for your issue, I’m guessing your github ID is oriharel? I didn’t see anything from that user filed at https://github.com/jxcore/jxcore/issues. Where did you ask the JXCore folks this question?

  10. Cristian
    October 28, 2015 at 5:04 am

    Reply

    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
      November 8, 2015 at 5:16 pm

      Reply

      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.

转载于:https://www.cnblogs.com/gssl/p/5626927.html

Building and running Node.js for Android相关推荐

  1. 来自Android客户端什么意思,如何通过回调函数中的Node.js来自Android客户端

    我想从插座Android客户端将数据发送到服务器的Node.js .. 在服务器端做了什么香港专业教育学院:如何通过回调函数中的Node.js来自Android客户端 socket.on('new u ...

  2. dax圣经 翻新_使用翻新和Node JS的Android图像上传

    dax圣经 翻新 In this tutorial, we'll be creating an android application that uploads an image using Retr ...

  3. 构建node.js基础镜像_在Android上构建Node.js应用程序

    构建node.js基础镜像 by Aurélien Giraud 通过AurélienGiraud 在Android上构建Node.js应用程序-第1部分:Termux,Vim和Node.js (Bu ...

  4. node.js书籍_2020年11部最佳Node Js书籍

    node.js书籍 When we talk about any programming language, it's very easy to find any video course on Ud ...

  5. java调用nodejs程序,从java里面调用node.js脚本

    How can I call a node.js inside java and save the console.log values in a String variable? 解决方案 It i ...

  6. node.js从0到0.1

    node.js网络请求与响应 使用原生Http包 //引入模块 var http = require('http');// 返回一个server示例 var server = http.createS ...

  7. android studio使用nodejs本地服务器json数据_使用Node.js的Alexa技巧

    可以使用AlexaLambda函数或RESTAPI端点开发Alexa技能.Lambda函数是Amazon实现AWS中提供的无服务器功能.Amazon建议使用Lambda函数,尽管它们不容易调试.虽然您 ...

  8. bsdiff php,Apk差分升级Android客户端和Node.js服务端实现

    核心的内容是bsdiff和bspatch 源码根目录/bootable/recovery/applypatch下找到,bsdiff官网同样也是可以的,编出来的二进制文件可以在源码根目录out/host ...

  9. mac环境下node.js和phonegap/cordova创建ios和android应用

    mac环境下node.js和phonegap/cordova创建ios和android应用 一介布衣  2015-01-12   nodejs  6888 分享到:QQ空间新浪微博腾讯微博人人网微信 ...

最新文章

  1. Map集合中value()方法与keySet()、entrySet()区别 ——转载
  2. Android中读取NFC标签卡中的ID
  3. Activiti数据库
  4. datagrip启动时报错Internal error
  5. Cookie、session、token对比
  6. 程序员有哪些可以写博客的网站?
  7. 正则表达式 正整数_史上最全的正则表达式 (1) -- 校验数字的表达式
  8. C++之继承探究(十一):多重继承、菱形继承、虚继承、二义性问题及其解决方案
  9. Unity VR成像原理
  10. 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据
  11. 继续教育-职场学习法 试题及答案
  12. 《富爸爸穷爸爸》精髓:穷人思维和富人思维的区别
  13. MAC 设置忽略部分IP代理
  14. linux脚本执行SQL文件创建表,shell脚本执行sql文件chrome安装
  15. SOYO的主板如何进入BIOS系统
  16. 联发科2022暑期实习笔试题
  17. ModuleNotFoundError: No module named ‘_ctypes‘的解决方案
  18. 2022N1叉车司机考试题模拟考试题库及模拟考试
  19. matlab 支点纵波,ABAQUS粘弹性边界及地震荷载施加的简单实现(Matlab生成input文件)...
  20. 2015年度新增开源软件排名TOP100

热门文章

  1. python类的属性和对象属性_在python的类中动态添加属性与生成对象
  2. 提高网站有效流量从三个方面出发
  3. 网络优化404页面没用吗?对于SEO会造成什么影响?
  4. python 会增加内存吗,在Python中增加内存限制?
  5. java中蛇的属性有哪些_学习Java类的属性
  6. 提莫隐身+机器人能钩_航空工业官宣全新歼20正式亮相,可以隐身的变形金刚
  7. 为什么TCP建立连接需要三次握手
  8. python 中super方法的调用
  9. python 写创建和追加一个文件
  10. cordova切换到安卓5.1.0,Android SDK not found