src/android/interoperability/with-c/bindgen.md
The bindgen tool can auto-generate bindings from a C header file.
Create a wrapper header file for the library (not strictly needed in this example):
interoperability/bindgen/libbirthday_wrapper.h:
{{#include bindgen/libbirthday_wrapper.h:include}}
interoperability/bindgen/Android.bp:
{{#include bindgen/Android.bp:libbirthday_bindgen}}
Finally, we can use the bindings in our Rust program:
interoperability/bindgen/Android.bp:
{{#include bindgen/Android.bp:print_birthday_card}}
interoperability/bindgen/main.rs:
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include bindgen/main.rs:main}}
The Android build rules will automatically call bindgen for you behind the
scenes.
Notice that the Rust code in main is still hard to write. It is good
practice to encapsulate the output of bindgen in a Rust library which
exposes a safe interface to caller.