Giter Site home page Giter Site logo

andjs's Introduction

andjs

  • support js engine quickjs and chromium v8
  • native javascript object, such as jscrypto, adb
  • multi-instance support
  • inject java method by annotation

How to integrate andjs:

1: add andjs aar into your project
2: new AndJS(context) to create AndJS Instance
3: @CalledByJavascript to annotation your java method, which will be called in javascript
4: mJSInstance.injectObject to inject java object
5: mJSInstance.loadJSBuf(String jsbuf) to Run javascript

Sample code

import com.github.wuruxu.andjs.AndJS;
public class MainActivity extends AppCompatActivity {
    private AndJS mJSInstance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      mJSInstance = new AndJS(this); //create AndJS Instance
      
      obj = new MyObject();
      //Inject java object into v8 engine as 'myobject'
      mJSInstance.injectObject(obj, "myobject", null);
      
      //Inject this Activity object into v8 engine as 'myactivity'
      mJSInstance.injectObject(this, "myactivity", null);
      
      String jsbuf = "myobject.doLog('This is a JS string');";
			jsbuf += "var msg = myobject.getMessage(); adb.info(msg);";
			jsbuf += "var home = myobject.getMyHome(); home.printRect(0, 0, 512, 512);";
			jsbuf += "var homemsg = home.getMessage(); adb.info(homemsg);";
			jsbuf += "var version = get_v8_version(); myactivity.updateTitle(version);";
        
      mJSInstance.loadJSBuf(jsbuf); // Run Javascript in V8 engine
    }
    
    @CalledByJavascript
    void updateTitle(String version) {
    	String str = getTitle().toString();
	str += "@v8(" + version+")";
	setTitle(str);
    }
}

//sample code of MyObject
import com.github.wuruxu.andjs.CalledByJavascript;

public class MyObject extends Object {
	private static final String TAG = "MyObject";
	private MyHome home;

	public MyObject() {
		home = new MyHome();
	}

	@CalledByJavascript
	public void doLog(String msg) {
		Log.i(TAG, " * " + msg);
	}

	@CalledByJavascript
	public String getMessage() {
		return "This is a java string";
	}

	@CalledByJavascript
	public MyHome getMyHome() {
		return home;
	}
}

//sample code of MyHome
import com.github.wuruxu.andjs.CalledByJavascript;

public class MyHome extends Object {
	private static final String TAG = "MyHome";

	public MyHome() {}

	@CalledByJavascript
	public void printRect(int x0, int y0, int x1, int y1) {
		Log.i(TAG, " * x0 " + x0 + " y0 " + y0 + " x1 " + x1 + " y1 " + y1);
	}

	@CalledByJavascript
	public String getMessage() {
		return "This is a java string from MyHome";
	}
}

Donate

Your donation makes andjs better:

andjs's People

Contributors

wuruxu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.