Back to Comprehensive Rust

Rust Libraries

src/android/build-rules/library.md

latest1.3 KB
Original Source
<!-- Copyright 2022 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

Rust Libraries

You use rust_library to create a new Rust library for Android.

Here we declare a dependency on two libraries:

hello_rust/Android.bp:

javascript
{{#include library/Android.bp}}

hello_rust/src/main.rs:

rust,ignore
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include library/src/main.rs:main}}

hello_rust/src/lib.rs:

rust,ignore
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include library/src/lib.rs:greeting}}

You build, push, and run the binary like before:

shell
{{#include ../build_all.sh:hello_rust_with_dep}}
text
Hello Bob, it is very
nice to meet you!
<details>
  • Go through the build steps and demonstrate them running in your emulator.

  • A Rust crate named greetings must be built by a rule called libgreetings. Note how the Rust code uses the crate name, as is normal in Rust.

  • Again, the build rules enforce that we add documentation comments to all public items.

</details>