From 9c33d1fbb0c0ab2c891bc82fef0d72f178640ea6 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 12 Oct 2022 16:03:29 +0200 Subject: [PATCH] Add dependency on native Rust project --- README.md | 10 ++++++++++ app/build.gradle | 16 ++++++++++++++++ build.gradle | 1 + native/.gitignore | 14 ++++++++++++++ native/Cargo.toml | 14 ++++++++++++++ native/src/lib.rs | 20 ++++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 README.md create mode 100644 native/.gitignore create mode 100644 native/Cargo.toml create mode 100644 native/src/lib.rs diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e8d79b --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Building + +Make sure the required Android NDK platforms are installed, and the environment +variable `ANDROID_NDK_TOOLCHAIN_DIR` is configured correctly. + +On Windows, this might be a path such as the following (NDK "Side by side" SDK tool): + +``` +%LOCALAPPDATA%\Android\Sdk\ndk\25.1.8937393\toolchains\llvm\prebuilt\windows-x86_64\bin +``` diff --git a/app/build.gradle b/app/build.gradle index 66468da..34f48af 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' + id "org.mozilla.rust-android-gradle.rust-android" } android { @@ -44,6 +45,7 @@ android { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } + ndkVersion '25.1.8937393' } dependencies { @@ -62,3 +64,17 @@ dependencies { debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version" } + +// See https://github.com/mozilla/rust-android-gradle for other targets and required toolchains +cargo { + module = "../native" + libname = "talaria" + targets = ["arm64", "arm", "x86"] + profile = 'release' +} + +tasks.whenTaskAdded { task -> + if ((task.name == 'javaPreCompileDebug' || task.name == 'javaPreCompileRelease')) { + task.dependsOn 'cargoBuild' + } +} diff --git a/build.gradle b/build.gradle index 639934d..5f07436 100644 --- a/build.gradle +++ b/build.gradle @@ -7,4 +7,5 @@ plugins { id 'com.android.application' version '7.3.0' apply false id 'com.android.library' version '7.3.0' apply false id 'org.jetbrains.kotlin.android' version '1.6.10' apply false + id "org.mozilla.rust-android-gradle.rust-android" version "0.9.3" } diff --git a/native/.gitignore b/native/.gitignore new file mode 100644 index 0000000..6985cf1 --- /dev/null +++ b/native/.gitignore @@ -0,0 +1,14 @@ +# 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 diff --git a/native/Cargo.toml b/native/Cargo.toml new file mode 100644 index 0000000..caad8ab --- /dev/null +++ b/native/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "talaria" +version = "0.1.0" +edition = "2021" + +[lib] +name = "talaria" +crate-type = ["cdylib"] + +[dependencies] +jni = { version = "0.10.2", default-features = false } + +[profile.release] +lto = true diff --git a/native/src/lib.rs b/native/src/lib.rs new file mode 100644 index 0000000..f2636e7 --- /dev/null +++ b/native/src/lib.rs @@ -0,0 +1,20 @@ +#![cfg(target_os = "android")] +#![allow(non_snake_case)] + +use std::ffi::{CStr, CString}; + +use jni::JNIEnv; +use jni::objects::{JObject, JString}; +use jni::sys::jstring; + +#[no_mangle] +pub unsafe extern fn Java_dev_lonami_talaria_MainActivity_hello(env: JNIEnv, _: JObject, j_recipient: JString) -> jstring { + let recipient = CString::from( + CStr::from_ptr( + env.get_string(j_recipient).unwrap().as_ptr() + ) + ); + + let output = env.new_string("Hello ".to_owned() + recipient.to_str().unwrap()).unwrap(); + output.into_inner() +}