// 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 jni::objects::{JClass, JString};
// 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 jni::sys::jstring;
// This keeps Rust from "mangling" the name and making it unique for this // crate. #[no_mangle] pubextern"system"fnJava_com_android_integratingrust_RustGreetings_greeting( env: JNIEnv, // 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, input: JString, ) -> jstring { // First, we have to get the string out of Java. Check out the `strings` // module for more info on how this works. letmut input: String = env .get_string(input) .expect("Couldn't get java string!") .into();
input = append_string(&input);
// Then we have to create a new Java string to return. Again, more info // in the `strings` module. let output = env .new_string(format!("Hello, {}!", input)) .expect("Couldn't create java string!");
// Finally, extract the raw pointer to return. output.into_raw() }