使用 Rust 和 iced 构建 Android 应用
Build Android apps using Rust and Iced

原始链接: https://github.com/ibaryshnikov/android-iced-example

本文详细介绍了在Android上运行基于Rust的应用程序的进展,特别是使用`iced` GUI库。通过`android-activity` crate,Rust应用程序现在可以为Android构建,利用`winit`和`wgpu`等crate的支持。 虽然`iced`本身并不原生支持Android,但它可以与现有的图形管道集成。一个示例展示了如何将`winit + wgpu`应用程序转换为使用`iced`。 当前功能包括基本的文本输入、复制/粘贴,以及通过Java调用控制软键盘。然而,关于软键盘调整窗口大小、输入语言切换和IME支持等方面仍然存在问题。 构建过程涉及设置NDK和SDK的环境变量,使用`cargo-ndk`,并使用特定的目标架构(在本例中为x86_64)构建应用程序。该设置在Arch Linux上,使用特定内核、JDK和API版本进行了测试。

一位开发者成功使用 Rust 构建了一个 Android 应用,并分享了他们的经验,以鼓励 Rust 在移动平台上的更广泛应用。该项目利用了现有的 crates,如 `android-activity`、`jni-rs`、`wgpu` 和 `winit`,并基于 `rust-android-examples` 中的示例进行构建。 主要挑战是将跨平台 GUI 库 `iced` 集成,该库并未明确支持移动平台。开发者通过利用 `iced` 的 wgpu 集成示例以及 Android 设置,成功构建了一个可用的应用程序,但需要进行字体加载调整和触摸支持补丁。 该文章还介绍了其他用于 Android 开发的 Rust 选项,包括 `xilem`、`egui` 以及游戏引擎如 `Fyrox` 和 `Bevy`。开发者还计划探索类似的方法用于 iOS,预计过程类似。 此次成功证明了通过结合现有工具和社区贡献,使用 Rust 构建原生移动应用程序的可行性。
相关文章

原文

There are NativeActivity and GameActivity examples here.

Based on several other examples:

iced integration example

Pixel first screenshot Pixed second screenshot

You can also run most of the examples from iced. For this omit the scene rendering part and set the background of the root container.

Watch first Watch second Watch third

Text input partially works, unresolved issues:

  • window doesn't resize on show/hide soft keyboard
  • how to change input language of soft keyboard
  • ime is not supported

Copy/paste and show/hide soft keyboard is implemented by calling Java

Pixel third screenshot

Check android-activity crate for detailed instructions. During my tests I was running the following command and using android studio afterwards:

export ANDROID_NDK_HOME="path/to/ndk"
export ANDROID_HOME="path/to/sdk"

rustup target add x86_64-linux-android
cargo install cargo-ndk

cargo ndk -t x86_64 -o app/src/main/jniLibs/  build

My setup is the following:

  • archlinux 6.9.6
  • jdk-openjdk 22
  • target api 35

Thanks to android-activity we can already build android apps in Rust, and key crates such as winit and wgpu also support building for android. iced doesn't support android out of the box, but it can be integrated with existing graphics pipelines, as shown in integration example. As a result, it was possible to convert existing example running winit + wgpu to use iced on top.

联系我们 contact @ memedata.com