Jasonette / JASONETTE-Android

πŸ“‘ Native App over HTTP, on Android
https://www.jasonette.com
Other
1.61k stars 270 forks source link

Use Webview as the JS engine for Jasonette JSON processing #162

Open maks opened 7 years ago

maks commented 7 years ago

Currently j2v8 is used to process jasonette JSON using parser (written in JS) as well as for the convert CSV/RSS -> JSON action. This brings in a large shared native library into the APK, that is then needed for each architecture supported by Jasonette android app (currently ony ARM and x86). Instead the Android Webview can be used for this as it already ships with v8 (except in older devices where JSC is used) and from API19 the evaluateJS() method is available which provides all the functionality currently used by jasonette from j2v8. For android versions older than 19, the loadUrl() method can be used BUT in this case parser.js and csv/rss parsers will need to be modified to return values via the JS->Java interface. eg.

var wrapped_json = function(template, data, json) {
  var result =  json(template, data, json);
  if (JAVARESULT) {
     JAVARESULT.result(result);
  }
  return result;
}

Where JAVARESULT woould be a Java object added via the webview.addJavascriptInterface() to the webview and has a result(String s) method.

Added benefit is that the webview being used to process the jasonette json can be remotely debugged via Chrome Dev Tools.

maks commented 7 years ago

@gliechtenstein give that the JS files for parser, csv and rss are currently browserified version in the android src, I think it would be best to edit the sources for them that you have and regenerate the browserified versions from your original sources.

gliechtenstein commented 7 years ago

looking into this

maks commented 7 years ago

@gliechtenstein actually I've realised there is a way for me to shim this for older versions of Android so you won't need to make any changes to your existing parser, csv, rss JS code.

gliechtenstein commented 7 years ago

@maks i've been looking at this and kind of figured out how to make it work but wasn't satisfied because it would be ideal if we never have to touch the original JS library. Sounds like you have exactly that! Looking forward to that solution πŸ‘πŸ‘πŸ‘

Anyway, just posting here what I've wrote so far for future reference : https://github.com/maks/JASONETTE-Android/pull/1

realitix commented 7 years ago

Hello @maks,

Can you tell us more about the advantages of this change.

Have you better performance (and can you give us micro-benchmark) ?

Thanks in advance !

maks commented 7 years ago

@realitix Sure.

The main driver for this change is reducing the size of the Jasonette APK rather than improving runtime performance, though I do not expect performance to be significantly impacted by this change.

By removing the dependency on j2v8, which needs to include a native library (of the v8 runtime) for each architecture supported by the apk (currently arm & x86_64) the APK size is reduced by approximately 11MB down to 4.8MB for the debug apk at the moment. This is especially important for using with Jasonette for Android Instant Apps where there is currently a hard limit of 4MB on the APK As a side note, there is another ~1MB that can be trimmed easily from the current APK by removing a mp3 file that is being bundled in via a dependency from the audio-recording library being used atm by Jasonette too.

I plan to do some benchmarking on this soon to ensure there is no significant perf drop in making this change, but in my initial subjective manual testing I have not seen any. If you have an example Jason files that could be used for benchmarking I would be most happy to try them out.

realitix commented 7 years ago

Thanks @maks for this explanation. Indeed it's a nice improvement!