Skip to main content

Module android

Module android 

Source
Available on Android and (crate features backend-android-activity-05 or backend-android-activity-06) only.
Expand description

Android backend.

Note: This module is only available on Android with the “backend-android-activity-06” feature

Slint uses the android-activity crate as a backend.

For convenience, Slint re-exports the content of the android-activity under slint::android::android_activity.

As with every application using the android-activity crate, the entry point to your app will be the android_main function. From that function, you can call slint::android::init or slint::android::init_with_event_listener

§Example

This is a basic example of an Android application. Do not forget the #[unsafe(no_mangle)]

#[unsafe(no_mangle)]
fn android_main(app: slint::android::AndroidApp) {
    slint::android::init(app).unwrap();

    // ... rest of your code ...
    slint::slint!{
        export component MainWindow inherits Window {
            Text { text: "Hello World"; }
        }
    }
    MainWindow::new().unwrap().run().unwrap();
}

That function must be in a cdylib library, and you should enable the “backend-android-activity-06” feature of the slint crate in your Cargo.toml:

[lib]
crate-type = ["cdylib"]

[dependencies]
slint = { version = "1.6", features = ["backend-android-activity-06"] }

§Building and Deploying

Building a Rust application requires the target toolchain to be installed. You can install it via rustup. For example, to target AArch64 Android, use the following command:

rustup target add aarch64-linux-android

Make sure that you have the Android NDK and SDK installed and set up in your development environment. For detailed instructions on how to set up the Android NDK and SDK, please refer to the Android Developer’s guide. The following environment variables need to be set:

  • ANDROID_HOME: The directory in which your Android SDK is located. Usually $HOME/Android/Sdk.
  • ANDROID_NDK_ROOT: The directory in which your Android NDK is located. Usually $HOME/Android/Sdk/ndk/${NDK_VERSION}. ${NDK_VERSION} is the version of the NDK you have installed.
  • JAVA_HOME: The directory in which your Java compiler (javac) is located. This variable is optional if a javac is found in your $PATH. Otherwise you can set JAVA_HOME to the javac installation shipped with Android Studio in android-studio/jbr.

Slint doesn’t require a specific tool to build, sign, and deploy the APK. The APK’s activity must be android.app.NativeActivity, or a subclass of it, because that’s the activity type the “backend-android-activity-06” feature supports. Tools that can package such an APK include:

  • cargo-apk2: cargo apk2 run --target aarch64-linux-android --lib
  • cargo-apk: cargo apk run --target aarch64-linux-android --lib
  • cargo-ndk to build the library, with Gradle to package it
  • xbuild: x run --device <id>

See the Android platform guide for how to set up each of them.

Modules§

android_activityNot (Android and (backend-android-activity-05 or backend-android-activity-06))
Re-export of the android-activity crate.

Functions§

init
Initializes the Android backend.
init_with_event_listener
Similar to init(), which allow to listen to android-activity’s event