Expand description

NOTE: This library is an internal crate of the Slint project.

WARNING: This crate does not follow the semver convention for versioning and can only be used with version = "=x.y.z" in Cargo.toml.

§Slint Android Activity Backend

This crate implements the Android backend/platform for Slint.

It uses the android-activity crate to initialize the app and provide events handling.

§Status

At the moment, this is a work in progress. In the future, we expect to add features directly to the the slint crate. In the mean time, it is already possible to use this crate to test Slint applications on Android.

An example of app using it is the todo example: https://github.com/slint-ui/slint/tree/master/examples/todo/rust In order to try it out, edit the Cargo.toml to uncomment the #wasm# lines, and add -p todo to the cargo apk command bellow.

§Usage

When using this crate into your project, be aware that it does not strictly adhere to semantic versioning (semver). This means breaking changes may be introduced in any patch release. It is crucial that the version of this crate matches the version of Slint you are using. To specify the exact version of this crate, include the = symbol in the version string.

You are required to add either the native-activity or the game-activity feature. The native-activity feature is a good starting point as it does not require Java stubs. However, it is more limited and may not work well with keyboard input. For more details, refer to the documentation of android-activity.

To create an Android build, your crate must be a library with the cdylib crate-type.

Below is an example of how to set up your Cargo.toml:

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

[dependencies]
slint = { version = "1.4.0", ... }
i-slint-backend-android-activity = { version = "=1.4.0", features = ["native-activity"] }

As with any application using android-activity, you need to implement the android_init function as #[no_mangle]. In it, create a AndroidPlatform and pass it to slint::platform::set_platform.

Here is an example:

#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app: i_slint_backend_android_activity::AndroidApp) {
    slint::platform::set_platform(Box::new(
        i_slint_backend_android_activity::AndroidPlatform::new(app)
    )).unwrap();

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

§Building and Deploying

To build and deploy your application, we suggest the usage of cargo-apk, a cargo subcommand that allows you to build, sign, and deploy Android APKs made in Rust.

You can install it and use it with the following command:

cargo install cargo-apk
cargo apk run --target aarch64-linux-android --lib

Please ensure that you have the Android NDK and SDK installed and properly set up in your development environment for the above command to work as expected.

Re-exports§

Structs§