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. }
  10. // Generate the C++ code that Rust calls into.
  11. genrule {
  12. name: "libcxx_test_bridge_code",
  13. tools: ["cxxbridge"],
  14. cmd: "$(location cxxbridge) $(in) > $(out)",
  15. srcs: ["lib.rs"],
  16. out: ["lib.rs.cc"],
  17. }
  • 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.