Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 39 ยท Mobile and Packaging

Tauri Mobile Development for Android and iOS

Build Android and iOS apps with Tauri 2 from one codebase: prerequisites, init, dev and build commands, the lib.rs entry point, mobile plugins, safe areas and responsive UI, with runnable stand-ins.

Tauri 2 can build apps for Android and iOS from the same web frontend and the same Rust core that power your desktop app. You keep your UI code, your commands and most plugins, and add the mobile project files that Tauri generates for you. This lesson explains the setup, the commands you will use daily, how to organize code for several platforms, and how to make a UI that feels right on a phone.

If you have not built a desktop app yet, finish the earlier chapters of this track first. You will also use ideas from The Tauri Plugin System and Capabilities and Permissions.

Real versus stand-in. Building for phones needs Android Studio or Xcode, so none of it runs on this site. The bash, toml, rust and json blocks are real. The runnable blocks are JavaScript models of platform branching and layout decisions.

Prerequisites

Android works on Windows, macOS and Linux. You need:

  • Android Studio, with the SDK Platform, Platform Tools, Build Tools, NDK (Side by side) and Command-line Tools installed through the SDK Manager.
  • Environment variables JAVA_HOME (the JDK bundled with Android Studio is fine), ANDROID_HOME and NDK_HOME.
  • The Rust compile targets:
Terminal
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android

iOS needs a Mac with Xcode and an Apple developer account for running on devices and shipping:

Terminal
rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
brew install cocoapods

On Windows and Linux you can target Android only; iOS builds require macOS.

Making the project mobile-ready

Tauri apps created with create-tauri-app already have the right shape. The entry point lives in src-tauri/src/lib.rs and is shared by all platforms:

Rust
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

src/main.rs just calls run() for desktop. On mobile, the operating system starts the app differently, so mobile_entry_point generates the glue. The Cargo.toml must build the library as several crate types:

TOML
[lib]
name = "notes_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

Then generate the native projects once:

Terminal
npm run tauri android init
npm run tauri ios init

This creates src-tauri/gen/android (a Gradle project) and src-tauri/gen/apple (an Xcode project). Commit them, since you may edit them, for example to add a permission to AndroidManifest.xml or an entry to Info.plist.

Daily commands

Terminal
npm run tauri android dev          # run on an emulator or connected device
npm run tauri ios dev              # run in the iOS simulator
npm run tauri android build        # release bundles
npm run tauri android build -- --apk
npm run tauri android build -- --aab   # for Google Play
npm run tauri ios build            # makes an .ipa

android dev hot reloads your frontend like the desktop version. When the app runs on a physical device, the phone must reach your dev server over the network. Tauri provides the machine's address in TAURI_DEV_HOST, which your bundler config should use:

TypeScript
// vite.config.ts
import { defineConfig } from 'vite';
const host = process.env.TAURI_DEV_HOST;

export default defineConfig({
  server: {
    host: host || false,
    port: 1420,
    strictPort: true,
    hmr: host ? { protocol: 'ws', host, port: 1421 } : undefined,
  },
});

Use adb logcat or Android Studio's Logcat to read native logs, and Safari's Web Inspector for the iOS webview. Chrome's chrome://inspect attaches to the Android webview.

One codebase, several platforms

You will often need code that exists only on some platforms. Rust provides compile-time switches:

Rust
pub fn run() {
    let mut builder = tauri::Builder::default();

    #[cfg(desktop)]
    {
        builder = builder
            .plugin(tauri_plugin_global_shortcut::Builder::new().build())
            .plugin(tauri_plugin_window_state::Builder::default().build());
    }

    #[cfg(mobile)]
    {
        builder = builder.plugin(tauri_plugin_haptics::init());
    }

    builder.run(tauri::generate_context!()).expect("error");
}

Put desktop-only dependencies in a target section of Cargo.toml so they are not even compiled for phones:

TOML
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-global-shortcut = "2"
tauri-plugin-window-state = "2"

Capability files can also be limited with "platforms": ["android", "iOS"] (capabilities).

In the frontend, ask the os plugin which platform you are on, and adapt:

TypeScript
import { type } from '@tauri-apps/plugin-os';

const isMobile = ['android', 'ios'].includes(type());
Node.js Stand-in: pick features per platform

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Mobile plugins and permissions

Many official plugins also support mobile, and some exist only for mobile: barcode-scanner, biometric, haptics, geolocation, nfc. Mobile operating systems have their own permission prompts on top of Tauri's capabilities. For example, using the camera needs both the Tauri permission (barcode-scanner:allow-scan) and the Android manifest or iOS Info.plist entry that explains why the app wants the camera. A plugin's docs list the exact native entries.

When you need an API no plugin offers, write native code. A plugin has a Kotlin class for Android and a Swift class for iOS, called from Rust (see Writing a Custom Plugin). The frontend calls the same invoke either way.

Designing for a phone

A good mobile UI is not a squeezed desktop UI. A few rules:

  • Add <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">.
  • Respect the safe areas (notches, home indicators) with CSS: padding-bottom: env(safe-area-inset-bottom).
  • Make touch targets at least 44 by 44 CSS pixels.
  • Use bottom navigation for the main sections and avoid hover-only features.
  • Handle the soft keyboard, which shrinks the viewport.
  • Keep work off the main thread and be careful with battery and background limits; phones suspend apps freely.
  • Remember there is no menu bar, no multiple windows and often no file system browsing, so use pickers and in-app storage.
Node.js Stand-in: choose a layout from the viewport width

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command generates the native Android project for a Tauri app?

    tauri android init creates src-tauri/gen/android, the Gradle project the Android tools build.
  2. 2. What does #[cfg_attr(mobile, tauri::mobile_entry_point)] do?

    On mobile the app has no main(), so the macro generates the entry point that calls your run function.
  3. 3. Which machine can build a Tauri iOS app?

    iOS builds require Xcode, which only runs on macOS.
  4. 4. How do you keep a desktop-only plugin out of the phone build?

    Compile-time gating keeps platform-specific code and dependencies out of other targets.

Summary

  • Tauri 2 builds Android and iOS from the same frontend and Rust core; iOS needs macOS.
  • Install the SDKs and Rust targets, then run tauri android init / tauri ios init, and use dev and build commands.
  • Share code through lib.rs with mobile_entry_point, and gate platform-specific code with cfg(desktop) and cfg(mobile).
  • Mobile-only plugins and native permission strings extend what the app can do.
  • Design for touch, safe areas, bottom navigation and app suspension.

Next: Bundling and Installers