From 8d1472b5a7af907424cc610f6a5ff6e17e4627dc Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Mon, 1 Jan 2024 17:45:08 +0100 Subject: [PATCH] feat: add basic rust JNI demo --- .gitignore | 1 + .../main/java/net/mbess/popequer/Native.kt | 8 ++++++++ .../net/mbess/popequer/ui/MainActivity.kt | 4 ++++ rust-sandbox/.gitignore | 20 +++++++++++++++++++ rust-sandbox/Cargo.toml | 13 ++++++++++++ rust-sandbox/src/lib.rs | 15 ++++++++++++++ 6 files changed, 61 insertions(+) create mode 100644 app/src/main/java/net/mbess/popequer/Native.kt create mode 100644 rust-sandbox/.gitignore create mode 100644 rust-sandbox/Cargo.toml create mode 100644 rust-sandbox/src/lib.rs diff --git a/.gitignore b/.gitignore index 050d011..c6def42 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ .externalNativeBuild .cxx local.properties +jniLibs diff --git a/app/src/main/java/net/mbess/popequer/Native.kt b/app/src/main/java/net/mbess/popequer/Native.kt new file mode 100644 index 0000000..1d08b97 --- /dev/null +++ b/app/src/main/java/net/mbess/popequer/Native.kt @@ -0,0 +1,8 @@ +package net.mbess.popequer + +object Native { + init { + System.loadLibrary("rustsandbox") + } + external fun add(a: Int, b: Int): Int +} \ No newline at end of file diff --git a/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt b/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt index 2402729..73952c7 100644 --- a/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt +++ b/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt @@ -8,10 +8,14 @@ import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.material3.* import androidx.compose.ui.platform.LocalContext import net.mbess.popequer.AppContext +import net.mbess.popequer.Native class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { + Native.add(143, 54).let { + println("From foreign rust result: $it") + } super.onCreate(savedInstanceState) val appContext = AppContext(this) diff --git a/rust-sandbox/.gitignore b/rust-sandbox/.gitignore new file mode 100644 index 0000000..389159a --- /dev/null +++ b/rust-sandbox/.gitignore @@ -0,0 +1,20 @@ +# Created by https://www.toptal.com/developers/gitignore/api/rust +# Edit at https://www.toptal.com/developers/gitignore?templates=rust + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# End of https://www.toptal.com/developers/gitignore/api/rust diff --git a/rust-sandbox/Cargo.toml b/rust-sandbox/Cargo.toml new file mode 100644 index 0000000..bc979df --- /dev/null +++ b/rust-sandbox/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rust-sandbox" +version = "0.1.0" +edition = "2021" + +[lib] +name = "rustsandbox" +crate-type = ["cdylib"] + +[dependencies] +jni = "0.21.1" + +[target.'cfg(target_os = "android")'.dependencies] diff --git a/rust-sandbox/src/lib.rs b/rust-sandbox/src/lib.rs new file mode 100644 index 0000000..17fef43 --- /dev/null +++ b/rust-sandbox/src/lib.rs @@ -0,0 +1,15 @@ +use jni::{ + objects::{JClass, JObject}, + JNIEnv, +}; + +#[no_mangle] +pub extern "system" fn Java_net_mbess_popequer_Native_add( + _env: JNIEnv, + _class: JClass, + a: i32, + b: i32 +) -> i32 { + a + b +} +