Building in Android

Create two genrules: One to generate the CXX header, and one to generate the CXX source file. These are then used as inputs to the cc_library_static.

1. // Generate a C++ header containing the C++ bindings
2. // to the Rust exported functions in lib.rs.
3. genrule {
4. name: "libcxx_test_bridge_header",
5. tools: ["cxxbridge"],
6. cmd: "$(location cxxbridge) $(in) --header > $(out)",
7. srcs: ["lib.rs"],
8. out: ["lib.rs.h"],
9. }

11. // Generate the C++ code that Rust calls into.
12. genrule {
13. name: "libcxx_test_bridge_code",
14. tools: ["cxxbridge"],
15. cmd: "$(location cxxbridge) $(in) > $(out)",
16. srcs: ["lib.rs"],
17. out: ["lib.rs.cc"],
18. }
  • The cxxbridge tool is a standalone tool that generates the C++ side of the bridge module. It is included in Android and available as a Soong tool.
  • By convention, if your Rust source file is lib.rs your header file will be named lib.rs.h and your source file will be named lib.rs.cc. This naming convention isn’t enforced, though.