標籤:
J2V8是基於webkit中核心js v8引擎的java 開源項目,實現了java與javaScript的互相調用,彌補了2種語言各自的缺陷
http://mvnrepository.com/search?q=com.eclipsesource.j2v8
http://eclipsesource.com/blogs/2015/02/25/announcing-j2v8-2-0/
優點
1.可實現方法動態產生,註冊和調用,可以解決java 方法爆棚問題
2.降低了互動難度,避免了使用webView
3.可直接調用大多數javascript api,讓類似解析JSON變得更容易,方便
4.j2v8提供了多種資料結構,並實現了新的快取資料儲存方式
5.實現了動態指令碼執行,實現了多線程
6.Javascript更容易調用Android代碼層,開發語言使用Javascript+原生UI更容易開發Native App
缺點
由於使用了v8引擎,apk會變得更大
不支援Java類方法註冊,不太支援Javascript的原型連中的call,apply
釋放過程過於繁瑣,記憶體不是ARC自動釋放
不支援按指令碼路徑載入指令碼,需要通過IO流轉為字串才能將指令碼加入
代碼展示
好了,以上是J2V8的相關資訊,下面欣賞一下這個開源項目的魅力
package org.twt.zipjar.test;import java.util.ArrayList;import java.util.List;import com.eclipsesource.v8.V8;import com.eclipsesource.v8.V8Array;import com.eclipsesource.v8.V8Object;import com.eclipsesource.v8.utils.V8Map;import com.eclipsesource.v8.utils.V8ObjectUtils;import com.eclipsesource.v8.utils.V8Runnable;import com.eclipsesource.v8.utils.V8Thread;public class J2V8TestCase {public static void main(String[] args) {} public void testMultiV8Threads() throws InterruptedException { final V8 v8 = V8.createV8Runtime(); final List<Thread> threads = new ArrayList<Thread>(); final List<Object> mergeSortResults = new ArrayList<Object>(); for (int i = 0; i < 10; i++) { V8Thread t = new V8Thread(new V8Runnable() {@Overridepublic void run(V8 v8) { synchronized (threads) { V8Array data = new V8Array(v8); int max = 100; for (int i = 0; i < max; i++) { data.push(max - i); } V8Array parameters = new V8Array(v8).push(data); mergeSortResults.add(V8ObjectUtils.toList(parameters)); parameters.release(); data.release(); }}}); threads.add(t); } for (Thread thread : threads) { thread.start(); } for (Thread thread : threads) { thread.join(); } }private static void declareFunctions() {V8 v8 = V8.createV8Runtime();v8.executeVoidScript("function getPerson(first, last, age) {return {‘first‘:first, ‘last‘:last, ‘age‘:age};}");V8Array parameters = new V8Array(v8);parameters.push("John");parameters.push("Smith");parameters.push(7);V8Object result = v8.executeObjectFunction("getPerson", parameters);parameters.release();result.release();}private static void TestMap() {V8 runtime = V8.createV8Runtime();V8Map<String> v8Map = new V8Map<String>();V8Object v8Object = new V8Object(runtime);v8Map.put(v8Object, "foo");v8Object.release();runtime.registerResource(v8Map);runtime.release(true);}private static void jsFunctionCall() {V8 v8 = V8.createV8Runtime();V8Array param = new V8Array(v8);V8Array arrAList = new V8Array(v8).push(0).push("0");V8Array arrBList = new V8Array(v8).push(1).push("1");param.push(arrAList).push(arrBList);V8Array result = v8.executeArrayScript("var s = [];Array.prototype.push.apply(s,[1,2,3]);s");System.out.println(V8ObjectUtils.toList(result));arrAList.release();arrBList.release();param.release();result.release();v8.release();}private static void jsonObjectCall() { V8 v8 = V8.createV8Runtime(); V8Object v8Object = new V8Object(v8).add("foo", "bar") add("name", "zhangsan") .add("age", 20); V8Object json = v8.getObject("JSON"); V8Array params = new V8Array(v8).push(v8Object); String result = (String) json.executeFunction("stringify", params); V8Array params2 = new V8Array(v8).push(result); V8Object result2 = json.executeObjectFunction("parse", params2); System.out.println(result); System.out.println(result2.getString("name")); params.release(); params2.release(); result2.release(); json.release(); v8Object.release(); v8.release();}private static void dataCache() {V8 v8 = V8.createV8Runtime();v8.add("name", "zhangsan");System.out.println(v8.get("name"));v8.release();}private static void callJavaNativeMethod() {V8 v8 = V8.createV8Runtime();v8.registerJavaMethod(new J2V8TestCase(), "print", "print",new Class[]{String.class});String s = "Hello World";v8.executeVoidScript("print(‘"+s+"‘)");v8.release();}private static void executeArrayScript(){V8 v8 = V8.createV8Runtime();V8Array result = v8.executeArrayScript("var buffer = new ArrayBuffer(256);\n" + "var i32 = new Int32Array(buffer);\n" + "i32[0] = 1;\n" + "i32[1] = 3;\n" + "i32[2] = i32[0] + i32[1];\n" + "i32;");int[] ints = result.getIntegers(0, 3);result.release();v8.release();}public void print(String s){System.out.println(s);}}
Android J2V8在App+j2ee開發中的使用