Add dependency on native Rust project

pull/1/head
Lonami Exo 2022-10-12 16:03:29 +02:00
parent fee6c52606
commit 9c33d1fbb0
6 changed files with 75 additions and 0 deletions

10
README.md Normal file
View File

@ -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
```

View File

@ -1,6 +1,7 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.android'
id "org.mozilla.rust-android-gradle.rust-android"
} }
android { android {
@ -44,6 +45,7 @@ android {
excludes += '/META-INF/{AL2.0,LGPL2.1}' excludes += '/META-INF/{AL2.0,LGPL2.1}'
} }
} }
ndkVersion '25.1.8937393'
} }
dependencies { dependencies {
@ -62,3 +64,17 @@ dependencies {
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$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'
}
}

View File

@ -7,4 +7,5 @@ plugins {
id 'com.android.application' version '7.3.0' apply false id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' 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.jetbrains.kotlin.android' version '1.6.10' apply false
id "org.mozilla.rust-android-gradle.rust-android" version "0.9.3"
} }

14
native/.gitignore vendored Normal file
View File

@ -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

14
native/Cargo.toml Normal file
View File

@ -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

20
native/src/lib.rs Normal file
View File

@ -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()
}