Writing a mobile app framework


#1

Moin Leute,

I would like to develop mobile apps for Android/iOS in Rust because Rust is awesome, safe and is the only sane system + application programming language IMO. Sadly, I have no idea where and how I should start. Therefore I researched about frameworks and relatives for that task. I found NativeScript, Phonegap/Cordova, Rubymotion, Xamarin and Djinni.


The approach of Xamarin is the best in my opinion.
They wrote an abstraction layer for the native UI (https://www.xamarin.com/forms).
For access to sensors, camera etc. they have a system called DependencyService. I think it actually wraps platform-native calls (JNI on Android, something in ObjC on iOS).

Pros:

  • ~90% portable code
  • Easy to apply UI design (predefined layouts in XAML) which compiles to native UI for the specific platform

Cons:

  • Can’t be used on Linux
  • Inherited cons of C#

I’d like to have a similiar framework for Rust.

Questions to solve:

  1. Language direction: Should the platform-native language invoke Rust code? For example on Android: Java -> Rust code on demand? Or rather: Rust -> some API wrapping Android calls (JNI) -> Java
  2. According to 1. How do we share data structure between languages if we have to? (C++: Djinni uses its own language for this problem: github.com/dropbox/djinni)
  3. How can we abstract a Rustic interface over platform calls?
  4. How do we share threads between languages?
  5. How do we manage the UI part?
  6. Which crates would we need to write? Hierarchy?

My opinion to 5.:
I think people should be able to write Rust code at least for the core (in MVVM: Model + ViewModel). For the UI there should be either/and/or:

  1. an interface in Rust (View also in Rust).
  2. a way to just put an Android Studio / XCode project over the whole thing (seperate View) .

For (2) the tree directory could look like this:

.
├── Cargo.toml
├── src
│   └── main.rs
└── ui
    ├── android    <- Android Studio Project
    └── ios        <- XCode Project

To build the app for a specific platform you’d spin up cargo like this:

cargo app iPhone cargo app Android


BTW I don’t have much Rustic and mobile app programming experience. I can’t implement such a big task myself. Are you interested in doing it together?

With <3
xnor


#2

I do/did some mobile development with Rust.

At now for Android only, but have plans to port to other OSes.

I use approach Rust for core of system, and “native” for UI.
For Android “native” is Java.

With help of JNI it is possible to call Rust code from Java, and Java from Rust.

For iOS Objective C or Swift can call C functions directly, and so Rust also can be called directly, so it is more simple to integrate Rust with Objective C/Swift then with Java.

To automate generation of binding to call Rust code from Java I created rust_swig,
I plan to use it also to cooperate with Swift, from Rust’s structure/trait generate C functions
like foo_new, foo_method, and then generate Swift class, which hide this ugly C functions,
and allow to work with Rust traits like Swift class.

About building, at now cargo build my Rust code, and gradle build Java code,
and I have gradle task that call cargo, collect output .so and finally pack to apk.


#3

Would you like to share an example project of an app? Im very very interested!


#4

You can look at android-example inside rust-swig

Rust struct Session created in Java class MyApplication,
and then used for calculation inside Activity.


#5

Would be incredible to have a tool like that made for Rust with the ability to write the applications in Rust.

You may want to play around with https://www.gtk.org/features.php

It seems like it allows using Rust to develop mobile apps.

See also: https://github.com/gtk-rs/gtk


#6

Gtk doesn’t do mobile apps AFAIK. While I would love to see that happen, there are going to be folks who scream “‘NOOOO’,” and for good reason. Gtk is tailored for desktop apps only, and even though Wayland support in EGL/GL ES/whatever-gl-I-missed is here, we don’t have a responsive interface ready-to-go. If that were to happen, I’d probably die of a heart attack.

But again, I’d support the idea of “binding” these platform-specific framework APIs to a single API because at that point you could achieve “write once, run anywhere.”

I know some people (like the guy who developed Audacity) say they regretted developing a cross-platform GUI framework, and they say that “certain things feel out of place”, but I feel like if you write an agnostic cross-platform app gui framework that binds nearly 1:1 to said platforms’ framework, there shouldn’t be an issue.

Because I’m on Linux, an example between Gtk and Android would be the test case I’d like to see, but just because I’d like to see it doesn’t mean it will happen.

Edit: If the part about Gtk mobile apps is wrong (first paragraph), please correct me because that would be a FUN toy to play with!!

Edit 2: Attempted to fix all grammar and whatnot, added a new paragraph.


#7

It depends on what you mean by “responsive”.

Gtk widgets can be elastic: you can decide how much space they take on a row/column and they will adapt to the size of the containing window.

It’s a bit more complicated, but you could have 2 (or more) glade files (or UIBuilder files, as I think they are called now), and load one of them based on your constraint (OS, screen size, window size, etc.), like Android does, or simply define the whole interface programmatically.


#8

I think when I said “responsive” I may have meant “converging” at some point. Like watching the same code you wrote respond to three totally different modes of input and you don’t have to touch a single LoC.


#9

How is it going with this project? I’m about to write a mobile app and thinking if I should only do iOS since I know the platform, or use Xamarin and do cross platform. The best ever would be if there was a Xamarin equivalent that uses Rust :smiley: