I have code which uses ScriptEngineManager, ScriptEngine class for executing JavaScript code using Java. But it works fine in Java SE, and doesn't work in Android - SDK show error of missing classes. Is it possible to execute JS code in Android? Thank you.
Javascript download free download - Learn JavaScript, DroidScript - JavaScript IDE, Android JavaScript Framework, and many more programs. Javascript download for android phones, Download Accelerator Plus 10, Download Accelerator Plus 10.0.5.3, Internet download manager IDM! Music software game. Javascript download for android phones. Windows Top Windows Mobile Mobile Top Mac Game. Windows software >> More. Nidesoft Android Video Converter is a great video converter for Google Android system, which could convert video to play on your Android devices. Download License: Shareware Downloads: 111 Category: windows - Video Tools - Video Converters.
10 Answers
AndroidJSCore is a great one. And here is another little library I wrote for evaluating JavaScript:
It creates a WebView behind the scenes. Works on Android version 3 and newer.
You can use Webview which inherits View class. Make an XML tag and use findViewById()
function to use in the activity. But to use the JavaScript, you can make a HTML file containing the JavaScript code. The example blelow might help.
Remember that the JS will run on WebView, not in native environment, thus you might experience a lag or slow FPS in emulator. However when using on an actual phone, the code may run fast, depending on how fast is your phone.
http://divineprogrammer.blogspot.com/2009/11/javascript-rhino-on-android.html will get you started. ScriptEngine is a java thing. Android doesn't have a JVM but a DalvikVM which is not identical but similar.
UPDATE 2018: AndroidJSCore has been superseded by LiquidCore, which is based on V8. Not only does it include the V8 engine, but all of Node.js is available as well.
Original answer:AndroidJSCore is an Android Java JNI wrapper around Webkit's JavaScriptCore C library. It is inspired by the Objective-C JavaScriptCore Framework included natively in iOS 7. Being able to natively use JavaScript in an app without requiring the use of JavaScript injection on a bloated, slow, security-constrained WebView is very useful for many types of apps, such as games or platforms that support plugins. However, its use is artificially limited because the framework is only supported on iOS. Most developers want to use technologies that will scale across both major mobile operating systems. AndroidJSCore was designed to support that requirement.
For example, you can share Java objects and make async calls:
You can use Rhino
library to execute JavaScript without WebView.
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.
You can see more details at my post.
I was also looking for a way to run javascript on Android and came across j2v8 library. This is a java wrapper for Google's v8 engine.
To use it add a dependency:
It has pretty simple api, but I haven't found any docs online apart from javadoc in maven repository. The articles on their blog are also useful.
Code sample from this article:
The javax.script package is not part of the Android SDK. You can execute JavaScript in a WebView, as described here. You perhaps can use Rhino, as described here. You might also take a look at the Scripting Layer for Android project.
Given that ScriptEngineManager and ScriptEngine are part of the JDK and Android SDK is not the same thing as the JDK I would say that you can't use these classes to work with JavaScript under Android.
You can check the Android SDK's reference documentation/package index to see what classes are included (what can you work on Android out of the box) and which of them are missing.
I just found the App JavaScript for Android
, which is the Rhino JavaScript engine for Java. It can use all Java-classes, so it has BIG potential. The problem is it might be slow, since it is not really optimized (heavy CPU load). There is another JavaScript engine named Nashorn, but that unfortunately doesn't works on Google's DalvikVM Java engine (does not support the optimizations of Oracle Java engine). I hope Google keeps up with that, I would just love it!
If you want to run some javascript code on chrome browser as per the question copy this code and paste it into address bar:
data:text/html, <html contenteditable>
<title>
Notepad </title>
<script>
alert('Abhasker Alert Test on Mobile'); </script>
</html>
Not the answer you're looking for? Browse other questions tagged javajavascriptandroid or ask your own question.
JsEvaluator may help you run JavaScript in an Android app and get the results. This is an alternative to evaluateJavascript
method of the WebView. Supports Android version 4.0 (Ice Cream Sandwich) and newer.
Setup
There are two ways your can add JsEvaluator to your project:
- From a remote Maven repository.
- From a local .aar or .jar file.
1. Setup from Maven repository in Android Studio
- Add
maven { url 'https://jitpack.io' }
into allprojects/repositories section of your project build.gradle file. For example:
- Add
compile 'com.github.evgenyneu:js-evaluator-for-android:v5.0.0'
into dependencies section of your module build.gradle file. For example:
For less information, see Gradle/Maven setup instructions on JsEvaluator jitpack.io page.
2. Setup from local .aar file
Download jsevaluator-1.0.aar. You can also build it yourself into jsevaluator/build/outputs/aar/ directory in Android Studio with this command:
Add aar file to Android Studio project
To add JsEvaluator to your app in Android Studio:
- Copy the
jsevaluator-1.0.aar
to your app/libs folder. - Add
compile(name:'jsevaluator-1.0', ext:'aar')
to dependencies block of your module build.gradle file.
- Add the following code to the allprojects/repositories block of your project build.gradle file.
Add jar file to Eclipse project
- Unzip the
jsevaluator-1.0.aar
and get the classes.jar from it. You may want to rename it to jsevaluator.jar. - Open properties for your app project.
- Select
Libraries
tab inJava Build Path
. - Click
Add JARs...
button and select the .jar file. - In your project properties click
Order and export
tab. - Tick the .jar file.
Usage
Create evaluator instance variable in your activity:
this
is a reference to your activity.
Evaluate JavaScript
Note: make sure to call evaluate
method in UI thread.
Call a JavaScript function
Any number of string, int or double parameters can be supplied.
Note: make sure to call callFunction
method in UI thread.
JavaScript is evaluated asynchronously
JavaScript is evaluated asynchronously without blocking UI thread. Result is returned in the UI thread. It is required to call evaluate
and callFunction
in UI thread.
JavaScript is evaluated in new context
Each time the JavaScript is evaluated in the new context. It can not access the result of a previous evaluation.Please concatenate all your JavaScript to one string and evaluate it in one go.
For example, if you need to load jQuery libary and then use it:
Advanced functionality
Destroying the evaluator
Calling the destroy()
method will destroy the Web View used by JsEvaluator and clear the memory. JsEvaluator can not be used after it is destroyed.
Accessing the WebView
Here is how to get the instance to the web view used by the JsEvaluator.
Known limitations
This library is suitable for evaluating only small amounts of JavaScript within hundreds of KB. It has been reported that the library can not evaluate a megabyte of JavaScript. If you run into similar problems you can try ericwlange/AndroidJSCore library instead.
How it works
Javascript Download For Android
Behind the scenes it creates a WebView
and feeds it JavaScript code for evaluation:
The result of evaluation is sent back into Android activity:
Catching JavaScript errors
This library catches errors by wrapping the whole JavaScript code in a try-catch block. The errors are then returned to Java in the onError
method of the callback object.
Please note that this method only catches runtime JavaScript errors, like undefined variables or properties. It will not, however, catch errors resulted from malformed JavaScript code, like missing a }
bracket.
Using with ProGuard
Javascript Download For Android Phone
If you are using ProGuard (minifyEnabled true
) you can add these rules to your proguard-rules.pro file.
You will most likely not need to modify your proguard file because JSEvaluator uses consumer proguard rules.
Tests
Tests are located in app
module of this project. The app can be run for manual testing as well.
Or run as Android JUnit Test
for unit testing.
Android versions tested:
- 4.0.3 (Ice Cream Sandwich)
- 4.1.2, 4.2.2, 4.3 (Jelly Bean)
- 4.4.2 (KitKat)
- 5.0, 5.1 (Lollipop)
- 6.0 (Marshmallow)
- 8.0 (Oreo)
Java Plugin For Android
Thanks 👍
- codebymikey for adding error handling.
- xbao for adding a Gradle file.
- clydzik for fixing a memory leak and simplifying the code.
Feedback is welcome
Javascript Free Download For Android Mobile
If you have any issues or need help please do not hesitate to create an issue ticket.