Beanshelldemo

Dynamically extend android apps

View the Project on GitHub GeorgeArgyrakis/beanshelldemo

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.

demo

Scripting features

Uses

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.

activity

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.

putty