I have an Android app (written in Kotlin), with the processing code running in Rust. I'm trying to send List<String> data to the Rust back-end and interpret it as a Vec<String>. The size of the list is variable. I'm using JNI (Java Native Interface) for this.
Android (Kotlin)
On the Kotlin side I have the following function signature:
package eu.mypackage.rust
class SomeName {
@Throws(IllegalArgumentException::class)
external fun checkAnswer(answerInput: String, answerList: List<String>): Boolean
}
Which I call with:
val isCorrect = sn.checkAnswer(answerInput, answerList)
Rust
On the Rust side I have this draft function:
#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {
extern crate jni;
// This is the interface to the JVM that we'll call the majority of our methods on.
// @See https://docs.rs/jni/latest/jni/
use self::jni::JNIEnv;
// These objects are what you should use as arguments to your native function.
// They carry extra lifetime information to prevent them escaping this context
// and getting used after being GC'd.
use self::jni::objects::{JClass, JString, JObject, JObjectArray}; // Not sure what is required
// This is just a pointer. We'll be returning it from our function.
// We can't return one of the objects with lifetime information
// because the lifetime checker won't let us.
use self::jni::sys::{jstring, jboolean, jobjectArray, jsize}; // Not sure what is required
fn kotlin_list_to_rust_vec_string(env: &JNIEnv, java_array: &jobjectArray) -> Vec<String> {
// TODO function to convert
}
#[no_mangle] // This keeps Rust from "mangling" the name so it is unique (crate).
pub extern "system" fn Java_eu_mycompany_rust_MyPackage_checkAnswer<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static native method.
_class: JClass<'local>,
answerInput: JString<'local>,
answerList: JObjectArray<'local>, // JObjectArray correct?
) -> jboolean
// Keep in mind that arrays of jboolean values should only ever hold values of 0 or 1 because any other value could lead to undefined behaviour within the JVM.
// https://docs.rs/jni/latest/jni/struct.JNIEnv.html#jboolean-elements
{
let answer_input: String = env.get_string(&answerInput).expect("Couldn't get java string!").into();
let answer_list = kotlin_list_to_rust_vec_string(&env, &answerList);
// ... code to process answer_list
return 1 // true or 0 for false
}
}
Question
- It seems there is no direct conversion for Java
List<String>-> RustVec<String>, so a function likekotlin_list_to_rust_vec_stringis required? - What code should be in
kotlin_list_to_rust_vec_string? I assumeenvandanswerListshould be a reference to avoid screwing up the data on the Kotlin side? (The amount of data is small, so copying the data is fine)