├── .gitignore
├── 20190918135108.jpg
├── README.md
├── app
├── build.gradle
├── libs
│ └── j2v8-5.0.103.aar
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── tang
│ │ └── test
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── assets
│ │ └── j2v8
│ │ │ └── j2v8test.js
│ ├── java
│ │ └── com
│ │ │ └── tang
│ │ │ └── test
│ │ │ ├── AsyncTaskBackListener.java
│ │ │ ├── J2V8ReadJavaScript.java
│ │ │ ├── MainActivity.java
│ │ │ └── j2v8
│ │ │ ├── J2V8Helper.java
│ │ │ ├── J2V8Interface.java
│ │ │ ├── J2V8InterfaceImpl.java
│ │ │ ├── J2V8Single.java
│ │ │ ├── J2V8Util.java
│ │ │ ├── J2V8ValueCallBack.java
│ │ │ ├── J2v8Example.java
│ │ │ ├── JavaCallbackImpl.java
│ │ │ └── JavaVoidCallbackImpl.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── tang
│ └── test
│ └── ExampleUnitTest.java
├── build.gradle
├── j2v8test.js
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | gradle/
4 | .idea/
5 | /app/build
6 | /gradle.properties
7 | /local.properties
8 | /gradlew
9 | /gradlew.bat
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 |
--------------------------------------------------------------------------------
/20190918135108.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tangbei/j2v8_android/fa62ecda32bd35107287936e153459b46f297dea/20190918135108.jpg
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # j2v8的android测试demo。
2 |
3 | 本工程只是 j2v8 测试demo, 包含了j2v8的所有使用方法。可以下载 工程体验。
4 |
5 | #### j2v8使用方式:
6 | 1. 在build.gradle中添加依赖:
7 | ```java
8 | dependencies {
9 | compile 'com.eclipsesource.j2v8:j2v8:5.0.103@aar'
10 | }
11 | ```
12 | 2. 也可以在app的libs中引入j2v8.aar文件。[点击获取maven地址](https://mvnrepository.com/artifact/com.eclipsesource.j2v8/j2v8)
13 | 同时在build.gradle中引入:
14 | ```
15 | repositories {
16 | flatDir {
17 | dirs 'libs'
18 | }
19 | }
20 | ```
21 |
22 | #### 后台java使用方式:
23 |
24 | maven依赖方式:
25 | ```maven
26 |
27 | com.eclipsesource.j2v8
28 | j2v8
29 | 5.0.103
30 |
31 | ```
32 | java后台使用方式,和android类似,只是其中的j2v8方法还有使用上的区别。
33 |
34 | #### 工程demo使用介绍:
35 |
36 | 本工程已经包含了 所有j2v8使用到的方法,和使用到的工具类。以及包含了三种读取 js 文件的方式,方便在任何场景下 java 和 js的交互。
37 |
38 | #### 一、本地 js文件/js字符串 读取方式
39 |
40 | 在主工程app的assets文件夹中添加js脚本文件,如果没有assets文件夹,则新建一个。
41 | 
42 |
43 | 获取js脚本文件的方式:
44 | ```
45 | final InputStream INPUTSTREAM = this.activity.getClass().getResourceAsStream("/assets/j2v8/j2v8test.js");//获取js脚本的输入流
46 | ```
47 |
48 | #### 二、后台数据库js的string字符串 读取方式
49 |
50 | 在需要调取js的类中 implements J2V8Interface这个接口,同时实现 getJs()方法。
51 | ```java
52 | /**
53 | * js脚本 字符串
54 | * @return
55 | */
56 | @Override
57 | public String getJs() {
58 | /* String s = "function j2v8String(x,y){\n" +
59 | " var name3 = x.concat(y);\n" +
60 | " return name3;\n" +
61 | "}";*/
62 | return "";
63 | }
64 | ```
65 |
66 | 此读取方式,更加灵活,可以直接把js存到后台数据库,每次调用的时候 拿到后台的js字符串即可。
67 | 同时调用`J2V8Helper.class`暴露的帮助实现方法 即可,demo写的很详细,一些方法还是比较全面的,同时你可以自己更改代码,获取自己想要的实现方式,并放在自己的工程中。 具体可下载工程查看使用方式。 /n
68 |
69 | 注意事项:**存的是js脚本字符串、或者js脚本字符串方法**。
70 |
71 |
72 | #### 三、服务器js文件 读取方式
73 |
74 | js文件既可以本地读取,那么 服务器链接 读取肯定也是可以的。
75 |
76 | ```java
77 | /**
78 | * 获取oss上的js文件,并转换成String返回
79 | * 如果是获取网络地址,则使用 J2V8Single.class 处理
80 | * @param _url oss上的js地址:https://github.com/tangbei/j2v8_android/blob/master/j2v8test.js
81 | * @return
82 | */
83 | public static String getOssJsUrl(String _url){
84 | String jsPath ="";
85 | try {
86 | // 构造URL
87 | URL url = new URL(_url);
88 | // 打开连接
89 | URLConnection con = url.openConnection();
90 | // 设置请求超时为5s
91 | con.setConnectTimeout(5 * 1000);
92 | // 输入流
93 | InputStream is = con.getInputStream();
94 | jsPath = supplyAsync(is);
95 | is.close();
96 | } catch (Exception e) {
97 | e.printStackTrace();
98 | }
99 | return jsPath;
100 | }
101 | ```
102 |
103 | 但是调取方式和第一种不同,是在 `J2V8Single.class`单例类中使用,目的是只网络读取一遍js脚本文件。
104 |
105 |
106 | #### 读取方式优劣分析
107 | 1. 三种方式在读取上第二种是最快的,直接拿到的是js脚本字符串。
108 | 2. 本地读取和本地读取字符串也是最快、但是不可动态配置js脚本,不灵活。
109 | 3. 服务器读取虽然动态可配置,但是读取速度上不占优,使用单例模式加载网络文件,目的是使用时只读取一遍,提升代码可用度。
110 |
111 | 综上,还是推荐数据库存储js字符串 读取、即可动态配置、也不失灵活、读取便捷、扩展性强、适用场景更多。
112 |
113 |
114 |
115 | #### 注意事项
116 | 1. 本demo只是粗略概括了`j2v8`的各种使用方式,为初次使用 jav8 的人搭个桥。
117 | 2. 使用j2v8时一定要及时释放,否则会很耗性能和内存。
118 | 3. 如果java后台使用j2v8 也可以使用此demo,只是依赖替换一下,具体使用大同小异。
119 | 4. 本demo,可以随意更改,并用到自己的工程中,希望能帮到你们。
120 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.tang.test"
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | repositories {
22 | flatDir {
23 | dirs 'libs'
24 | }
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 | implementation 'com.android.support:appcompat-v7:28.0.0'
30 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
31 |
32 | implementation (name: 'j2v8-5.0.103',ext: 'aar')
33 |
34 | testImplementation 'junit:junit:4.12'
35 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
36 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
37 | }
38 |
--------------------------------------------------------------------------------
/app/libs/j2v8-5.0.103.aar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tangbei/j2v8_android/fa62ecda32bd35107287936e153459b46f297dea/app/libs/j2v8-5.0.103.aar
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/tang/test/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.tang.test;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.tang.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/assets/j2v8/j2v8test.js:
--------------------------------------------------------------------------------
1 |
2 | function j2v8String(x,y){
3 |
4 |
5 | var name3 = x.concat(y);
6 | return name3;
7 | }
8 |
9 | function j2v8Int(x,y){
10 |
11 | var name3 = x + y;
12 | return name3;
13 | }
14 |
15 | function j2v8Boolean(x,y){
16 |
17 | return x == y;
18 | }
19 |
20 | function putVoid(x,y){
21 | var array = [{name:'zhangSan'}, {name:'liSi'}, {name:'wangMaZi'}];
22 | for ( var i = 0; i < array.length; i++ ) {
23 | javaVoid.call(array[i], " 你好啊"); // javaVoid 是 java 注册到 js 的一个函数
24 | }
25 | return j2v8Int(x,y);
26 | }
27 |
28 | var j2v8New = {
29 | a: "111",
30 | b: "222"
31 | };
32 |
33 | function handle(x,y,callBack){
34 | callBack(x+y);
35 | }
36 |
37 | function javaCallBack(x,y){
38 |
39 | var t = javaBack(x+y);//javaBack是 java注册到js中的一个回调函数
40 | return t;
41 | }
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tang/test/AsyncTaskBackListener.java:
--------------------------------------------------------------------------------
1 | package com.tang.test;
2 |
3 | /**
4 | * 描述:
5 | * 作者 : Tong
6 | * e-mail : itangbei@sina.com
7 | * 创建时间: 2019/8/5.
8 | */
9 | public interface AsyncTaskBackListener {
10 |
11 | void onAsyncTaskCallBack(T t);
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tang/test/J2V8ReadJavaScript.java:
--------------------------------------------------------------------------------
1 | package com.tang.test;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.res.AssetManager;
6 | import android.os.AsyncTask;
7 |
8 | import com.tang.test.j2v8.J2V8Util;
9 |
10 | import java.io.BufferedReader;
11 | import java.io.IOException;
12 | import java.io.InputStream;
13 | import java.io.InputStreamReader;
14 |
15 | /**
16 | * 描述: 使用asyncTask异步读取本地js脚本
17 | * 作者 : Tong
18 | * e-mail : itangbei@sina.com
19 | * 创建时间: 2019/8/5.
20 | */
21 | public class J2V8ReadJavaScript extends AsyncTask
22 | {
23 | private AsyncTaskBackListener listener;
24 |
25 | private Activity activity;
26 | public J2V8ReadJavaScript(Activity activity, AsyncTaskBackListener listener)
27 | {
28 | this.listener = listener;
29 | this.activity = activity;
30 |
31 | }
32 | public J2V8ReadJavaScript()
33 | {
34 | super();
35 | }
36 |
37 | @Override
38 | protected String doInBackground(String... strparams)
39 | {
40 | final InputStream INPUTSTREAM = this.activity.getClass().getResourceAsStream("/assets/j2v8/j2v8test.js");//获取js脚本的输入流
41 | return J2V8Util.getFileContent(INPUTSTREAM);
42 | }
43 | @Override
44 | protected void onPostExecute(String result)
45 | {
46 | if (listener != null)
47 | {
48 | listener.onAsyncTaskCallBack(result);
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/tang/test/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.tang.test;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 | import android.view.View;
7 | import android.widget.TextView;
8 |
9 | import com.eclipsesource.v8.JavaCallback;
10 | import com.eclipsesource.v8.JavaVoidCallback;
11 | import com.eclipsesource.v8.V8;
12 | import com.eclipsesource.v8.V8Array;
13 | import com.eclipsesource.v8.V8Function;
14 | import com.eclipsesource.v8.V8Object;
15 | import com.tang.test.j2v8.J2V8Helper;
16 | import com.tang.test.j2v8.J2V8Interface;
17 | import com.tang.test.j2v8.J2V8InterfaceImpl;
18 | import com.tang.test.j2v8.J2V8Util;
19 | import com.tang.test.j2v8.J2V8ValueCallBack;
20 | import com.tang.test.j2v8.J2v8Example;
21 | import com.tang.test.j2v8.JavaCallbackImpl;
22 | import com.tang.test.j2v8.JavaVoidCallbackImpl;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | public class MainActivity extends AppCompatActivity implements J2V8Interface {
28 |
29 | private TextView tvContent;
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_main);
35 |
36 | tvContent = findViewById(R.id.tv_content);
37 | }
38 |
39 |
40 | public void jav8OnClick(View view){
41 | // localRead();
42 |
43 | readJs();
44 | }
45 |
46 | private void localRead(){
47 |
48 | J2v8Example.getJs(this,new JavaCallbackImpl(),new JavaVoidCallbackImpl(),new J2V8ValueCallBack() {
49 | @Override
50 | public void getReadValue(String value) {
51 | tvContent.setText(value);
52 | }
53 | });
54 | }
55 |
56 | /**
57 | * js脚本 字符串
58 | * @return
59 | */
60 | @Override
61 | public String getJs() {
62 | /* String s = "function j2v8String(x,y){\n" +
63 | " var name3 = x.concat(y);\n" +
64 | " return name3;\n" +
65 | "}";*/
66 | return "";
67 | }
68 |
69 | private void readJs(){
70 | List