Skip to content

Instantly share code, notes, and snippets.

@matthunz
Last active October 11, 2024 16:31
Show Gist options
  • Select an option

  • Save matthunz/d1e469dee32027d78ed52bed97d23181 to your computer and use it in GitHub Desktop.

Select an option

Save matthunz/d1e469dee32027d78ed52bed97d23181 to your computer and use it in GitHub Desktop.

Prerequisites

First install cargo-mobile2

cargo install cargo-mobile2

Then install and add the Android SDK and NDK for your platform to your path.

Setup

Start by creating a new crate and adding the required dependencies:

cargo new my_app
cd my_app

cargo add dioxus
cargo add dioxus-blitz --git https://github.com/DioxusLabs/blitz

Then initialize cargo-mobile2

cargo mobile init
Current issues

cargo-mobile2 currently has an issue compiling with newer versions of Gradle (tauri-apps/cargo-mobile2#392) but this should be fixed soon in tauri-apps/cargo-mobile2#404.

For now you can remove lines 42-44 in gen/android/app/build.gradle.kts:

kotlinOptions {
    jvmTarget = "1.8"
}

Now you're ready to start writing your Dioxus application:

use dioxus::prelude::*;

fn app() -> Element {
    rsx!{ h1 { "Hello, World!" } }
}

#[no_mangle]
fn android_main(android_app: android_activity::AndroidApp) {
    dioxus_blitz::set_android_app(android_app);

    dioxus_blitz::launch(app);
}

Running your app

Finally connect your device in debugging mode, or startup a simulator in Android Studio, and run:

cargo android run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment