Running ClojureScript on Android

Running ClojureScript on Android

[[151938]]

Over the past few days, I've had one of the most rewarding experiences of my development career, and I wanted to share it with you.

We now have ClojureScript running on Android. Not in a WebView, not using something like Cordova, but actually running in an embedded JavaScript engine. What's going on?

We recently did some work to create a ClojureScript REPL app for iOS, Replete. It looks pretty cool, but the Android counterpart is only just barely buildable. Unfortunately, my Android skills are mediocre and I wasn't able to get started right away.

However, Tahmid Sadik's skills in this area are still up to par.

Tahmid could string the UI together and instantiate Rhino and evaluate JavaScript statements, but getting the bootstrapped ClojureScript to work was a challenge.

Before that, though, he has to bootstrap ClojureScript in his app. I’ve specified the word bootstrap to indicate that he needs to bootstrap the ClojureScript runtime using Google’s Closure dependency management system, without JavaScript and without other optimizations (e.g. :none mode), by defining the CLOSURE_IMPORT_SCRIPT environment variable as needed. Importing the runtime this way is important if you want to include a REPL. This also provides support for including source-level namespaces in your REPL.

Actually, Replete also needs to do this, using a feature of Ambly. Of course, I know that Replete is a standalone REPL and does not need Ambly. But I used a little trick to reuse this feature and let ClojureScript start running in Replete.

By the way, I originally planned to use React Native to implement Replete. It turned out that it was too early to use React Native's ClojureScript support, and the bootable ClojureScript was indeed just an early feature, and it would take a lot of effort to make it work. So, for Replete, I kept it simple, and even Goby was not used.

The good thing about the above results is that there is no need for React Native dependencies (Android version has not been officially released yet) and no Goby dependencies (only supports iOS). Replete's built-in ClojureScript is very concise and uses the traditional iOS UI, which proved to be very helpful for Tahmid to complete the Android version of the same function.

Let's go back to the beginning of the story: Tahmid essentially copied Ambly's bootstrap logic and executed the JavaScript statements in Rhino one by one in order. However, he would encounter some strange problems from time to time. I remember that I encountered a similar problem a few months ago. After studying Ambly's code, I gave some suggestions.

After that, he can basically guide the successful ClosureScriptle.

  1. cljs.core.apply.call( null ,cljs.core.inc, new cljs.core.PersistentVector( null , 1 , 5 , cljs.core.PersistentVector.EMPTY_NODE, [ 1 ], null ))

This line is the JavaScript code that needs to be bound (apply inc [1])

Then Tahmid finally had 2.0. Awesome! This was probably the first time ever that ClojureScript ran on Rhino embedded on Android.

Next, you need to try using the reader, analyzer, and compiler. Let's try it now. Simply use the JavaScript in Replete and let the Android app execute Replete's read_eval_print function with the string (+ 1 2) as the parameter. If it works, then congratulations, ClojureScript has been successfully started.

  1. replete.core.read_eval_print.call( null , '(+ 1 2)' )

Wait a minute, Transit has a problem with goog.require('replete.core');, probably related to randomUUID. Sad, it seems there is a lot of work to do.

But that’s okay, Replete had been experimenting with different ways to load the analysis cache, and thanks to Karl Mikkelsen, we had a working version that used pure JavaScript and no dependencies. After getting this working and getting the print callback right (so methods like println would work), Tahmid notified me via Slack:

  1. I got 3  
  2.  
  3. (+ 1   2 ) = 3  

... and kept annoying me and blogging about it without my permission. And just like that, the bootstrapped ClojureScript REPL on Android was born!

Tahmid wrapped some stuff up in the interface, fixed some minor issues with the JavaScript/ClojureScript integration, and released Replicator.

It's a complete storm!

Now that Tahmid is replacing Rhino with JavaScriptCore, that's not going to happen so fast.

I think that will give us some important foundation for native switching capabilities using JavaScriptCore on Android.

Looking at the big picture, I really think ClojureScript runs fast on Android. Especially with JavaScriptCore. For a question on this point of view, take a look at the difference in startup speed of Vladimir Iakovlev's port of Bocko to Android.

  1. Clojure ~ 14 seconds
  2.  
  3. Clojure / Skummet ~ 11 seconds
  4.  
  5. ClojureScript ~ 2 seconds

The above results are from running on a simulator, but I still think it shows that ClojureScript really delivers on its promise of reducing computation latency on mobile devices. I think it’s time to get ClojureScript active and use it for mobile device development!

<<:  Google wants to dig deeper into mobile Internet and acquires HTML5 development platform Divshot

>>:  What are the top ten mistakes Jack Ma made in the past 15 years?

Recommend

5 common methods of e-sports marketing!

EDG is the champion! In the League of Legends S11...

Cook hints at new iWatch feature: voice control

Apple CEO Cook has hinted many times that they wil...

How much is one gram of Cordyceps sinensis in Nagqu, Tibet in 2020

Cordyceps sinensis has been a very nourishing med...

Predicting Flu Trends: Harder Than You Think

Even if you were given a forecast now about the t...

Can't buy vitamin C? Don't panic, these foods are very good for vitamin C!

With the relaxation of new epidemic prevention po...

What will the 315 Gala expose in 2020? Why was the 315 Gala postponed?

2020 315 Gala Time: 8pm, July 16, 2020 2020 315 G...

How to choose Wenchang Tower?

1. First of all, the jade Wenchang Tower has the ...

A day's plan begins in the morning. How should you eat breakfast?

As the Chinese saying goes, a day's plan begi...

The product promotion logic of Internet advertising!

What the advertising business sells is every one ...