slint/android.rs
1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4// cSpell: ignore javac
5//! Android backend.
6//!
7//! **Note:** This module is only available on Android with the "backend-android-activity-06" feature
8//!
9//! Slint uses the [android-activity crate](https://github.com/rust-mobile/android-activity) as a backend.
10//!
11//! For convenience, Slint re-exports the content of the [`android-activity`](https://docs.rs/android-activity) under `slint::android::android_activity`.
12//!
13//! As with every application using the android-activity crate, the entry point to your app will be the `android_main` function.
14//! From that function, you can call [`slint::android::init`](init()) or [`slint::android::init_with_event_listener`](init_with_event_listener)
15//!
16//! # Example
17//!
18//! This is a basic example of an Android application.
19//! Do not forget the `#[unsafe(no_mangle)]`
20//!
21//! ```rust
22//! # #[cfg(target_os = "android")]
23//! #[unsafe(no_mangle)]
24//! fn android_main(app: slint::android::AndroidApp) {
25//! slint::android::init(app).unwrap();
26//!
27//! // ... rest of your code ...
28//! slint::slint!{
29//! #[slint_edition = "1.19"]
30//! export component MainWindow inherits Window {
31//! Text { text: "Hello World"; }
32//! }
33//! }
34//! MainWindow::new().unwrap().run().unwrap();
35//! }
36//! ```
37//!
38//! That function must be in a `cdylib` library, and you should enable the "backend-android-activity-06"
39//! feature of the slint crate in your Cargo.toml:
40//!
41//! ```toml
42//! [lib]
43//! crate-type = ["cdylib"]
44//!
45//! [dependencies]
46//! slint = { version = "1.6", features = ["backend-android-activity-06"] }
47//! ```
48//!
49//! ## Building and Deploying
50//!
51//! 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:
52//!
53//! ```sh
54//! rustup target add aarch64-linux-android
55//! ```
56//!
57//! Make sure that you have the Android NDK and SDK installed and set up in your development environment.
58//! For detailed instructions on how to set up the Android NDK and SDK, please refer to the [Android Developer's guide](https://developer.android.com/studio/projects/install-ndk).
59//! The following environment variables need to be set:
60//! * `ANDROID_HOME`: The directory in which your Android SDK is located. Usually `$HOME/Android/Sdk`.
61//! * `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.
62//! * `JAVA_HOME`: The directory in which your Java compiler (`javac`) is located. This variable is optional if a `javac` is found in your `$PATH`.
63//! Otherwise you can set `JAVA_HOME` to the `javac` installation shipped with Android Studio in `android-studio/jbr`.
64//!
65//! Slint doesn't require a specific tool to build, sign, and deploy the APK.
66//! The APK's activity must be `android.app.NativeActivity`, or a subclass of it,
67//! because that's the activity type the "backend-android-activity-06" feature supports.
68//! Tools that can package such an APK include:
69//!
70//! * [cargo-apk2](https://crates.io/crates/cargo-apk2): `cargo apk2 run --target aarch64-linux-android --lib`
71//! * [cargo-apk](https://crates.io/crates/cargo-apk): `cargo apk run --target aarch64-linux-android --lib`
72//! * [cargo-ndk](https://crates.io/crates/cargo-ndk) to build the library, with Gradle to package it
73//! * [xbuild](https://github.com/rust-mobile/xbuild): `x run --device <id>`
74//!
75# for how to set up each of them.")]
76
77/// Re-export of the android-activity crate.
78#[cfg(all(
79 target_os = "android",
80 any(feature = "backend-android-activity-05", feature = "backend-android-activity-06")
81))]
82pub use i_slint_backend_android_activity::android_activity;
83
84#[cfg(not(all(
85 target_os = "android",
86 any(feature = "backend-android-activity-05", feature = "backend-android-activity-06")
87)))]
88/// Re-export of the [android-activity](https://docs.rs/android-activity) crate.
89pub mod android_activity {
90 #[doc(hidden)]
91 pub struct AndroidApp;
92 #[doc(hidden)]
93 pub struct PollEvent<'a>(&'a ());
94}
95
96/// Re-export of AndroidApp from the [android-activity](https://docs.rs/android-activity) crate.
97#[doc(no_inline)]
98pub use android_activity::AndroidApp;
99
100use crate::platform::SetPlatformError;
101
102/// Initializes the Android backend.
103///
104/// **Note:** This function is only available on Android with the "backend-android-activity-06" feature
105///
106/// This function must be called from the `android_main` function before any call to Slint that needs a backend.
107///
108/// See the [module documentation](self) for an example on how to create Android application.
109///
110/// See also [`init_with_event_listener`]
111pub fn init(app: android_activity::AndroidApp) -> Result<(), SetPlatformError> {
112 #[cfg(not(target_os = "android"))]
113 unreachable!();
114 #[cfg(target_os = "android")]
115 {
116 crate::platform::set_platform(alloc::boxed::Box::new(
117 i_slint_backend_android_activity::AndroidPlatform::new(app),
118 ))?;
119 #[cfg(any(feature = "mcp", feature = "system-testing"))]
120 i_slint_backend_selector::init_testing_backends();
121 Ok(())
122 }
123}
124
125/// Similar to [`init()`], which allow to listen to android-activity's event
126///
127/// **Note:** This function is only available on Android with the "backend-android-activity-06" feature
128///
129/// The listener argument is a function that takes a [`android_activity::PollEvent`](https://docs.rs/android-activity/latest/android_activity/enum.PollEvent.html)
130///
131/// # Example
132///
133/// ```rust
134/// # #[cfg(target_os = "android")]
135/// #[unsafe(no_mangle)]
136/// fn android_main(app: slint::android_activity::AndroidApp) {
137/// slint::android::init_with_event_listener(
138/// app,
139/// |event| { eprintln!("got event {event:?}") }
140/// ).unwrap();
141///
142/// // ... rest of your application ...
143///
144/// }
145/// ```
146///
147/// Check out the [module documentation](self) for a more complete example on how to write an android application
148pub fn init_with_event_listener(
149 app: android_activity::AndroidApp,
150 listener: impl Fn(&android_activity::PollEvent<'_>) + 'static,
151) -> Result<(), SetPlatformError> {
152 #[cfg(not(target_os = "android"))]
153 unreachable!();
154 #[cfg(target_os = "android")]
155 {
156 crate::platform::set_platform(alloc::boxed::Box::new(
157 i_slint_backend_android_activity::AndroidPlatform::new_with_event_listener(
158 app, listener,
159 ),
160 ))?;
161 #[cfg(any(feature = "mcp", feature = "system-testing"))]
162 i_slint_backend_selector::init_testing_backends();
163 Ok(())
164 }
165}