The answer to "how do I use Rust on iOS and Android?"

cargo-mobile takes care of generating Xcode and Android Studio project files, building and running on device, generating project boilerplate, and a few other things!

cargo-mobile

The answer to "how do I use Rust on iOS and Android?"

cargo-mobile takes care of generating Xcode and Android Studio project files, building and running on device, generating project boilerplate, and a few other things!

Check out the announcement post!

Status

Everything here works and is already used internally! However, this hasn't seen a lot of external use yet, so there could still be some rough edges.

Building for iOS is broken on Rust 1.46.0, 1.47.0, and 1.48.0!

Sorry for the inconvenience! This is resolved in Rust 1.49.0, so please update to that if you haven't already:

Installation

The build will probably take a bit, so feel free to go get a snack or something.

cargo-mobile is currently supported on macOS and Linux. Note that it's not possible to target iOS on platforms other than macOS! You'll still get to target Android either way.

A PR adding Windows support would be hugely appreciated!

You'll need to have Xcode and the Android SDK/NDK installed. Some of this will ideally be automated in the future, or at least we'll provide a helpful guide and diagnostics.

Whenever you want to update:

Usage

To start a new project, all you need to do is make a directory with a cute name, cd into it, and then run this command:

After some straightforward prompts, you'll be asked to select a template pack. Template packs are used to generate project boilerplate, i.e. using the bevy template pack gives you a minimal Bevy project that runs out-of-the-box on desktop and mobile.

name info
bevy Minimal Bevy project derived from sprite example
bevy-demo Bevy breakout demo
wgpu Minimal wgpu project derived from hello-triangle example
winit Minimal winit project derived from window example

Template pack contribution is encouraged; we'd love to have very nice template packs for Bevy, Amethyst, and whatever else people find helpful! We'll write up a guide for template pack creation soon, but in the mean time, the existing ones are a great reference point. Any template pack placed into ~./cargo-mobile/templates/apps/ will appear as an option in cargo mobile init.

Once you've generated your project, you can run cargo run as usual to run your app on desktop. However, now you can also do cargo apple run and cargo android run to run on connected iOS and Android devices respectively!

If you prefer to work in the usual IDEs, you can use cargo apple open and cargo android open to open your project in Xcode and Android Studio respectively.

For more commands, run cargo mobile, cargo apple, or cargo android to see help information.

Android

cargo android run will build, install and run the app and follows device logs emitted by the app.

By default, warn and error logs are displayed. Additional logging of increasing verbosity can be shown by use of the -v or -vv options. These also provide more verbose logging for the build and install steps.

For fine-grained control of logging, use the --filter (or -f) option, which takes an Android log level, such as debug. This option overrides the default device logging level set by -v or -vv.

If using the android_logger crate to handle Rust log messages, trace logs from Rust are mapped to verbose logs in Android.

Issues

Collection of the latest Issues

rib

rib

0

When first running cargo android run I found that it tried to run adb internally but didn't find the executable (since it's not in my PATH). It could be good if cargo-mobile didn't depend on the PATH being configured for SDK tools since it should be able to find them based on the the SDK path instead I think.

rib

rib

0

Hi,

I was just looking at giving cargo-mobile a try and one initial little thing I noticed was that it didn't seem to recognise ANDROID_NDK_ROOT or ANDROID_NDK_HOME which I think are both standard environment variables for locating an Android NDK. (Normally I think I expect to switch between either of those and haven't used NDK_ROOT before)

For the SDK it also look cargo mobile considers ANDROID_HOME to be deprecated vs ANDROID_SDK_ROOT, which is incidentally the opposite of these docs: https://developer.android.com/studio/command-line/variables

I guess at this point there's not really any standard for these (lots of conflicting claims about which ones are standard) and I wonder if it could be most practical if cargo-mobile would just try all of these and maybe just show where the SDK/NDK are discovered (and by which variable) without needing to warn that any of the others are deprecated.

SDK:

ANDROID_SDK_HOME ANDROID_SDK_ROOT ANDROID_HOME

NDK:

ANDROID_NDK_HOME ANDROID_NDK_ROOT NDK_ROOT

E.g. in my case I already had my environment set up with ANDROID_NDK_ROOT and ANDROID_HOME before running cargo mobile init

chengchangwu

chengchangwu

0

While I execute cargo android run, it failed with the following message,

It failed because I'm developing for an android STB which often have no bluetooth.

jkelleyrtp

jkelleyrtp

1

Eventually in Dioxus we plan to add cargo-mobile-ish support in our CLI but in the meantime it's been easier to just recommend cargo-mobile for users interested in building mobile apps.

