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 +} +