9

I am new to Android programming, and looking for some general knowledge. I am considering writing logic of my application in javascript so that the same code could be executed in a webapp and in a desktop application. Would it be possible to also have it working on Android? I know that:

  • SL4A is marked as alpha-quality, and user would need to install it to make such an app work. Still it provides access to Android API. SL4A scripts also cannot go to Android Market, as far as I know.
  • A simple webapp doesn't have access to most Android API.

Would it be possible to write a simple Java app that would embed an HTML widget with javascript code and provide some wrapper to access necessary API?

I am not looking for a fully portable thing--I intend to adapt UI to each environment manually. I just would like to have the internal logic common to all ports.

4 Answers 4

11

PhoneGap allows you to write an HTML-based app that not only works with Android, but also iPhone, Windows Phone 7, WebOS and more. The API is standardized, so you can use the same page on all the platforms.

There's support for the most common native features on most platforms. (Here's a chart of the features supported on each platform) In addition, if you find that there's a feature you cannot replicate using only their API, you can write a plugin in the native platform language (so, for Android that'd be Java), and then call that plugin from your HTML/Javascript-page.

3
  • Great! Is it possible to use native UI too? Intents on Android?
    – liori
    Feb 8, 2012 at 20:50
  • @liori: You can mix the HTML activities with standard Android activities as you please, so you can mix HTML with native UI, yes. Listening for intents can be done the standard Android way. Sending intents can be done through a plugin (example). Feb 8, 2012 at 21:10
  • 1
    If you're only interested in the free and open source part of PhoneGap, check out Apache Cordova which is contributed to the ASF by Adobe. (info)
    – andrew-e
    Aug 22, 2015 at 0:52
8

If you want to use javascript and access the native api then you should try Appcelerator.com. Those people are providing this.

1
  • Very interesting. I was looking for something closer to Android itself, but this might also fit the bill.
    – liori
    Dec 9, 2010 at 21:04
2

There is Rhino, which is a Javascript engine written in Java. It works in Android, and it is used by Appcelerator's Titanium mentioned in another answer here.

User interface and Android-specific API can then be written and wrapped in Java, then called by the logic code written in Javascript and run by Rhino.

0
0

Consider GWT, a Java to javaScript compiler. You can write your logic and a lot of other code in plain old Java(There are a few things(e.g. reflection) that you can't do like reflection but you wouldn't be able to do it in javaScipt either) Applications like Google inbox are using GWT to reuse a lot of their code in javaScript. They don't just reuse logic either. You can reuse dependency injection, your architecture, AJAX calls and more. Also, GWT is faster than javaScript in both the browser and the JVM. The biggest problem you might have with GWT is that it's more complicated javaScript. Regular Java is already more complicated. Making it work on both the JVM and browser can only make things more complicated. Also, GWT was designed from the ground up for extremely complciated web apps.

1
  • 1
    The question was about evaluating choices with JavaScript-based solutions, not Java-based ones. Therefore, GWT is not a valid answer here.
    – liori
    Feb 3, 2015 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.