<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>7 互操作 on 编程那些事儿</title><link>https://before80.github.io/prgms/Rust/comprehensiveRust/android/interoperability/</link><description>Recent content in 7 互操作 on 编程那些事儿</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Tue, 11 Aug 2026 23:57:44 +0800</lastBuildDate><atom:link href="https://before80.github.io/prgms/Rust/comprehensiveRust/android/interoperability/index.xml" rel="self" type="application/rss+xml"/><item><title>7.3 与 Java 互操作</title><link>https://before80.github.io/prgms/Rust/comprehensiveRust/android/interoperability/01-java/</link><pubDate>Tue, 11 Aug 2026 11:30:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/comprehensiveRust/android/interoperability/01-java/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://google.github.io/comprehensive-rust/"&gt;Comprehensive Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://google.github.io/comprehensive-rust/android/interoperability/java.html"&gt;https://google.github.io/comprehensive-rust/android/interoperability/java.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="73-与-java-互操作"&gt;7.3 与 Java 互操作&lt;/h1&gt;
&lt;p&gt;Java 可以通过
&lt;a href="https://en.wikipedia.org/wiki/Java_Native_Interface"&gt;Java Native Interface (JNI)&lt;/a&gt;
加载共享对象。&lt;a href="https://docs.rs/jni/"&gt;&lt;code&gt;jni&lt;/code&gt; crate&lt;/a&gt; 允许你创建兼容的库。&lt;/p&gt;
&lt;p&gt;首先，创建一个要导出给 Java 的 Rust 函数：&lt;/p&gt;
&lt;p&gt;&lt;em&gt;interoperability/java/src/lib.rs&lt;/em&gt;：&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-rust,compile_fail" data-lang="rust,compile_fail"&gt;// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
//! Rust &amp;lt;-&amp;gt; Java FFI demo.

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

/// HelloWorld::hello method implementation.
// SAFETY: There is no other global function of this name.
#[unsafe(no_mangle)]
pub extern &amp;#34;system&amp;#34; fn Java_HelloWorld_hello(
 mut env: JNIEnv,
 _class: JClass,
 name: JString,
) -&amp;gt; jstring {
 let input: String = env.get_string(&amp;amp;name).unwrap().into();
 let greeting = format!(&amp;#34;Hello, {input}!&amp;#34;);
 let output = env.new_string(greeting).unwrap();
 output.into_raw()
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;em&gt;interoperability/java/Android.bp&lt;/em&gt;：&lt;/p&gt;</description></item></channel></rss>