Skip to content

Commit

Permalink
Build Rust library
Browse files Browse the repository at this point in the history
  • Loading branch information
Frixuu committed Oct 3, 2023
1 parent 8247f56 commit fb0888e
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 11 deletions.
19 changes: 16 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import com.nishtahir.CargoBuildTask

plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
id("dagger.hilt.android.plugin")
id("org.mozilla.rust-android-gradle.rust-android")
}

android {
Expand Down Expand Up @@ -70,13 +73,19 @@ android {
}
}

cargo {
module = "src/main/rust"
libname = "crossword"
targets = listOf("arm", "arm64", "x86_64")
}

dependencies {

implementation("org.jetbrains.kotlin:kotlin-stdlib:${libs.versions.kotlin.get()}")
implementation(libs.kotlin.stdlib)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

implementation("com.google.dagger:hilt-android:${libs.versions.hilt.get()}")
kapt("com.google.dagger:hilt-compiler:${libs.versions.hilt.get()}")
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
Expand All @@ -102,3 +111,7 @@ dependencies {
kapt {
correctErrorTypes = true
}

tasks.preBuild.configure {
dependsOn.add(tasks.withType(CargoBuildTask::class.java))
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CrosswordHelperApplication : Application() {
companion object {
init {
System.loadLibrary("native-lib")
System.loadLibrary("crossword")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.lukasz.xword.search

import android.content.res.AssetManager
import timber.log.Timber
import xyz.lukasz.xword.interop.NativeSharedPointer
import java.util.*

Expand All @@ -15,6 +16,7 @@ class MissingLettersIndex(locale: Locale) : WordIndex(locale) {
*/
override fun loadFromAsset(assetManager: AssetManager) {
unload()
Timber.w(helloWorld())
val assetPath = resolveAssetPath()
val threadCount = Runtime.getRuntime().availableProcessors()
nativeIndex = loadNative(assetManager, assetPath, threadCount)
Expand All @@ -30,4 +32,6 @@ class MissingLettersIndex(locale: Locale) : WordIndex(locale) {
*/
private external fun loadNative(assetManager: AssetManager, filename: String, threads: Int)
: NativeSharedPointer

private external fun helloWorld(): String
}
1 change: 1 addition & 0 deletions app/src/main/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
243 changes: 243 additions & 0 deletions app/src/main/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/src/main/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "crossword"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[target.'cfg(target_os="android")'.dependencies]
jni = { version = "0.21.1" }
20 changes: 20 additions & 0 deletions app/src/main/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//#[cfg(target_os="android")]
#[allow(non_snake_case)]
pub mod android {

use jni::JNIEnv;
use jni::objects::{JClass, JString};
use jni::sys::{jstring};

use std::ffi::{CString};

#[no_mangle]
pub unsafe extern fn Java_xyz_lukasz_xword_search_MissingLettersIndex_helloWorld(env: JNIEnv, _: JClass) -> jstring {
// Our Java companion code might pass-in "world" as a string, hence the name.
//let world = rust_greeting(env.get_string(java_pattern).expect("invalid pattern string").as_ptr());
// Retake pointer so that we can use it below and allow memory to be freed when it goes out of scope.
let world_ptr = CString::new("XDDDDDD").expect("cannot create cstring");
let output = env.new_string(world_ptr.to_str().unwrap()).expect("Couldn't create java string!");
output.into_raw()
}
}
13 changes: 6 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
buildscript {

repositories {
google()
mavenCentral()
}

dependencies {
classpath("com.android.tools.build:gradle:8.1.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${libs.versions.kotlin.get()}")
classpath("com.google.dagger:hilt-android-gradle-plugin:${libs.versions.hilt.get()}")
}
}

Expand All @@ -19,6 +12,12 @@ allprojects {
}
}

plugins {
id("org.mozilla.rust-android-gradle.rust-android").version("0.9.3")
id("com.google.dagger.hilt.android").version(libs.versions.hilt.get()).apply(false)
}


tasks.register<Delete>("clean") {
delete(rootProject.buildDir)
}
Loading

0 comments on commit fb0888e

Please sign in to comment.