use rust druid build ios and android app

249 阅读1分钟

install rust and toolchain

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Android targets
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android

# iOS targets
rustup target add aarch64-apple-ios armv7-apple-ios armv7s-apple-ios x86_64-apple-ios i386-apple-ios

Tools for iOS

# install the Xcode build tools.
xcode-select --install
# this cargo subcommand will help you create a universal library for use with iOS.
cargo install cargo-lipo
# this tool will let you automatically create the C/C++11 headers of the library.
cargo install cbindgen

Tools for Android

For Android, we have to be sure that we have correctly set up the $ANDROID_HOME environment variable. In macOS this is tipically set to ~/Library/Android/sdk.

It is also recommended that you install Android Studio and the NDK. Once you have everything installed, ensure that the $NDK_HOME environment variable is properly set. In macOS this should be tipically set to ~/Library/Android/sdk/ndk-bundle.

Finally, we’re just going to install cargo-ndk, which handles finding the correct linkers and converting between the triples used in the Rust world to the triples used in the Android world:

cargo install cargo-ndk

create project

cargo new rust_mobile_app
cd rust_mobile_app

Next, add the necessary dependencies in the Cargo.toml file. For this example, let's use druid:

[dependencies]
druid = "0.7"

Now, in the main.rs file, create a basic window:

use druid::widget::Label;
use druid::{AppLauncher, WindowDesc, Widget};

fn main() {
    let main_window = WindowDesc::new(ui_builder).title("Rust Mobile App");
    AppLauncher::with_window(main_window).launch(()).expect("Failed to launch app");
}

fn ui_builder() -> impl Widget<()> {
    Label::new("Hello, Rust!")
}

install cargo-apk

cargo apk build