Skip to main content

2015/04/05/Execute-JavaScript-in-Android-without-WebView/



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. https://developer.android.com/guide/webapps/migrating#Threads


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. https://www.theengineer.info/2023/02/google-drive-programming-style-and.html


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 https://github.com/mozilla/rhino.git


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


rhino.setOptimizationLevel(-1);


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 = jsFunction.call(rhino, scope, scope, params);


// Parse the jsResult object to a String


String result = Context.toString(jsResult);


}


} finally {


Context.exit();


}


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: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JMenu


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JMenu


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JMenu


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.ButtonGroup


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JScrollPane


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find


referenced class java.awt.event.ActionEvent


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class java.awt.event.ActionEvent


Warning: org.mozilla.javascript.tools.shell.JSConsole$1: can’t find referenced class javax.swing.filechooser.FileFilter


Warning: org.mozilla.javascript.tools.shell.JSConsole$2: can’t find referenced class java.awt.event.WindowAdapter


Warning: org.mozilla.javascript.tools.shell.JSConsole$2: can’t find referenced class java.awt.event.WindowEvent


Warning: org.mozilla.javascript.tools.shell.JSConsole$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.**


Comments

Popular posts from this blog

My honest Review of Surfshark VPN or ISPS protector

  VPN:  https://get. surfshark .net/aff_ c?offer_id=926&aff_id=19425 Antivirus:  https://get. surfshark .net/aff_ c?offer_id=934&aff_id=19425 Incogni:  https://get.incogni.io/aff_c? offer_id=1017&aff_id=19425 Helps you to secure your data online by using VPNS to hide what you do online. https://www.theengineer.info/2023/04/fang-businesses-for-jobs.html   The basic point of VPN is to protect what you do online this could be because of your career or shyness or many someone fears being reported to the government body for why they may feel the need to do it plus for some jobs being able to swap IPS can be a powerful thing to show you are working from say USA than where you are based in say Italy so showing IP can show you as well the internet very different as well since somethings are illegal depending where you live such as like Chat GPT is now not allowed to work on it  having IP from somewhere it is accepted is a good way to make of this powerful software.

Acer Laptop Reviews - Top Picks

Benefits of Acer ? Good value for money High quality Nothing goes wrong Reliable https://www.theengineer.info/2023/04/learn-basics-of-julia-programming.html  A lot of people forget about Acer value for many since they see terrible laptops such as one made from SGIN which can be cheaper sometimes but often break or give you shit value for money when you consider how often crash over made from china shit quality service wise overall. In  most Acers will be as good quality as a computer costing $100 to max of $1000 more from another brand such as like Sony or Alien Wear which often surprised me more people don't speak highly of  when  so many terrible brands exist such as like Lenovo probably worst creator of computers in the world now overall for no quality is the main thing they always do for why so many of them computers break so fast most of the time or compete with screen or error problems often . I have a few laptops from them which are over 10 years old still work fine I use th

FANG businesses for jobs

 A lot of firms like Facebook or Amazon all experience being in business with no competition or almost no competition plus unlimited funding it was more less impossible to fail for most of there business lifetime consider this if you could run any business and have unlimited funding supply for all of your business life or most of it then like 95% of them would probably become successful since  you could buy your success. Facebook made a lot of giant mistakes over META made me wonder if Mark really believed in this area or it was just a process to rebrand Facebook the reason to try and make it seem cool or interesting since Facebook saw mostly like old person site which makes sense since it has no real coolness under Zuckerberg way he acts. The idea of open META sounded to most people in the world silly when you consider the graphics they made were terrible and world of war craft has existed for a long time no one bragged about it being META game or META system but Mark did so much for