Going Mobile: Android and iOS
Going Mobile: Android and iOS
Section titled “Going Mobile: Android and iOS”One of the most revolutionary features of Tauri v2 is the ability to take the exact same web frontend and Rust backend you wrote for your desktop application and run it on mobile devices. This transforms Tauri from a desktop framework into a true cross-platform solution.
Mobile Prerequisites
Section titled “Mobile Prerequisites”Before diving in, ensure you completed the mobile setup steps in Chapter 2. Development for mobile requires heavy platform-specific tools:
- Android: You need Android Studio and the Android SDK environment variables (JAVA_HOME, ANDROID_HOME) configured.
- iOS: You need a macOS device with Xcode installed. iOS development is not possible on Windows or Linux.
Initializing Mobile Projects
Section titled “Initializing Mobile Projects”Tauri does not clutter your project with mobile files by default. You need to explicitly initialize them. This process creates the native project structures (a Gradle project for Android and an Xcode workspace for iOS) inside the src-tauri/gen folder.
Run the following commands in your terminal to scaffold the mobile targets:
tauri android inittauri ios initDuring initialization, the CLI might prompt you to install specific Rust targets (like aarch64-linux-android or aarch64-apple-ios). Allow it to do so.
Running on Simulators and Devices
Section titled “Running on Simulators and Devices”Once initialized, running your app on a mobile emulator is surprisingly similar to running it on a desktop. The Tauri CLI handles compiling your Rust code into a library, bundling your frontend, and launching the native app.
# For Android Emulator or connected devicetauri android dev
# For iOS Simulator or connected devicetauri ios devWhen you run these commands, Tauri will attempt to find a connected device or a running emulator. If none are found, it may prompt you to select one.
Mobile-Specific Considerations
Section titled “Mobile-Specific Considerations”While the code is shared, the user experience (UX) is different. Here are key adjustments to keep in mind:
- Touch vs. Mouse: There is no ‘hover’ state on mobile. Ensure your UI relies on tap interactions.
- Safe Areas: Mobile screens have notches and rounded corners. Use CSS environment variables like
env(safe-area-inset-top)to prevent your content from being hidden behind the status bar. - Backend Limitations: While most standard Rust code works perfectly, crates that rely on specific desktop OS APIs (like interacting with the Windows Registry or Linux X11) will not work on mobile. You may need to use
#[cfg(target_os = "android")]to conditionally compile code.
Opening the Native IDEs
Section titled “Opening the Native IDEs”Sometimes you need to configure native settings, such as the App Icon, Launch Screen, or specific permissions (like Camera access in Info.plist or AndroidManifest.xml). Tauri provides a shortcut to open the generated projects in their respective native IDEs:
tauri android opentauri ios openThis opens Android Studio or Xcode, allowing you to tweak the native layer just as if you were writing a native app.