buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
}
//加入国内镜像防止比较大google一直无法下载
allprojects {
repositories {
maven { url ‘https://maven.aliyun.com/repository/google’ }
maven{ url ‘https://maven.aliyun.com/repository/jcenter’}
mavenCentral()
google()
jcenter()
}
}
implementation ‘com.eclipsesource.j2v8:j2v8:6.0.0@aar’
- 使用Integer(返回最后一行Integer结果)
/**
-
使用Integer(返回最后一行Integer结果)
-
两个字符串连接起来并且返回了结果字符串的长度
*/
private void printV8Integer() {
V8 runtime = V8.createV8Runtime();
int result = runtime.executeIntegerScript(“” +
“var hello = 'hello, ';\n” +
“var world = ‘world!’;\n” +
“hello.concat(world).length;\n”);
Log.e(TAG, "JS result = " + result);//JS result = 13
runtime.release(true);
}
- 使用String(返回最后一行结果)
/**
- 使用String(返回最后一行结果)
*/
private void printV8String() {
V8 runtime = V8.createV8Runtime();
String result = runtime.executeStringScript(“” +
“var hello = 'hello, ';\n” +
“var world = ‘world!’;\n” +
“hello.concat(world);\n”);
Log.e(TAG, "JS result = " + result);//S result = hello, world!
runtime.release(true);
}
- 获取Javascript对象
/**
-
获取Javascript对象
-
使用J2V8可以从Java中获取javascript对象的句柄
-
-
V8Object还提供了一些其他有用的方法。
-
getKeys()将返回与Object关联的键。