{"id":19753,"date":"2024-01-04T11:06:26","date_gmt":"2024-01-04T03:06:26","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=19753"},"modified":"2024-01-04T11:06:26","modified_gmt":"2024-01-04T03:06:26","slug":"rust%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2024\/19753\/","title":{"rendered":"rust\u5f00\u53d1\u5b89\u5353app\u64cd\u4f5c\u65b9\u6cd5"},"content":{"rendered":"
Rust\u5f00\u53d1\u5b89\u5353APP\uff1a\u539f\u7406\u4e0e\u8be6\u7ec6\u4ecb\u7ecd<\/p>\n
Rust\u662f\u4e00\u79cd\u73b0\u4ee3\u591a\u7528\u9014\u7f16\u7a0b\u8bed\u8a00\uff0c\u56e0\u4e3a\u5176\u9ad8\u5b89\u5168\u6027\u548c\u6027\u80fd\u6df1\u53d7\u5e7f\u5927\u5f00\u53d1\u8005\u559c\u6b22\u3002\u5982\u679c\u4f60\u60f3\u4f7f\u7528Rust\u5f00\u53d1Android\u5e94\u7528\u7a0b\u5e8f\uff0c\u672c\u6559\u7a0b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u5176\u539f\u7406\u53ca\u8be6\u7ec6\u8fc7\u7a0b\u3002<\/p>\n
\u4e00\u3001Rust\u5f00\u53d1\u5b89\u5353APP\u7684\u539f\u7406<\/p>\n
\u4f7f\u7528Rust\u7f16\u5199\u7684Android\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u91c7\u7528JNI (Java Native Interface)\u4e0eAndroid\u7cfb\u7edf\u6253\u4ea4\u9053\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Rust\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u6838\u5fc3\u529f\u80fd\uff0c\u5e76\u901a\u8fc7JNI\u4e0eJava\u5c42\u4ee3\u7801\u8fdb\u884c\u901a\u4fe1\uff0c\u4ece\u800c\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684Android\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n
\u4e8c\u3001\u8be6\u7ec6\u64cd\u4f5c\u6b65\u9aa4<\/p>\n
1. \u73af\u5883\u914d\u7f6e<\/p>\n
\u8981\u5f00\u59cb\u4f7f\u7528Rust\u5f00\u53d1Android\u5e94\u7528\u7a0b\u5e8f\uff0c\u9996\u5148\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u6b63\u786e\u5b89\u88c5\u4e86\u4ee5\u4e0b\u5de5\u5177\uff1a<\/p>\n
– Rust \u7f16\u7a0b\u8bed\u8a00\uff08https:\/\/www.rust-lang.org\/tools\/install\uff09<\/p>\n
– Android\u5f00\u53d1\u73af\u5883\uff08Android Studio: https:\/\/developer.android.com\/studio\/index.html\uff09<\/p>\n
– NDK (Native Development Kit) \uff08https:\/\/developer.android.com\/ndk\/guides\uff09<\/p>\n
2. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b89\u5353\u9879\u76ee<\/p>\n
\u4f7f\u7528Android Studio\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Android\u9879\u76ee\uff0c\u9009\u62e9\u9002\u5f53\u7684Android\u5e94\u7528\u7a0b\u5e8f\u6a21\u677f\u3002<\/p>\n
3. \u65b0\u5efaRust\u5e93<\/p>\n
\u5728Android\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2aRust\u5de5\u7a0b\uff0c\u5c06\u5176\u547d\u540d\u4e3a\u201crust\u201d\uff1a<\/p>\n
“`bash<\/p>\n
$ cargo new –lib rust<\/p>\n
“`<\/p>\n
4. \u7f16\u8f91Cargo.toml\u6587\u4ef6<\/p>\n
\u6253\u5f00\u201crust\u201d\u6587\u4ef6\u5939\u4e0b\u7684Cargo.toml\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n
“`<\/p>\n
[lib]<\/p>\n
crate-type = [“cdylib”]<\/p>\n
[dependencies]<\/p>\n
jni = “0.16.0”<\/p>\n
“`<\/p>\n
5. \u7f16\u5199Rust\u4ee3\u7801<\/p>\n
\u6253\u5f00\u201crust\u201d\u6587\u4ef6\u5939\u4e0b\u7684src\/lib.rs\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n
“`rust<\/p>\n
use jni::JNIEnv;<\/p>\n
use jni::objects::{JClass, JString};<\/p>\n
use jni::sys::jstring;<\/p>\n
#[no_mangle]<\/p>\n
pub extern “C” fn Java_com_example_project_MainActivity_hello(<\/p>\n
env: JNIEnv,<\/p>\n
_: JClass,<\/p>\n
greet: JString,<\/p>\n
) -> jstring {<\/p>\n
let hello = env.get_string(greet).expect(“Couldn’t get Java string!”);<\/p>\n
let response = format!(“Hello, {}!”, hello);<\/p>\n
let response = env.new_string(response).expect(“Couldn’t create Java string!”);<\/p>\n
response.into_inner()<\/p>\n
}<\/p>\n
“`<\/p>\n
\u6ce8\u610f\uff1a\u5176\u4e2d`Java_com_example_project_MainActivity_hello`\u8868\u793aJNI\u65b9\u6cd5\uff0c\u4f60\u9700\u8981\u6839\u636e\u81ea\u5df1\u7684\u9879\u76ee\u5305\u540d\u53caMainActivity\u547d\u540d\u81ea\u884c\u8c03\u6574\u3002<\/p>\n
6. \u7f16\u5199`build.rs`\u811a\u672c<\/p>\n
\u5728\u201crust\u201d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cbuild.rs\u201d\u7684\u6587\u4ef6\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n
“`rust<\/p>\n
use std::env;<\/p>\n
use std::fs::File;<\/p>\n
use std::io::Write;<\/p>\n
use std::path::Path;<\/p>\n
fn main() {<\/p>\n
let target = env::var(“TARGET”).unwrap();<\/p>\n
let output_dir = env::var(“OUT_DIR”).unwrap();<\/p>\n
let output_file = Path::new(&output_dir).join(“target_arch.rs”);<\/p>\n
let mut output = File::create(output_file).unwrap();<\/p>\n
if target.contains(“aarch64”) {<\/p>\n
write!(output, “pub const ARCH: &str = \\”{}\\”;”, “aarch64”).unwrap();<\/p>\n
} else if target.contains(“arm”) {<\/p>\n
write!(output, “pub const ARCH: &str = \\”{}\\”;”, “armeabi-v7a”).unwrap();<\/p>\n
} else if target.contains(“x86_64”) {<\/p>\n
write!(output, “pub const ARCH: &str = \\”{}\\”;”, “x86_64”).unwrap();<\/p>\n
} else if target.contains(“i686”) {<\/p>\n
write!(output, “pub const ARCH: &str = \\”{}\\”;”, “x86”).unwrap();<\/p>\n
} else {<\/p>\n
panic!(“Unsupported<\/p>\n
target architecture: {}”, target);<\/p>\n }<\/p>\n }<\/p>\n “`<\/p>\n 7. \u66f4\u65b0Android\u9879\u76ee\u7684build.gradle\u6587\u4ef6<\/p>\n \u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684`build.gradle`\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n “`gradle<\/p>\n …<\/p>\n android {<\/p>\n …<\/p>\n sourceSets {<\/p>\n main {<\/p>\n jniLibs.srcDirs = [‘src\/main\/jniLibs’]<\/p>\n }<\/p>\n }<\/p>\n }<\/p>\n …<\/p>\n task buildRust(type: Exec) {<\/p>\n inputs.dir(“rust\/src”)<\/p>\n inputs.file(“rust\/Cargo.toml”)<\/p>\n outputs.dir(“src\/main\/jniLibs”)<\/p>\n commandLine ‘cargo’, ‘build’, ‘–release’, ‘–target-dir’, ‘target’, ‘–target’, ‘aarch64-linux-android’<\/p>\n commandLine ‘cargo’, ‘build’, ‘–release’, ‘–target-dir’, ‘target’, ‘–target’, ‘armv7-linux-androideabi’<\/p>\n commandLine ‘cargo’, ‘build’, ‘–release’, ‘–target-dir’, ‘target’, ‘–target’, ‘x86_64-linux-android’<\/p>\n commandLine ‘cargo’, ‘build’, ‘–release’, ‘–target-dir’, ‘target’, ‘–target’, ‘i686-linux-android’<\/p>\n workingDir ‘.\/rust’<\/p>\n }<\/p>\n<\/figure>\n<\/p>\n