Using JavaScript code from native code without using a WebView (sic!) can be really useful to
iOS
iOS offers the native JavaScriptCore:
Evaluate JavaScript programs from within an app, and support JavaScript scripting of your app.
So you can evaluate a script and read its return value natively:
context.evaluateScript(myLib);
let result = context.evaluateScript("myFunction();")
let myFunction = context.objectForKeyedSubscription("myFunction");
let parametrizedResult = myFunction.invokeMethod(...)
Or you can inject a native object into the JavaScript engine:
@objc protocol MyJSProtocol: JSExport {
var myVar: String {get set}
}
@objc class MyObject: NSObject, MyJSProtocol {
dynamic var myVar: String
}
...
context.setObject(MyObject.self, forKeyedSubscript: "MyObject")
Documentation JavaScriptCore
Android
For Android there are methods to inject native objects into the JavaScript context in a WebView; but you always have to initiate a WebView which is pretty heavy-weight.
There are third party libs that fill this gap:
- introduce dynamic functionality that was updatable without need of updating the app
- shorter iteration cycles
- share code between backend and native iOS and Android apps
You can have business logic provided by a JavaScript that then controls the app that inject native functionality in the JavaScript context.
You could even write an A/B testing framework based on this approach.
iOS
iOS offers the native JavaScriptCore:
Evaluate JavaScript programs from within an app, and support JavaScript scripting of your app.
So you can evaluate a script and read its return value natively:
context.evaluateScript(myLib);
let result = context.evaluateScript("myFunction();")
let myFunction = context.objectForKeyedSubscription("myFunction");
let parametrizedResult = myFunction.invokeMethod(...)
Or you can inject a native object into the JavaScript engine:
@objc protocol MyJSProtocol: JSExport {
var myVar: String {get set}
}
@objc class MyObject: NSObject, MyJSProtocol {
dynamic var myVar: String
}
...
context.setObject(MyObject.self, forKeyedSubscript: "MyObject")
Documentation JavaScriptCore
Android
For Android there are methods to inject native objects into the JavaScript context in a WebView; but you always have to initiate a WebView which is pretty heavy-weight.
There are third party libs that fill this gap:
- J2V8, a Java wrapper around Google’s V8 JavaScript engine.
- JS Evaluator for Android, based around the native Android WebView.
- AndroidJSCore, a Java wrapper around the WebKit JavaScriptCore engine.
- Duktape Android, the Duktape embeddable JavaScript engine packaged for Android.
Most stable and best performing candidate proved to be J2V8.
An Android app in JavaScript sounds like a fascinating approach to mobile development. Leveraging JavaScript allows developers to utilize their existing web development skills and tools, potentially streamlining the process. However, I'm curious about performance and native device integration. How does the app handle hardware features? Security concerns also come to mind. Nonetheless, it's an exciting prospect that opens up new possibilities. I'd love to see a detailed comparison with traditional Android development to better understand its benefits and limitations. Great post!
ReplyDelete