Skip to main content


April 16, 2022

The reasons I need do some Javascript code in Andriod based project was because of me running the background thread (in doinbackground part in an Asynctask), I want want to use Webview

Why not WebView

The first problem here is that Webview is heavy which then makes it run slow overall which is bigger problem when you are doing large kind of proejct and you need to add as well a lot of webview related setting which slows things done further when you want to do things like code snippet so my thoughts it’s not worth doing it because of these sort of problems overall.

Second main points I want to make which is UI related webview should be created in a UI thread which will then load Javascript code in the background of the thread then pass the results bac kto the UI it’s workable overall but it’s not safe in my thoughts.

If you can call methods on webview from any other thread than your app UI thread it can cause unexpected results.

Last but not final Webview is fragmented after Android 4.4 or level 19 is using Webview based on Chromium style and even oo this is a good thing my mindset is 4.3 so a older version which could give you problems. You should try and code two versions of Webview in related code to just try and make it work.

A lot of code should be re-wrote.

JavaScript  Brand computer graphic

Luckily, the good people in Mozilla make a project called Rhino. It is typically embedded into Java applications to provide scripting to end users.

How to use Rhino?

Rhino is a open source project, you can get the source code using this command:

git clone

Of course, you don’t have to get the source code. If you want to use it in your Android

project. Download Rhino first, unzip it, put the js.jar file under libs folder. It is very small, so you don’t need to worry your apk file will be ridiculously large because of this one external jar.

Here is some simple code to execute JavaScript code.

Object[] params = new Object[] { “javaScriptParam” };

// Every Rhino VM begins with the enter()

// This Context is not Android’s Context

Context rhino = Context.enter();

// Turn off optimization to make Rhino Android compatible


try {

Scriptable scope = rhino.initStandardObjects();

// Note the forth argument is 1, which means the JavaScript source has

// been compressed to only one line using something like YUI

rhino.evaluateString(scope, javaScriptCode, “JavaScript”, 1, null);

// Get the functionName defined in JavaScriptCode

Object obj = scope.get(functionNameInJavaScriptCode, scope);

if (obj instanceof Function) {

Function jsFunction = (Function) obj;

// Call the function with params

Object jsResult =, scope, scope, params);

// Parse the jsResult object to a String

String result = Context.toString(jsResult);


} finally {



As you can see, it is quite easy. You can dig up more in Rhino document.

Proguard & Obfuscation

You can set up Rhino’s proguard setting by adding this following line into your proguard-android.txt file.

-keep class org.mozilla.javascript.** { *; }

When you run the assembleRelease task, with minifyEnabled true of course, you still can see a lot of warning like these:

Warning: can’t find referenced class javax.swing.JMenu

Warning: can’t find referenced class javax.swing.JMenu

Warning: can’t find referenced class javax.swing.JMenu

Warning: can’t find referenced class javax.swing.ButtonGroup

Warning: can’t find referenced class javax.swing.JScrollPane

Warning: can’t find

referenced class java.awt.event.ActionEvent

Warning: can’t find referenced class java.awt.event.ActionEvent

Warning:$1: can’t find referenced class javax.swing.filechooser.FileFilter

Warning:$2: can’t find referenced class java.awt.event.WindowAdapter

Warning:$2: can’t find referenced class java.awt.event.WindowEvent

Warning:$2: can’t find referenced class java.awt.event.WindowEvent

Warning: org.mozilla.javascript.xml.impl.xmlbeans.LogicalEquality: can’t find referenced class org.apache.xmlbeans.XmlCursor

Warning: org.mozilla.javascript.xml.impl.xmlbeans.LogicalEquality: can’t find referenced class org.apache.xmlbeans.XmlCursor

Warning: org.mozilla.javascript.xml.impl.xmlbeans.LogicalEquality: can’t find referenced class org.apache.xmlbeans.XmlCursor

Luckily, evan with these warnings, the app runs fine. You can ignore them(that’s what I do), or adding this line inot your proguard-android.txt file.

-dontwarn org.mozilla.javascript.**


Popular posts from this blog

cobol code

COBOL (Common Business-Oriented Language) is a programming language that is primarily used in business and administrative systems. It's known for its readability and usage in handling large volumes of data. Although it might be difficult to cover all aspects of COBOL in a single response, I can provide you with a brief introduction and an example code snippet to help you get started. COBOL programs are composed of a series of paragraphs that contain statements. Each statement starts with a verb and ends with a period. Here's a simple example of a COBOL program that calculates the sum of two numbers: cobol Copy code IDENTIFICATION DIVISION. PROGRAM-ID. ADDITION. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM1 PIC 9(5). 01 NUM2 PIC 9(5). 01 SUM PIC 9(6). PROCEDURE DIVISION. DISPLAY "Enter the first number: ". ACCEPT NUM1. DISPLAY "Enter the second number: ". ACCEPT NUM2.

Development Programming Languages Python Python : Basics you should learn for beginners

    Why is Python number one programming language in the world? It's easy to understand and complete simple or complex tasks plus it has many library or APIs to allow you to perform other actions more easy through using it . Showing how simple it it to put the most common message on the screen being print("Hello world")   Wages you can get paid range from around $60,000 to max of like $150,000  per  year if you can learn this well some of the biggest companies which use this language include. Udemy Facebook Google Pinterest Instagram Twitter Netflix Spotify Reddit Redhat Lyft Youtube How to install Python on Windows and Apple.

Why Windows XP the best Operation system to ever be released from Microsoft?

 Best parts of it  Loads fast Does not take up much power when it comes to loading the computer Many options to change how things look or run Hardly ever crashes Semi good security behind it Many people made software to run on it A big reason so many people liked XP compared to newer or older versions of the operation system was it would load fast most of the time even if you put it under heavy pressure such as a lot of software running or a lot of tabs on the internet browser . A lot of older videogames would run on it without many problems since it was well equipped to handle it without you having to change over how the computer works or be forced to use emulator as much for why it became very popular even today to allow older videogames to run on the computer for why some people keep these computers purely for retrogame play or other purposes. Some government bodies actually still run XP the reason for why so many people can work