7

Is it possible to use JavaScript in Android?? if so, how? Please provide some examples.

Thanks.

2
  • 2
    Androids browser engine WebKit can run JavaScript. So you create a HTML file with embedded JS or some other referenced file and use it. Commented Nov 18, 2010 at 4:56
  • It's not java script. It's javascript, a language that has nothing whatsoever to do with java (in fact, C++ is more closely related to java than javascript is). Commented Nov 18, 2010 at 4:59

4 Answers 4

9

I am way late to the party here, but I had this exact need. iOS 7 now includes JavaScriptCore natively and it is really easy to use (despite limited documentation). The problem is that I didn't want to use it unless I could also use something similar on Android. So I created the AndroidJSCore project. It allows you to use your JavaScript code natively in Android without requiring a bulky WebView and injection. You can also seamlessly make asynchronous calls between Java and Javascript.

Update 27 Mar 17: AndroidJSCore has been deprecated in favor of LiquidCore. LiquidCore is based on V8 rather than JavascriptCore, but works essentially same. See the documentation on using LiquidCore as a raw Javascript engine.

From the documentation:

... to get started, you need to create a JavaScript JSContext. The execution of JS code occurs within this context, and separate contexts are isolated virtual machines which do not interact with each other.

JSContext context = new JSContext(); 

This context is itself a JavaScript object. And as such, you can get and set its properties. Since this is the global JavaScript object, these properties will be in the top-level context for all subsequent code in the environment.

context.property("a", 5); JSValue aValue = context.property("a"); double a = aValue.toNumber(); DecimalFormat df = new DecimalFormat(".#"); System.out.println(df.format(a)); // 5.0 

You can also run JavaScript code in the context:

context.evaluateScript("a = 10"); JSValue newAValue = context.property("a"); System.out.println(df.format(newAValue.toNumber())); // 10.0 String script = "function factorial(x) { var f = 1; for(; x > 1; x--) f *= x; return f; }\n" + "var fact_a = factorial(a);\n"; context.evaluateScript(script); JSValue fact_a = context.property("fact_a"); System.out.println(df.format(fact_a.toNumber())); // 3628800.0 

You can also write functions in Java, but expose them to JavaScript:

JSFunction factorial = new JSFunction(context,"factorial") { public Integer factorial(Integer x) { int factorial = 1; for (; x > 1; x--) { factorial *= x; } return factorial; } }; 

This creates a JavaScript function that will call the Java method factorial when called from JavaScript. It can then be passed to the JavaScript VM:

context.property("factorial", factorial); context.evaluateScript("var f = factorial(10);") JSValue f = context.property("f"); System.out.println(df.format(f.toNumber())); // 3628800.0 
Sign up to request clarification or add additional context in comments.

15 Comments

I just gave AndroidJSCore a try, and it seems to work quite well! I noticed however that it runs only on 32 bit machines. Are you planning on adding 64 bit support? Since most new phones are 64 bit, it is pretty much a showstopper...
Yes. 64-bit support is planned for the 2.1 release. It turns out that the compiler in the NDK is based on GCC 4.9.2. It has a bug related to atomics that JavaScriptCore depends on. I am working on a GCC 4.9.3 build which reportedly fixes the problem. Once I can get it to work, I will release it. Should be soon.
Oh, awesome! Thanks, I'm really looking forward to it.
AndroidJSCore 2.1 is now released, including 64-bit support. As an added bonus, I've extended support back to Jellybean, so 94% of devices are now covered.
Great! I'll try it out in a few days!
|
1

Do you mean something like making a native app using Javascript? I know there are tools like Titanium Mobile that let you make native apps using web tools/languages.

You could also make Web Apps. There are loads of resources and tutorials out there for that. Just search "Android Web App tutorial" or something similar.

Comments

1

Yes you can, just create a wrap up code that points to html page and includes your javascript and css.

There are different libraries that can help you with this:

Comments

0

Just posting this for posterity but React Native is a great solution in this space. You can write a complete app in JS using native views under the hood, or embed a JS component inside an existing Java app. https://reactnative.dev/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.