AIDL Client

Finally, we can create a Rust client for our new service.

birthday_service/src/client.rs:

  1. //! Birthday service.
  2. use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
  3. use com_example_birthdayservice::binder;
  4. const SERVICE_IDENTIFIER: &str = "birthdayservice";
  5. /// Connect to the BirthdayService.
  6. pub fn connect() -> Result<binder::Strong<dyn IBirthdayService>, binder::StatusCode> {
  7. binder::get_interface(SERVICE_IDENTIFIER)
  8. }
  9. /// Call the birthday service.
  10. fn main() -> Result<(), binder::Status> {
  11. let name = std::env::args()
  12. .nth(1)
  13. .unwrap_or_else(|| String::from("Bob"));
  14. let years = std::env::args()
  15. .nth(2)
  16. .and_then(|arg| arg.parse::<i32>().ok())
  17. .unwrap_or(42);
  18. binder::ProcessState::start_thread_pool();
  19. let service = connect().expect("Failed to connect to BirthdayService");
  20. let msg = service.wishHappyBirthday(&name, years)?;
  21. println!("{msg}");
  22. Ok(())
  23. }

birthday_service/Android.bp:

  1. rust_binary {
  2. name: "birthday_client",
  3. crate_name: "birthday_client",
  4. srcs: ["src/client.rs"],
  5. rustlibs: [
  6. "com.example.birthdayservice-rust",
  7. "libbinder_rs",
  8. ],
  9. prefer_rlib: true,
  10. }

Notice that the client does not depend on libbirthdayservice.

Build, push, and run the client on your device:

  1. $ m birthday_client
  2. $ adb push $ANDROID_PRODUCT_OUT/system/bin/birthday_client /data/local/tmp
  3. $ adb shell /data/local/tmp/birthday_client Charlie 60
  4. Happy Birthday Charlie, congratulations with the 60 years!