-
Create a blank React Native project:
npx react-native@latest init example
-
Create a blank Cargo project inside:
cd example && cargo init --lib example-jsi-module
-
Add
jsi
,jni
, andcxx
as dependencies ofexample-jsi-module
jsi
will allow us to create a React Native modulejni
is needed for interop with Java code on Androidcxx
is needed to initializejsi
because JSI is implemented in C++ with smart pointers
-
Write the code in
example-jsi-module/src
-
Update
android/build.gradle
to add the following plugin underbuildscript.dependencies
:buildscript { repositories { // don't remove the existing repositories, just add maven b/c the Rust plugin is hosted there maven { url "https://plugins.gradle.org/m2/" } } dependencies { // don't remove the existing dependencies, just add this one classpath("io.github.MatrixDev.android-rust:plugin:0.3.2") } }
This plugin will compile
example-jsi-module
using the Android NDK as part of building our application for Android -
Make sure the Android NDK is installed. Version 23 should work
-
Update
android/app/build.gradle
to add the following lines:apply plugin: "io.github.MatrixDev.android-rust" androidRust { module("example-jsi-module") { it.path = file("../example-jsi-module") // default abi targets are arm and arm64; if you want to run on Android // Emulator then you may need to add x86_64 // targets = ["arm", "arm64", "x86", "x86_64"] } }
Note: when you compile the program, this Gradle plugin will install all of the Rust Android targets if they are not already installed
-
Write the code in
android/app/src/main/java/com/example/ExampleJsiModule.java
. This will be called when the application starts, and it gives us a pointer to the React Native runtime so we can initialize our Rust code -
Write the code in
android/app/src/main/java/com/example/ExampleJsiPackage.java
. This lets React Native discover our module -
In
android/app/src/main/java/com/example/MainApplication.java
, add the following line togetPackages()
:packages.add(new ExampleJsiPackage());
-
Add a couple of lines to
App.tsx
to call our native module (I added them near the top):// call our Rust module const {ExampleJsiModule} = NativeModules; ExampleJsiModule.install();
-
Connect your Android device or start an emulator
-
Run
npm run start
and press A to deploy to Android -
You should see
hello from Rust
in the terminal after the app loads
example
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||