DuktapeJava Save

Tiny Powerfull JavaScript Engine On Android Platform integrating with java

Project README

DuktapeJava

JavaScript Engine on android platform base on Duktape, which is tiny, powerful, low memory cost. you can use any java method in javascript by just small engine, give you endless power integrating java with javascript on android platform.

Quick Start

1、how to use engine in your studio project

compile 'com.furture.react:DuktapeJava:1.2.0'

or

compile 'com.furture.react:DuktapeJava:1.2.0@aar'

2、new DuktapeEngine instance,put js context then execute javascript code.

@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();
}

3、duk.js javascript code sample


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();
				}
			}));
};

 Toast.makeText(activity, "Hello World JavaScript", Toast.LENGTH_SHORT).show();

Javascript Guide

1、JavaScript call java method like java

 importClass("android.widget.Toast")
 Toast.makeText(activity, "Javascript Hi Toast", Toast.LENGTH_SHORT).show();
importClass("android.view.View")

var view = new View(activity);
importClass("android.view.View.OnClickListener")
view.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);
}));

view2.setOnClickListener(new OnClickListener({
	onClick:function(){
		 Toast.makeText(activity, "Button2 Clicked", Toast.LENGTH_SHORT).show();
	}
}));

2、JavaScript new java abstract instance with specific implemation

importClass("com.furture.react.ext.JSBaseAdapter")

gridView.setAdapter(new JSBaseAdapter({
	getCount : function() {
		return 8;
	},
	getView : function(position,  convertView, parent){
			if(convertView == null){
					convertView = ui.fromXml(gridItemXml, null);
			}
			return convertView;
	},
	getViewTypeCount : function () {
			return 1;
	}
}));

Java Guide

1、Java Call JavaScript Method

Java Code Sample

public class DataUtils{
	public static void showData(JSRef ref){
			int count = ((Number)ref.call("count")).intValue();
			for(int i=0; i<count; i++){
					Log.d("DataUtils", "Call JavaScript getItem Method :  " + ref.call("getItem", i));
			}
	 }
}   

javascript code sample

  importClass("com.efurture.react.DataUtils")
  var data = {};
  data.count =  10;
  data.getItem = function(index){
         return "Javascript Data " + index;
  }
  DataUtils.showData(data);

Share Common Context For Multi Engine Instance

JSContext Provide Share Context For Multi DuktapeEngine, All Context Will Auto Be Imported To DuktapeEngine when Instance Created. you can use it share context.

JSContext.put("application", application);

Product Usage

AssetScript class and JSTransformer.jar is only for development usage. for Product, you should not include it on your app. you can use JSTransformer.jar transform JavaScript source, then use transformed js in your app.

cd JSTransformer/js
java -jar JSTransformer.jar  ui.js  transformed/ui.js

then use transformed/ui.js to your app.

you can also use gulp tool. see dev-server demo. write source in src/ then javascript will auto be transformed in build/ directory

cd  dev-server
npm install
npm start

License

DuktapeJava is MIT licensed

Reference

DuktapeJava Java Docs

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java

http://www.w3schools.com/js/default.asp

https://github.com/jasonsantos/luajava

Open Source Agenda is not affiliated with "DuktapeJava" Project. README Source: gubaojian/DuktapeJava
Stars
76
Open Issues
5
Last Commit
1 year ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating