Android utilize V8 without WebView

后端 未结 5 856
甜味超标
甜味超标 2020-12-12 13:55

I\'m exercising executing javascript from Java. Rhino works very well for this on desktop, but has to fall back to (slow) interpreted mode on Android (due to dalvik being u

相关标签:
5条回答
  • 2020-12-12 14:17

    You can create a new V8 Context via its API and use that to execute your JavaScript, look into https://android.googlesource.com/platform/external/v8 include directory which contains two C++ header files. Link against the libwebcore.so (compiled from https://android.googlesource.com/platform/external/webkit) library via the NDK, nothing special.

    v8::Persistent<v8::Context> context = v8::Persistent<v8::Context>::New(v8::Context::New());
    context->Enter();
    

    Refer to https://developers.google.com/v8/get_started which will work on Android. Just make sure the device actually ships with V8 (some older devices ship with JSC [JavaScript Core]).

    0 讨论(0)
  • 2020-12-12 14:20

    A bit of a late response but it may be useful to anyone stumbling upon this question. I used the J2V8 library which is a Java wrapper on Google's V8 engine. This library comes with pre-compiled V8 binaries for x86 and armv7l Android devices. It work seamlessly. See here for tutorials. Just keep in mid that since pure V8 is just an Ecmascript engine, there is no DOM element available.

    0 讨论(0)
  • 2020-12-12 14:20

    Can you get a hold of the Context that is your Application? There are a couple ways to do this.

    1. Call getApplication() from your Activity (Application is a child of Context)
    2. Call getApplicationContent() from a Context (Context likely being your Activity)

    UPDATE

    According to this Android documentation, your bound Javascript code will run in a separate process anyways, so there should be no need to set it up in its own Thread.

    From the link:

    Note: The object that is bound to your JavaScript runs in another thread and not in the thread in which it was constructed. (The 'object' being referred to is the JavascriptInterface class)

    0 讨论(0)
  • 2020-12-12 14:21

    I found this really nifty open source ECMAScript compliant JS Engine completely written in C called duktape

    Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint.

    You'd still have to go through the ndk-jni business, but it's pretty straight forward. Just include the duktape.c and duktape.h from the distributable source here(If you don't want to go through the build process yourself) into the jni folder, update the Android.mk and all that stuff.

    Here's a sample C snippet to get you started.

    #include "duktape.h"
    
    JNIEXPORT jstring JNICALL
    Java_com_ndktest_MainActivity_evalJS
    (JNIEnv * env, jobject obj, jstring input){
        duk_context *ctx = duk_create_heap_default();
        const char *nativeString = (*env)->GetStringUTFChars(env, input, 0);
        duk_push_string(ctx, nativeString);
        duk_eval(ctx);
        (*env)->ReleaseStringUTFChars(env, input, nativeString);
        jstring result = (*env)->NewStringUTF(env, duk_to_string(ctx, -1));
        duk_destroy_heap(ctx);
        return result;
    }
    

    Good luck!

    0 讨论(0)
  • 2020-12-12 14:22

    You can use the AndroidJSCore project. It is not based on V8, but JavaScriptCore. The current version (2.2+) supports JIT compilation on all processors not named MIPS.

    UPDATE 2018: AndroidJSCore has been superseded by LiquidCore, which is, in fact, based on V8. Not only does it include the V8 engine, but all of Node.js is available as well.

    0 讨论(0)
提交回复
热议问题