I'd like to add templates for Wry, Tao, and Dioxus now that Tao works on both Android and iOS.

I have had success with Tao/Wry/Dioxus - all that's left is building the template.

Screen Shot 2022-04-16 at 6 57 57 PM

songlinshu

songlinshu

1

: Host platform not yet supported by cargo-mobile! We'd love if you made a PR to add support for this platform ❤️ --> src\os\mod.rs:16:1 | 16 | compile_error!("Host platform not yet supported by cargo-mobile! We'd love if you made a PR to add support for this platform ❤️"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0432]: unresolved import crate::os::command_path --> src\util\mod.rs:11:23 | 11 | use crate::os::{self, command_path}; | ^^^^^^^^^^^^ no command_path in os

error[E0433]: failed to resolve: could not find info in self --> src\os\mod.rs:28:15 | 28 | self::info::check() | ^^^^ could not find info in self

error[E0433]: failed to resolve: could not find Application in os --> src\util\mod.rs:583:9 | 583 | os::Application::detect_editor() | ^^^^^^^^^^^ could not find Application in os

error[E0425]: cannot find function open_file_with in module os --> src\android\cli.rs:163:17 | 163 | os::open_file_with("Android Studio", config.project_dir()).map_err(Error::OpenFailed) | ^^^^^^^^^^^^^^ not found in os

error[E0412]: cannot find type DetectEditorError in module os --> src\util\mod.rs:568:22 | 568 | DetectFailed(os::DetectEditorError), | ^^^^^^^^^^^^^^^^^ not found in os

error[E0412]: cannot find type OpenFileError in module os --> src\util\mod.rs:569:20 | 569 | OpenFailed(os::OpenFileError), | ^^^^^^^^^^^^^ not found in os

Some errors have detailed explanations: E0412, E0425, E0432, E0433. For more information about an error, try rustc --explain E0412. error: could not compile cargo-mobile due to 7 previous errors warning: build failed, waiting for other jobs to finish...

stable-i686-pc-windows-gnu stable-i686-pc-windows-msvc stable-x86_64-pc-windows-gnu stable-x86_64-pc-windows-msvc (default) nightly-x86_64-pc-windows-gnu nightly-x86_64-pc-windows-msvc 1.60.0-x86_64-pc-windows-msvc

installed targets for active toolchain

wasm32-unknown-unknown x86_64-pc-windows-msvc

active toolchain

stable-x86_64-pc-windows-msvc (default) rustc 1.60.0 (7737e0b5c 2022-04-04)

jkelleyrtp

jkelleyrtp

3

I can't seem to build for the simulator using the template generated for macOS m1.

I tried to change the x86_64 target to aarch64 but then ran into:

building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

I haven't quite figured out the right way of getting things to build, but at the very least, the template is busted for m1.

As an aside: thank you for doing all this work. It's pretty amazing. Happy holidays!

alecnotthompson

alecnotthompson

type: bug
1

I seem to have had a bad android studio setup on my computer for years from only occasionally having react native gigs and can't figure out how to fix it.

After some toiling I think I have a working installation but can't run a cargo mobile generated project. It works just fine on desktop and iOS simulator (without legacy build system even!) and am getting the following error:

The file is indeed not in the folder it is looking in but I do see some -as files.

Could it be that I have upgraded beyond supported android studio? 😭 I don't know much about the build system or what I'm looking for but I did see this page saying certain things related to standalone toolchains are obsolete when googling things related to the error above.

Any help would be appreciated. I'm excited to finally be digging into a project with this tool!

Zeerooth

Zeerooth

type: bug
0

I recently tried updating cargo-mobile, along with Android NDK and running the winit example, but it seems like it's broken on my device, because no events are being logged at all and whenever I touch the screen I get this in adb logcat:

Here are some startup logs:

Honestly, I am lost as to why this might be happening, wgpu template seems to be drawn fine for example. I'll try to investigate it futher soon, but any ideas would be really appreciated :)

NDK version: 23.1.7779620 Rust: 1.51.0

indiein

indiein

type: bug
1

Created app from bevy-demo template and app crashes on startup. Here is the stack trace from logcat.

benmkw

benmkw

type: enhancement
0

I was very happy to find this tool as I thought about running wgpu on iOS and basically had no idea what would be required. The wgpu and winit examples worked great and the code is minimal such that I feel like I roughly understand whats happing.

At the same time I'm learning about SwiftUI. This is a stark contrast because its very high level and I'm a beginner there so I'm not really sure how I would go about getting a wgpu "view" (for the lack of a better word, meaning just part of a screen) to run in an SwiftUI based app. My conceptual understanding of the setup would be that I would need to create some kind of Context in SwiftUI(or UIKit, wrapped in SwiftUI) and then pass some kind of handle, which refers to that context, to wgpu but currently I don't see such a thing happening in the code and wgpu just takes over the whole screen.

