android stk,GitHub - janstk/duktape-android: The Duktape embeddable Javascript engine packaged for A...

本文介绍了如何在Android应用中使用Duktape库来实现Java和JavaScript之间的互操作。通过创建并绑定Java接口到JavaScript全局对象,可以方便地从JavaScript调用Java方法。示例展示了如何将十六进制字符串解码为UTF-8。同时,文章提供了依赖库的下载和构建指南。
摘要由CSDN通过智能技术生成

Duktape Android

Usage

Duktape duktape = Duktape.create();

try {

Log.d("Greeting", duktape.evaluate("'hello world'.toUpperCase();"));

} finally {

duktape.close();

}

Calling Java from JavaScript

You can bind a Java object to a JavaScript global, and call Java functions from JavaScript!

Currently, the following Java types are supported for function arguments and return values:

boolean and Boolean

int and Integer

double and Double

String

void return values are also supported.

Example

Suppose we wanted to expose the functionality of okio's ByteString in JavaScript to

convert hex-encoded strings back into UTF-8. First, define a Java interface that declares

the methods you would like to call from JavaScript:

interface Utf8 {

String fromHex(String hex);

}

Next, implement the interface in Java code (we leave the heavy lifting to okio):

Utf8 utf8 = new Utf8() {

@Override public String fromHex(String hex) {

return okio.ByteString.decodeHex(hex).utf8();

}

};

Now you can bind the object to a JavaScript global, making it available in JavaScript code:

Duktape duktape = Duktape.create();

try {

// Bind our interface to a JavaScript object called Utf8.

duktape.bind("Utf8", Utf8.class, utf8);

String greeting = duktape.evaluate("" +

// Here we have a hex encoded string.

"var hexEnc = 'EC9588EB8595ED9598EC84B8EC9A9421';\n" +

// Call out to Java to decode it!

"var message = Utf8.fromHex(hexEnc);\n" +

"message;");

Log.d("Greeting", greeting);

} finally {

duktape.close();

}

Download

compile 'com.squareup.duktape:duktape-android:0.9.5'

This library is provided as a "fat" aar with native binaries for all available architectures. To

reduce your APK size, use the ABI filtering/splitting techniques in the Android plugin:

http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits

Snapshots of the development version are available in Sonatype's snapshots repository.

Building

For Android

./gradlew build

NOTE: When building with Android NDK r11b, the NDK bundle has an issue where the path to

clang is incorrect. This can be fixed by creating a symlink called /toolchains/llvm-3.8

to /toolchains/llvm.

For Mac

./build_mac

Set the java.library.path system property to build/ when you execute Java.

License

Copyright 2015 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

Note: The included C code from Duktape is licensed under MIT.

DuktapeJava 是针对 Android 平台封装的 Duktape  JavaScript 引擎;实现Javascript 和 Java的无缝调用。 初始化DuktapeEngnine   @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); duktapeEngine = new DuktapeEngine(); duktapeEngine.put("activity",this); duktapeEngine.execute(AssetScript.toScript(getBaseContext(), "duk.js")); duktapeEngine.call("activityListener", "onCreate", savedInstanceState); } @Override protected void onDestroy() { if (duktapeEngine != null) { duktapeEngine.destory(); duktapeEngine = null; } super.onDestroy(); }   duk.js javascript 代码示例   importClass("com.furture.react.R") importClass("android.view.View.OnClickListener") importClass("android.widget.Toast") importClass("java.lang.Runnable") var activityListener = {}; activityListener.onCreate = function(){ print("activity onCreate onJavaScript"); activity.setContentView(R.layout.activity_duk) button1 = activity.findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener(function(){ Toast.makeText(activity, "Button1 Clicked", Toast.LENGTH_SHORT).show(); var intent = new Intent(activity, "com.furture.react.activity.DetailActivity"); activity.startActivity(intent); })); button2 = activity.findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener({ onClick:function(){ Toast.makeText(activity, "Button2 Clicked", Toast.LENGTH_SHORT).show(); } })); } activityListener.onStart = function(){ print("activity onStart"); } activityListener.onResume = function(){ print("activity onResume"); } activityListener.onPause = function(){ print("activity onPause"); } activityListener.onStop = function(){ print("activity onStop"); } activityListener.finish = function(){ print("activity finish" num); }   Java 和 Javascript 的无缝调用 javascript 调用java方法示例: importClass
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值