You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/develop/Plugins/develop-mobile.mdx
+123Lines changed: 123 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -355,6 +355,129 @@ Use a nullable type and set the default value on the command function instead.
355
355
Required arguments are defined as `let <argumentName>: Type`
356
356
:::
357
357
358
+
## Calling Rust From Mobile Plugins
359
+
360
+
It is often preferable to write plugin code in Rust, for performance and reusability. While Tauri doesn't directly provide a mechanism to call Rust from your plugin code, using JNI on Android and FFI on iOS allows plugins to call shared code, even when the application WebView is suspended.
361
+
362
+
### Android
363
+
364
+
In your plugin's `Cargo.toml`, add the jni crate as a dependency:
365
+
366
+
```toml
367
+
[target.'cfg(target_os="android")'.dependencies]
368
+
jni = "0.21"
369
+
```
370
+
371
+
Load the application library statically and define native functions in your Kotlin code. In this example, the Kotlin class is `com.example.HelloWorld`, we need to reference the full package name from the Rust side.
372
+
373
+
```kotlin
374
+
privateconstvalTAG="MyPlugin"
375
+
376
+
init {
377
+
try {
378
+
// Load the native library (libapp_lib.so)
379
+
// This is the shared library built by Cargo with crate-type = ["cdylib"]
380
+
System.loadLibrary("app_lib")
381
+
Log.d(TAG, "Successfully loaded libapp_lib.so")
382
+
} catch (e:UnsatisfiedLinkError) {
383
+
Log.e(TAG, "Failed to load libapp_lib.so", e)
384
+
throw e
385
+
}
386
+
}
387
+
388
+
externalfunhelloWorld(name:String): String?
389
+
```
390
+
391
+
Then in your plugin's Rust code, define the function JNI will look for. The function format is `Java_package_class_method`, so for our class above this becomes `Java_com_example_HelloWorld_helloWorld` to get called by our `helloWorld` method:
iOS only uses standard C FFI, so doesn't need any new dependencies. Add the hook in your Swift code, as well as any necessary cleanup. These functions can be named anything valid, but must be annotated with `@_silgen_name(FFI_FUNC)`, where FFI_FUNC is a function name to be called from Rust:
Google is moving to make 16KB memory pages a requirement in all new Android app submissions. Building with an NDK version 28 or higher should automatically generate bundles that meet this requirement, but in the event an older NDK version must be used or generated files aren't 16KB aligned, the following can be added to `.cargo/config.toml` to flag this to `rustc`:
0 commit comments