The use of Android j2v8 in APP+J2EE development

Source: Internet
Author: User

J2V8 is a Java Open source project based on the kernel JS V8 engine in WebKit, which implements the mutual invocation between Java and JavaScript, which makes up the defects of 2 languages.

Http://mvnrepository.com/search?q=com.eclipsesource.j2v8

http://eclipsesource.com/blogs/2015/02/25/announcing-j2v8-2-0/


Advantages

1. Dynamic generation, registration and invocation of methods can be implemented to solve the problem of Java method bursting

2. Reduces the difficulty of interaction, avoids the use of webview

3. Most JavaScript APIs can be called directly, making it easier to parse JSON like

4.J2V8 provides a variety of data structures and implements new ways to store cached data

5. Implement dynamic script execution, realize multi-threading

6.Javascript easier to call Android code layer, development language use javascript+ native UI easier to develop Native App


Disadvantages
    1. APK will become larger due to the use of the V8 engine

    2. Java class method Registration is not supported, call,apply is not supported in JavaScript's prototype connection

    3. The release process is too cumbersome and the memory is not automatically released by arc

    4. Loading scripts by script path is not supported and requires the IO stream to be a string to join the script


Code Show

Well, the above is J2V8 information, below to enjoy the charm of this open source project

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 > (&NBSP;&NBSP;&NBS);p;     for  (int i = 0; i < 10; i++)  {             V8Thread t = new  V8thread (new v8runnable ()  {@Overridepublic  void run (V8&NBSP;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&nbsp (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);p Arameters.push ("John");p Arameters.push ("Smith"); Parameters.push (7); V8object result = v8.executeobjectfunction ("Getperson",  parameters);p arameters.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");p Aram.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 ();p aram.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 ", &NBSP;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 () &nbsP {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);}} 


The use of Android j2v8 in APP+J2EE development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.