1.webview.loadUrl
2.jsevaluator 内部实现也是webview.loadUrl
image.png
GitHub - codebymikey/js-evaluator-for-android: A library for running JavaScript in Android apps.
3.JS in Android (Google V8)
J2V8就是对V8 JavaScript 引擎 的 Java 封装,所以在Java中如果引入了J2V8,我们就可以在Java中运行JavaScript代码。
implementation 'com.eclipsesource.j2v8:j2v8:5.0.103@aar'
https://gitee.com/huangxiaoguo/J2V8.git
test.js
var hisV1 = 0
function myFunction(p1, p2,callback) {
callback(p1,p2)
hisV1 = p1
return p1 * p2;
}
V8 runtime = V8.createV8Runtime();
runtime.executeScript(loadJs("real_library/test.js"));
V8Function function = new V8Function(runtime, new JavaCallback() {
@Override
public Object invoke(V8Object v8Object, V8Array v8Array) {
Log.e("RealLibrary", v8Array.getInteger(0)+"" + v8Object);
return null;
}
});
V8Array push = new V8Array(runtime).push(10).push(20).push(function);
runtime.executeVoidFunction("myFunction",push);
function.close();
push.close();
runtime.close();
网友评论