android v8 js,javascript - Android utilize V8 without WebView - Stack Overflow

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 unable to execute the Java bytecode the Rhino JIT compiles).

Android has its built-in V8 javascript engine which is accessed internally via JNI and ought to give much better performance than Rhino; however, the only way I can find to access it is indirectly through a WebView.

Unfortunately, WebView requires a Context, and crashes with NPE with a null context, so I'm unable to even instantiate a dummy WebView to merely execute the code and return the result. The nature of my exercise doesn't really allow me to provide a Context for WebView, so I'm hoping perhaps there's something I'm overlooking.

Several of these V8Threads run in parallel, so it's not really feasible (as far as I'm aware) to add a WebView to my layout and hide it, as I don't believe a single WebView can execute functions in multiple threads.

private class V8Thread extends Thread

{

private WebView webView;

private String source;

private double pi;

private int i, j;

public V8Thread(int i, int j)

{

pi = 0.0;

this.i = i;

this.j = j;

source = "";

try {

InputStreamReader isReader = new InputStreamReader(assetManager.open("pi.js"));

int blah = isReader.read();

while (blah != -1)

{

source += (char)blah;

blah = isReader.read();

}

webView = new WebView(null);

webView.loadData(source, "text/html", "utf-8");

webView.getSettings().setJavaScriptEnabled(true);

webView.addJavascriptInterface(this, "V8Thread");

} catch (IOException e) {

e.printStackTrace();

}

}

public double getResult()

{

return pi;

}

@Override

public void run()

{

webView.loadUrl("javascript:Androidpicalc("+i+","+j+")");

}

}

Ideally there must be some supported way to call V8 directly, or at least execute javascript without requiring an actual WebView, as it seems a rather clunky and convoluted method just to run javascript code.

UPDATE

I've rearranged my code a bit, though unseen here is that now I am instantiating the V8Threads on the AsyncTasks's onPreExecute() while keeping everything else in doInBackground(). The source code is read in earlier in the program, so it's not redundantly re-read for each thread.

Because now the V8Thread is instantiated on the UI Thread, I can pass it the current view's Context (I'm using fragments so I can't just pass it "this"), so it no longer crashes.

private class V8Thread extends Thread

{

private WebView webView;

private double pi;

private int i, j;

public V8Thread(int i, int j)

{

pi = 0.0;

this.i = i;

this.j = j;

source = "";

webView = new WebView(v.getContext());

}

@SuppressWarnings("unused")

public void setResult(String in)

{

Log.d("Pi",in);

}

public double getResult()

{

return pi;

}

@Override

public void run()

{

webView.getSettings().setJavaScriptEnabled(true);

webView.addJavascriptInterface(this, "V8Thread");

webView.loadData(source, "text/html", "utf-8");

//webView.loadUrl("javascript:Androidpicalc("+i+","+j+")");

webView.loadUrl("javascript:test()");

Log.d("V8Thread","Here");

}

}

However, when executing, logcat spits out one per thread of the error "Can't get the viewWidth after the first layout" and the javascript code never executes. I know the thread fires completely, because the "Here" log message is sent. Here's the relevant test() function in the js code.

function test()

{

V8Thread.setResult("blah");

}

Working correctly, "blah" should show up four times in logcat, but it never shows up. Could be my source code is read incorrectly, but I doubt that.

Scanner scan = new Scanner(assetManager.open("pi.js"));

while (scan.hasNextLine()) source += scan.nextLine();

The only other thing I can imagine is that due to these aforementioned errors, the webView never actually gets around to executing the javascript.

I'll also add that pi.js contains only javascript, no HTML whatsoever. However, even when I wrap it in just enough HTML for it to qualify as a webpage, still no luck.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值