Mobile Plugin Development
Plugins can run native mobile code written in Kotlin (or Java) and Swift. The default plugin template includes an Android library project using Kotlin and a Swift package including an example mobile command showing how to trigger its execution from Rust code.
Follow the steps in the Plugin Development guide to initialize a new plugin project.
If you have an existing plugin and would like to add Android or iOS capabilities to it, you can use
plugin android init and
plugin ios init to bootstrap the mobile library projects and guide you through the changes needed.
The default plugin template splits the plugin’s implementation into two separate modules:
The desktop implementation uses Rust code to implement a functionality, while the mobile implementation sends a message to the native mobile code to execute a function and get a result back. If shared logic is needed across both implementations, it can be defined in
This implementation simplifies the process of sharing an API that can be used both by commands and Rust code.
A Tauri plugin for Android is defined as a Kotlin class that extends
app.tauri.plugin.Plugin and is annoted with
app.tauri.annotation.TauriPlugin. Each method annotated with
Tauri uses Kotlin by default for the Android plugin implementation, but you can switch to Java if you prefer. After generating a plugin, right click the Kotlin plugin class in Android Studio and select the “Convert Kotlin file to Java file” option from the menu. Android Studio will guide you through the project migration to Java.
A Tauri plugin for iOS is defined as a Swift class that extends the
Plugin class from the
Tauri package. Each function with the
@objc attribute and the
(_ invoke: Invoke) parameter (for example
The plugin is defined as a Swift package so that you can use its package manager to manage dependencies.
Refer to the Plugin Configuration section of the Plugin Development guide for more details on developing plugin configurations.
The plugin instance on mobile has a getter for the plugin configuration:
Plugins can hook into several lifecycle events:
- load: When the plugin is loaded into the web view
- onNewIntent: Android only, when the activity is re-launched
There are also the additional lifecycle events for plugins in the Plugin Development guide.
- When: When the plugin is loaded into the web view
- Why: Execute plugin initialization code
Note: This is only available on Android.
- When: When the activity is re-launched. See Activity#onNewIntent for more information.
- Why: Handle application re-launch such as when a notification is clicked or a deep link is accessed.
There is a plugin class inside the respective mobile projects where commands can be defined that can be called by the Rust code:
tauri::plugin::PluginHandle to call a mobile command from Rust:
If a plugin requires permissions from the end user, Tauri simplifies the process of checking and requesting permissions.
First define the list of permissions needed and an alias to identify each group in code. This is done inside the
First override the
Tauri automatically implements two commands for the plugin:
Plugins can emit events at any point of time using the
The helper functionss can then be called from the NPM package by using the
addPluginListener helper function:
© 2024 Tauri Contributors. CC-BY / MIT