いくつか方法はあるようだが、ここでは WebView + rust target=android の方式。
System API は file と network(https) を試した。 コードは Rust で。 Rust の std/reqwest API で済んでいて、NDK や Java 関数呼び出しは直接書いていない(std 内部では呼んでるかもしれない)。
ビルド環境は cargo-mobile2 を使う。
https://github.com/dai1975/etude-androidapp-rust
aarch64 の release ビルドの apk は 21MB ほど。
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
cargo install --git https://github.com/tauri-apps/cargo-mobile2
普通に cargo new
後、cargo mobile init
する。
cargo mobile init
Detected template packs:
[0] bevy
[1] bevy-demo
[2] egui
[3] wgpu
[4] winit
[5] wry
色々な描画方法に対応している。 ここでは WebView 使うので wry.
WebView 使う GUI フレームワークには、android 環境のドキュメントが充実していた Dioxus を試した。 React ライクに書けるやつ。
cargo add dioxus
cargo add dioxus-desktop --no-default-features --features tokio_runtime
生成された Cargo.toml の wry が古かったので最新に。
少なくとも dioxus v0.4.0 では、issue 1525 にあるように、 issune cx.needs_update で止まるバグがある。 fix されてるブランチを明示的に使う。
dioxus = { git = "https://github.com/ealmloff/dioxus", branch = "bump-wry" }
dioxus-mobile = { git = "https://github.com/ealmloff/dioxus", branch = "bump-wry" }
dioxus-desktop = { git = "https://github.com/ealmloff/dioxus", branch = "bump-wry" }
使ってる wry のバージョンも違うようなので変更。
wry = "0.34.0"
明示的に openssl crate 必要。
openssl = { version = "0.10.59", features = ["vendored"] }
さらに、ビルド時に ranlib が無いと言われる。環境変数 RANLIB を指定。
export RANLIB=$(NDK_HOME)/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ranlib
普通に Rust の io が使える。
アプリ用の領域は /data/user/0/<app package name>
.
本来はこのパスは API から取るようだが Java にしかない? ぽくて面倒なので直書き。
reqwest crate でアクセスできた。
SSL も crate をビルド。 (TODO: android の提供してるだろう標準ライブラリは使えないのかな?)
cargo android run -f Debug