3.2 库
02-库 — Comprehensive Rust
1分钟内可阅读完
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/android/build-rules/library.html
3.2 库
使用 rust_library 可为 Android 创建新的 Rust 库。
这里我们声明对两个库的依赖:
libgreeting,在下方定义,libtextwrap,已是external/rust/android-crates-io/crates/中 vendored 的 crate。
hello_rust/Android.bp:
| |
hello_rust/src/main.rs:
// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
//! Rust demo.
use greetings::greeting;
use textwrap::fill;
/// Prints a greeting to standard output.
fn main() {
println!("{}", fill(&greeting("Bob"), 24));
}
hello_rust/src/lib.rs:
// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
//! Greeting library.
/// Greet `name`.
pub fn greeting(name: &str) -> String {
format!("Hello {name}, it is very nice to meet you!")
}
像之前一样构建、推送并运行该二进制:
| |
| |
走一遍构建步骤,并在模拟器中演示运行。
名为
greetings的 Rust crate 必须由名为libgreetings的规则构建。注意 Rust 代码使用的是 crate 名,这与平常的 Rust 用法一致。同样,构建规则要求我们为所有公开项添加文档注释。