Photo by Sophia Kunkel on Unsplash

This post is next part of the previous one - Krabik: Making Rust Mobile Experiments Less Painful. If you haven’t read that one already, take a look, maybe you will find it useful.

Three Approaches to Native Mobile UI

I’ve been thinking about writing UI for mobile in Rust for some time now and after giving it a though and some research I came up with three possible approaches I’d like to share.

The idea is relatively simple: keep application logic and UI architecture in Rust, but use native platform widgets to render the interface. On Android, that means Android Views. On iOS, UIKit.

Let’s start with Android because that’s what I’m more familiar with.

There are several ways to approach this problem. I looked at three possibilities:

  1. Use Android’s existing layout system.
  2. Build a custom layout engine using Rust and Taffy.
  3. Reuse Jetpack Compose as the Android rendering layer.

Each approach has some interesting advantages, but also introduces different architectural trade-offs.

1. Using Android’s existing layout system

The most straightforward approach is to let Android do what it already does well.

Android has several built-in layout containers:

  • LinearLayout
  • FrameLayout
  • RelativeLayout
  • ConstraintLayout

A Rust UI description could be translated into a hierarchy of native Android views and layout containers.

For example, consider a simple UI:

Column::new()
    .child(Text::new("Hello"))
    .child(
        Row::new()
            .child(Button::new("Cancel"))
            .child(Button::new("Save"))
    )

It could also be more enhaned with proc macros, similar to dioxus

ui! {
    Column {
        Text("Hello")

        Row {
            Button("Cancel")
            Button("Save")
        }
    }
}

The Android renderer could translate this into:

LinearLayout (vertical)
│
├── TextView
│
└── LinearLayout (horizontal)
    │
    ├── Button
    └── Button

The Rust side describes the interface, while Android handles measurement and positioning.

Pros

  • Less code to write - There is no need to implement own layout engine. Android already handles measurement, positioning, padding, margins, and many other layout concerns.
  • Native behaviour - The framework uses Android’s own layout mechanisms, allowing it to integrate naturally with the existing platform.
  • Straightforward implementation - This is probably the easiest approach for an initial prototype.
  • Good debugging experience - Developers can inspect the native view hierarchy using Android Studio’s Layout Inspector.

Cons

  • Platform-specific architecture - The layout implementation becomes tied to Android. In order to support iOS, I’d have to implement a separate implementation using UIKit and its own layout mechanisms.

    It might end up with something like:

    Rust UI
       │
       ├── Android renderer
       │     └── LinearLayout / FrameLayout
       │
       └── iOS renderer
             └── UIStackView / Auto Layout
    

    Both renderers would need to implement equivalent layout behaviour. It’s a solvable problem though, after creating both implementations, I could create a shared layout abstraction that represents the UI tree in a platform-agnostic way.

  • Layout container limitations - There might be some layout containers that don’t map cleanly to a shared cross-platform UI model. For example, a Rust Column might become a LinearLayout on Android, but what should it become on iOS?

  • View hierarchy complexity - Translating every layout node into a native container can produce deeply nested view hierarchies. This introduces additional native objects and layout work.

When does this approach make sense?

This approach makes sense when Android is the primary target. It would map nicely to Rust data structures and it would be straightforward to add new widget types.


2. Custom ViewGroup + Taffy

The second approach is to do custom layout system in Rust.

Instead of translating Column into LinearLayout or Row into another native container, the Rust runtime calculates the position and size of every element.

