Any thoughts or conversation about JVM / JRuby / Android for Crystal

Hello !

Has there been any talk of implementing Crystal to run on the JVM / Dalvik / ART / Android stack ?

I dabble from time to time with Android development, and I keep coming back to how I hate Java. I have been doing a fair amount of work with React Native, and it’s pretty impressive. Sometimes though in my fantasy world I think, what would Android look like, with a programming language inspired by the Ruby I love and adore.

Thanks !

2 Likes

Did you try Kotlin? It has most of the feature the Crystal has. It’s great!

Hey!

Binding the NDK to Crystal would certainly be possible. You would need to implement the platform support for android (iirc the calling conventions are slightly different from normal arm-abi) and fix issues like https://github.com/crystal-lang/crystal/issues/9297.

Then of course there’s the whole topic of Crystal not being an excellent candidate for shared libraries, given it requires bdwgc. But maybe that’s managable.

Meanwhile, I’d recommend you to look into Kotlin or even Flutter (+ Dart) for mobile application development :) Flutter really brings some fresh winds, sidestepping most of the aging Android framework.

2 Likes

I have done a bit of Kotlin and didn’t get a whole lot of satisfaction so far. I’ll probably be doing it in my next gig so I’ll be hopefully learning more.

There’s been a few people that have tried this. This is the only one I remember off the top of my head https://github.com/ysbaddaden/android.cr

But if you wanted to take a stab at it, you could get some inspiration from http://ruboto.org/ See about porting some stuff to Crystal. That would be great to be able to do android apps in crystal.

2 Likes

@ether_joe I second @jhass recommendation. I eventually gave up on React Native and re-engineered my app (including the elements I had written in Kotlin) in Flutter/Dart. What a difference - a much better platform for developing Android apps than React Native IMHO with first class debug support in VScode. In particular I was able to integrate Leaflet based maps that worked and that was the principle reason I embarked on the re-implementation of my app. Functional programming and excellent streams support - there is a lot to like.

2 Likes

Will crystal ever support shared libraries? Is it possible with the crystal architecture?

Not in 1.0, and probably never.

3 Likes

It actually depends.
The language itself should actually work fine for a shared library. The hard part is the stdlib/runtime. It expects to be ruling things, not be part of something else.

If you are cross-compiling you get an object file that can be linked into a shared library, so the main difference is whether the program’s entry point is main or something else (ANativeActivity_onCreate by default). So writing an Android app in pure Crystal is definitely doable, as long as you ensure that the new entry point more or less mimics the runtime initialization in fun main, and that no other shared libraries use Crystal as it avoids the complication of linking multiple Crystal runtimes.

This is what it may look like (the unsafe class properties are defined here). Afterwards you can do anything you want at the top level, like using an app glue. See Hello World from Android ARM64 for details and also Add the ability to create a dynamic library · Issue #921 · crystal-lang/crystal · GitHub.

Naturally, most Java-specific functionality still has to be accessed through JNI bindings for Crystal. I haven’t looked into Ruboto yet but Ruby + Crystal for mobile development sounds quite attractive.

1 Like