I assume the first question would be how to do it in a UIKit App and then how to wrap that and maybe if there are further issues which may make this more difficult. One thing I noticed is that the current setup seems to use a legacy build system of some sort of xcode and default SwiftUI projects don't so this might be one such issue.

If someone has time to respond or even extend the examples that would be highly appreciated 👍

szsoftware

szsoftware

type: question
2

Is it possible to compile an external rust source with that toolchain? For instance tectonic typesetting and a resulting commandline programm instead of the jni lib mechanism. If yes, do you have an example?

Many thanks in advance.

Joe23232

Joe23232

type: question
4

Hi I have some questions regarding this crate:

  1. Am I able to make full blown Android applications by using this crate and the fltk-rs GUI library crate?
  2. Is it stable or beta/alpha?
  3. Is there a docs page that is similar to the Rust Programming Book?
  4. Is it more difficult to use or easier to use compared to using Android Studio Java?
  5. Am I required to use Android Studio for this? If so then am I required to do some Java programming as well?
  6. If I wanted to do something that is specific to Android, for example list out all the apps installed or to create a lockscreen for the user (this wouldn't work on iOS nor Linux), then would this crate still work? Am I able to call Android APIs in a high level manner just like with Android Studio Java?
wbrickner

wbrickner

type: question
2

I'd rather put a few methods in Java rather than put up with the verbosity and overhead of sending objects back and forth between the JVM, especially with the Android system APIs it's really suboptimal.

I only care about android right now, but I will eventually need to do this for both platforms.

In the generated android studio project I can't seem to see any java or Kotlin files with an obvious entry point, so there's some magic going on that I don't understand.

Can you give me some information on how I would for instance add custom java code that I can call using the JNI? I'd like to have most of the "business logic" / meat live in the Rust side for performance and correctness and place all the platform API stuff in Java.

Thank you!

Thought: I wonder if someday a DSL could be implemented via procedural macros to allow you to interact with the JNI more cleanly, almost a 1:1 with the equivalent java code? It could be really neat and tidy too, with platform-guarded modules for each DSL that lets you use the system APIs (Java and Swift/ObjC/Apple JNI equivalent).

wbrickner

wbrickner

status: needs discussion
5

Hello,

I am targeting the Oculus Quest 2, and have prepared my own bindings via bindgen for their Mobile SDK. I have gotten the library to link - the build succeeds.

At runtime (on my android phone, and also on the physical Quest 2), the app launches and immediately crashes, complaining that the primary application my_app.so generated by cargo-mobile depends on my libvrapi.so:

This only partially makes sense, shouldn't the libvrapi.so be on the system? Isn't that the entire point of having a shared library? To have it be shared among all the binaries which make use of it? I never gave any special instructions to include the libvrapi.so literally in the APK bundle, should I have? How would I do that?

Thank you, this project will allow so much technological progress!

nikhilyeole1

nikhilyeole1

status: upstream
6

Hello Francesca, Team, Really appreciate your work. cargo-mobile is really good!

I tried to run cargo mobile on my apple MacBook Air m1 that has arm chip. I have rust 1.49 beta installed (which is natively supported for m1 via rustup, I haven't tested 1.49 beta intel version through rosetta though).

Below is the error log -

Thanks, Nikhil

Information - Updated May 20, 2022

Stars: 666
Forks: 35
Issues: 21

SUPER Android Analyzer

Secure, Unified, Powerful and Extensible Rust Android Analyzer

SUPER Android Analyzer

Rust core for native Android and iOS apps

The Android build contains a wrapper library, which is imported in the Android app with Carthage

Rust core for native Android and iOS apps

cargo-ndk - Build Rust code for Android

This cargo extension handles all the environment configuration needed for successfully building libraries

cargo-ndk - Build Rust code for Android

Very simple command line tool to setup configuration for android native binaries

If you want to use native binary in your android project, and you don't like to write it in C/C++, writting in rust can be...

Very simple command line tool to setup configuration for android native binaries

Rust bindings for Oboe library

Oboe High-Performance Audio library for Android

Rust bindings for Oboe library

EndToEnd Anonymus chatting app

With Rust Backend and Android support

EndToEnd Anonymus chatting app

swapi-rust-mobile

Crossplatform mobile solution based on Rust for IOS & Android

swapi-rust-mobile
Facebook Instagram Twitter GitHub Dribbble
Privacy