Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NodeJS in Android using J2V8

I have created code for android using J2V8 library for executing nodejs script in android mobile. but it gives me error when i run application.

Gradle dependencies

compile 'com.eclipsesource.j2v8:j2v8:4.6.0@aar'

Code

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_console);
    runScript();
}

private void runScript() {
    NodeJS nodeJS = NodeJS.createNodeJS();

    try {
        File script = createTempScript("console.log(\"Hello NodeJS\")");

        nodeJS.exec(script);

        script.delete();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        nodeJS.release();
    }

}

private File createTempScript(String script) throws IOException {
    File file = File.createTempFile("temp",".js", getCacheDir());
    FileWriter fileWriter = new FileWriter(file);
    fileWriter.write(script);
    fileWriter.close();
    return file;
}

...

Error

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.asissuthar.lion/in.asissuthar.lion.ConsoleActivity}: java.lang.UnsupportedOperationException:
StartNodeJS Not Supported.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5345)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)

Please Help me to solve this error.


Normal V8 engine works fine but above createNodeJS gives error.

V8 v8 = V8.createV8Runtime()
like image 996
asissuthar Avatar asked Mar 03 '17 09:03

asissuthar


People also ask

Can I run node js on Android?

If you are excited about Node. js and own an Android device, no doubt you'll enjoy running Node. js on it. Thanks to Termux a terminal emulator and Linux environment for Android, the fun of developping Node.

Can we use Nodejs as backend in Android?

You cannot run Node. js on Android, as these are unrelated things. Node. js runs on the server, while Android is the operating system.

Can we make app using node JS?

Node. js is great for building fast and scalable front-end applications like single-page applications (SPAs). Node. js has many features that make it really easy to handle requests without needing to refresh your web app every time you make changes, making node.


1 Answers

Root Cause

The J2V8 library contains a JAR and a native library containing v8 engine. In your case, the JNI native library is not compiled with -D NODE_COMPATIBLE=1 option and hence you get the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.asissuthar.lion/in.asissuthar.lion.ConsoleActivity}: java.lang.UnsupportedOperationException: StartNodeJS Not Supported.

This can be asserted by looking into J2V8 code. I have added below a snippet:

#ifndef NODE_COMPATIBLE
  (env)->ThrowNew(unsupportedOperationExceptionCls, "StartNodeJS Not Supported.");
#endif

Possible Solutions:

  1. You need to recompile the JNI source code with -D NODE_COMPATIBLE=1 option. The source code is available at https://github.com/eclipsesource/J2V8

OR

  1. Raise a ticket on their github so that they can update the aar with updated native library with node support.
like image 132
manishg Avatar answered Sep 30 '22 14:09

manishg