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

Hackers, opponents or users, where does Apple’s interest come from?

Apple continued to appear in court on Wednesday t...

Baidu direct e-commerce product selection and marketing practical skills!

This article contains: 1) Overview of the direct-...

Parent-child experience activity planning (commercial operation case)

The author of this article shares an activity pla...

The soul of digital marketing is user portrait

01 Digitalization without user portraits is just ...

Mid-Autumn Festival brand marketing planning program!

For ordinary people, Mid-Autumn Festival is a goo...

Data Structure of Information Olympiad

Brief introduction to the data structure resource...

Weibo Fans Advertising and Charging Standards!

1. Introduction to Fantong What is Fantong? To pu...

5 operational lessons learned from Sina Weibo's second rise

Regarding Sina Weibo , let me first show you thre...

QQ launches youth mode for mobile version: a simpler social experience

Recently, Tencent has taken into account that sch...

Life is too "sad", do these 5 things to make your heart healthier

We usually pay attention to various major and min...