loader

Dynamically extend android apps with Beanshell

George Argyrakis | 05/07/2016 | Android, programming, java, sample, scripting

What is Beanshell?

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

[end]

Scripting features:

  • Optionally typed variables.
  • Scripted methods with optionally typed arguments and return values
  • Scripted objects (method closures)
  • Scripted interfaces and event handlers.
  • Convenience syntax for working with JavaBean properties, hashtables, and primitive wrapper types.
  • Auto-allocation of variables to emulate Java properties files.
  • Extensible set of utility and shell-like commands
  • Dynamic classpath management including find grained class reloading
  • Dynamic command loading and user command path
  • Sophisticated namespace and callstack management
  • Detailed error reporting


Uses:

  • Scripting extension for applications - Allow your applications to be extended via scripts in an intuitive and simple way.
  • Macro Languages - Generate scripts as macros and execute them live in your VM easily.
  • Expression evaluator for scientific, financial apps and rules engines - evaluate complex expressions with conditions and loops.
  • Remote debugging - Embed a live, remotely accessible shell / command line in your application with just a few lines of code.
  • Use BeanShell declaratively to replace properties files and replace startup config files with real scripts that perform complex initialization and setup with the full Java syntax at their disposal.


How to Use it:

The best way to understand the power of beanshell is through an example. Let’s create a sample android app that will execute arbitrary code of user’s choice.

Get in on Github.


Gradle Setup:

Create a new app and add the following gradle dependency.

compile 'org.beanshell:bsh:2.0b5'


Activity Layout:

Create a simply layout with one edit text and one button.
The edit text will hold the java code and the button will execute it.


Initialize beanshell

First create a new interpreter that will evaluate our scripts:

Interpreter interpreter = new Interpreter();

Then of the activity we pass any variable we will refer to it directly from script:

interpreter.set("myapp", this); //a reference to activity
interpreter.set("code", findViewById(R.id.editTextCode));
interpreter.set("button", findViewById(R.id.buttonRun));

Optionally we can enable a server at [device ip]:[portnum] to remotely run commands:

interpreter.set("portnum", 1234); // set port
interpreter.eval("setAccessibility(true)"); // turn off access restrictions
interpreter.eval("server(portnum)"); // enable server


Run Code

Finally, on button click listener we will run the code in edittext:

interpreter.eval(javaCode.getText().toString());


Sample Code

Following are some samples you can try and run inside the app:

// myapp: variable passed to the interpreter
import android.widget.Toast;
Toast.makeText(myapp, "This is a Toast from beanshell", Toast.LENGTH_SHORT).show();

// code: variable passed to the interpreter
import android.support.design.widget.Snackbar;
Snackbar.make(code,"This is a Snackbar from beanshell",Snackbar.LENGTH_LONG).show();

// Run on UI thread
code.post(new Runnable() {public void run() {button.setText(\"Changed from code\");} });



Telnet

If you have enabled server you can connect and run commands either at http://[device ip]:[portnum] or with telnet at [device ip]:[portnum+1]. I recommend using putty with Connection Type =Raw.