Latest way to make Android app?

I see some old posts about Android, but the way things change I figure I should ask again. Are there any guides on how to build an Android app, specifically an app using winit and wgpu?

With Xcode/iOS I build a staticlib crate, and use Xcode to create minimal app that calls the Rust entry point (an extern "C" fn) from the Objective-C main(). Don't know if I should try to do a similar thing with Android or if there are cargo commands or other tools that will help.

Have you checked out what the rust-mobile people are up to? They have ndk bindings and maintain cargo xbuild. I haven't tried them myself yet, but I'm rather excited to do so when I find the time.

2 Likes

Nope, but I will now. That looks promising.

The following makes me hesitant to use xbuild. It suggests it hasn't worked out too well.

When I tried cargo apk it gave me the kind of error message that makes want to quit using it immediately:

% cargo apk run -p my-rust-app                                  
Using package `my-rust-app` in `/MonoRepo/my-rust-app/Cargo.toml`
Error: Invalid args.

I wonder if it's possible to use straight and simple cargo like I did with iOS. For that I built a static library:

cargo build -p my-rust-app --target aarch64-apple-ios

That I used that lib in an Xcode project. I don't mind using Xcode or Android Studio to create a simple shell app. Unless the rust command line tools are 100% done and well documented, I think those IDEs will make it easier to do the Apple/Android specific stuff for running on devices or creating store upload packages.

But when I try to do the same with Android target, I get a linker error:

cargo build -p my-rust-app --target aarch64-linux-android 
... [building stuff] ....
error: linker `/Users/rob/Library/Android/sdk/ndk/27.2.12479018/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-clang` not found

That folder does have clang files in it that almost match.

ls /Users/[...]/darwin-x86_64/bin/aarch64-linux*
/Users/[...]/darwin-x86_64/bin/aarch64-linux-android21-clang
/Users/[...]/darwin-x86_64/bin/aarch64-linux-android21-clang++
...
/Users/[...]/darwin-x86_64/bin/aarch64-linux-android35-clang
/Users/[...]/darwin-x86_64/bin/aarch64-linux-android35-clang++

Maybe I need to set something more in my Cargo.toml? Maybe it's not possible to build for Android like this? If it is possible, I'd prefer this simpler path, so I can do the "run on device" step, or "build signed APK" step, from Android studio, at least for now.

If I must use cargo-apk or xbuild then I'll persist in trying to learn those tools.

I may have found what I'm looking for here with cargo-ndk.

cargo install cargo-ndk  
cargo ndk -t arm64-v8a -o ./jniLibs build -p my-app 

That created a .so native library, as desired. But it's going to take me some time to see how to call that from the shell app I create in Android Studio.

1 Like