To implement something like this, I’d consider Taffy (Made by Dioxus team) or something similar - Torin, Yoga, etc. (https://arewelayoutyet.mespin.me/)

Taffy is a layout library written in Rust. It supports Flexbox, CSS Grid and block layout.

The architecture would look something like this:

             Rust
               │
               v
            UI Tree
               │
               v
             Taffy
               │
               v
        Layout calculations
               │
               v
          Rust Renderer
               │
             JNI
               │
               v
            Android
               │
         RustRootView
               │
        ┌──────┼──────┐
        │      │      │
      Text   Button  Image

The important distinction is that layout nodes don’t necessarily become native Android views.

For example:

Column
│
├── Text
│
└── Row
    │
    ├── Button
    └── Button

Could become:

RustRootView : ViewGroup
│
├── TextView
├── Button
└── Button

The Column and Row exist only in Rust. Taffy calculates their children’s positions and dimensions, and the renderer applies those coordinates to native widgets. It’s also worth noting that framework might elevate usage of native widgets like scrollables, to avoid implementing a complex scrolling system in Rust and preserve native feeling.

Pros

  • Platform-independent layout - The same layout engine can be used on Android and iOS.
  • Rust owns the architecture - The UI tree, layout calculations, reconciliation and application logic can all live in Rust. Kotlin and Swift become relatively thin platform integration layers.
  • Less native view nesting - Layout-only nodes don’t need corresponding Android views, helping keep the native view hierarchy relatively flat.
  • More platforms in the future - It’s not constrained by Android’s layout containers or the structure of an existing UI framework.

Cons

  • Framework needs to handle layout integration - Taffy calculates positions and dimensions, but it doesn’t automatically measure Android TextView or UIKit labels. The framework needs to implement a measurement interface that communicates with native platforms (Text is particularly interesting because its dimensions depend on available width, font metrics, line wrapping and other properties).

  • Native widgets aren’t always simple - Some widgets have complex internal behaviour. Text fields, scroll containers, dialogs and navigation components require more than simply setting their position and dimensions.

  • Accessibility requires deliberate design - Native widgets provide a lot of accessibility functionality, but layout-only nodes and custom component structures still need appropriate accessibility semantics.

When does this approach make sense?

When the goal is to build a Rust-first cross-platform framework and to own more of the infrastructure.

It offers the most architectural freedom, but also requires the most initial investment.


3. Reusing Jetpack Compose

The third option is to reuse an existing declarative UI framework.

Jetpack Compose already provides a modern UI architecture for Android.

It handles:

  • Declarative UI
  • State and recomposition
  • Layout and measurement
  • Modifiers
  • Animations
  • Material Design components
  • Accessibility

Instead of implementing all of this in Rust, framework could use Kotlin as a rendering layer.

Rust would describe the UI and communicate with Kotlin through JNI.

              Rust
                │
                v
             UI Tree
                │
                v
              JNI
                │
                v
              Kotlin
                │
                v
          Compose Runtime
                │
          ┌─────┼─────┐
          │     │     │
        Column  Text  Button

The Kotlin side would translate Rust UI descriptions into Compose components.

For example, a Rust description could be translated into something conceptually similar to:

@Composable
fun RustScreen() {
    Column {
        Text("Hello")

        Row {
            Button(onClick = {}) {
                Text("Cancel")
            }

            Button(onClick = {}) {
                Text("Save")
            }
        }
    }
}

Rust would remain responsible for application logic, while Compose would handle Android UI rendering.

Pros

  • A mature UI framework - Compose already solves many difficult problems. No need to implement own layout engine, recomposition mechanism or animation system.
  • Modern Android development - Access to modern Android UI APIs and Material 3 components.
  • Less low-level Android code - No need to manually manage every native view or implement own measurement integration.
  • Good developer experience - Compose has excellent tooling, previews and a well-established ecosystem.

Cons

  • Kotlin becomes an important part of the framework - Even if application developers write Rust, the framework itself depends heavily on Kotlin. Two languages in UI development, no way around it.

  • State synchronisation - One of the biggest architectural questions is how Rust state updates should trigger Compose recomposition. There would have to be a reliable communication mechanism between Rust and Compose.

    For example:

    Rust state update
           │
           v
    JNI notification
           │
           v
    Compose state update
           │
           v
    Recomposition
    

    There would essentially be building a bridge between two reactive UI systems.

  • Android-specific implementation - Compose is Android-specific. It would still need a separate rendering implementation for iOS, probably using SwiftUI or UIKit.

  • Potential abstraction leaks - Compose has its own concepts, including composables, modifiers, composition locals and effects. Framework would either expose these concepts through Rust API or hide them behind another abstraction. Hiding them might make some Compose functionality difficult to access.

When does this approach make sense?

When Android is the primary target and there’s a need toleverage the existing Android ecosystem rather than build own UI infrastructure.

Saying that, there might be a case to actually support this case additionally with the other approach for better integration.


4. Comparing the approaches

So summing things up, here’s a comparasion:

AreaAndroid layoutsTaffy + ViewGroupJetpack Compose
Rust ownershipPartialHighPartial
Layout engineAndroidTaffyCompose
Android native widgetsYesYesCompose components
Cross-platform layoutNoYesNo
Initial complexityLowHighMedium
Kotlin dependencyYesMinimalHigh
CustomisationMediumHighHigh
Native integrationDirectDirectFramework-mediated

There isn’t necessarily one universally correct answer. It depends on the usecase.

If the goal is to get a Rust application running on Android, using existing Android layouts is probably sufficient.

If the goal is to use Rust for application logic while leveraging the Android ecosystem, Compose is an interesting option.

But if the goal is to build a Rust-first cross-platform mobile framework, Taffy starts looking much more attractive.


5. What about React Native?

React Native is an interesting reference point here.

It uses Yoga for layout calculations and Fabric for rendering native platform views.

Conceptually, its architecture is quite close to the second approach:

JavaScript / React
        │
        v
     Shadow Tree
        │
        v
       Yoga
        │
        v
   Fabric Renderer
        │
        v
   Native Platform Views

Which is very similar to the second approach.


6. My current preference

After looking at these options, I’d lean towards the second approach:

Rust UI tree + Taffy + native platform renderers.

It’s very flexible and would give me flexbility to add more platforms in the future.

The architecture would look like this:

             Rust Application
                    │
                    v
               Public UI API
                    │
                    v
                 UI Tree
                    │
                    v
             Runtime / State
                    │
                    v
                  Taffy
                    │
                    v
             Platform Renderer
                    │
           ┌────────┴────────┐
           │                 │
           v                 v
        Android             iOS
           │                 │
       ViewGroup            UIView
           │                 │
     Native Views         UIKit Views

What would be the plan?

Try to generate small POC, then bigger one with AI and get the parts that are good and handcraft it.