รูปแบบสนิมของ Android

หน้านี้มีข้อมูลเกี่ยวกับ Android Logging มีตัวอย่าง Rust AIDL ที่บอกวิธี โทรหา Rust จาก C และให้วิธีการ สำหรับการทำงานร่วมกันของ Rust/C++ โดยใช้ CXX

การบันทึกของ Android

ตัวอย่างต่อไปนี้แสดงวิธีบันทึกข้อความไปยัง logcat (ในอุปกรณ์) หรือ stdout (ในโฮสต์)

ในโมดูล Android.bp ให้เพิ่ม liblogger และ liblog_rust เป็นทรัพยากร Dependency:

rust_binary {
    name: "logging_test",
    srcs: ["src/main.rs"],
    rustlibs: [
        "liblogger",
        "liblog_rust",
    ],
}

จากนั้น ในแหล่งที่มาของ Rust ให้เพิ่มโค้ดนี้

use log::{debug, error, LevelFilter};

fn main() {
    let _init_success = logger::init(
        logger::Config::default()
            .with_tag_on_device("mytag")
            .with_max_level(LevelFilter::Trace),
    );
    debug!("This is a debug message.");
    error!("Something went wrong!");
}

กล่าวคือ เพิ่มทรัพยากร Dependency 2 รายการที่แสดงด้านบน (liblogger และ liblog_rust) เรียกใช้เมธอด init ครั้งเดียว (คุณจะเรียกเมธอดนี้ได้มากกว่า 1 ครั้งหากจำเป็น) และ บันทึกข้อความโดยใช้มาโครที่ให้ไว้ โปรดดู ลังไม้ สำหรับรายการตัวเลือกการกำหนดค่าที่เป็นไปได้

ลังบันทึกจะมี API สำหรับกำหนดสิ่งที่คุณต้องการบันทึก ขึ้นอยู่กับ โค้ดกำลังทำงานในอุปกรณ์หรือบนโฮสต์ (เช่น ส่วนหนึ่งของ การทดสอบฝั่งโฮสต์) ข้อความจะถูกบันทึกโดยใช้ android_logger หรือ env_logger

ตัวอย่างของ Rust AIDL

ส่วนนี้จะแสดงตัวอย่างสไตล์ Hello World ของการใช้ AIDL กับ Rust

การใช้คู่มือนักพัฒนาซอฟต์แวร์ Android ภาพรวมเกี่ยวกับ AIDL เป็นจุดเริ่มต้น สร้าง external/rust/binder_example/aidl/com/example/android/IRemoteService.aidl ที่มีเนื้อหาต่อไปนี้ในไฟล์ IRemoteService.aidl

// IRemoteService.aidl
package com.example.android;

// Declare any non-default types here with import statements

/** Example service interface */
interface IRemoteService {
    /** Request the process ID of this service, to do evil things with it. */
    int getPid();

    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

จากนั้น ภายในไฟล์ external/rust/binder_example/aidl/Android.bp ให้กำหนด โมดูล aidl_interface คุณต้องเปิดใช้แบ็กเอนด์ของ Rust อย่างชัดแจ้ง ไม่ได้เปิดใช้โดยค่าเริ่มต้น

aidl_interface {
    name: "com.example.android.remoteservice",
    srcs: [ "aidl/com/example/android/*.aidl", ],
    unstable: true, // Add during development until the interface is stabilized.
    backend: {
        rust: {
            // By default, the Rust backend is not enabled
            enabled: true,
        },
    },
}

แบ็กเอนด์ AIDL เป็นเครื่องมือสร้างแหล่งที่มาของ Rust จึงทำงานเหมือนกับแหล่งที่มา Rust อื่นๆ และสร้างไลบรารี Rust โมดูลไลบรารีของ Rust ที่สร้างขึ้นนั้นสามารถ โมดูล Rust อื่นๆ ใช้เป็นทรัพยากร Dependency ตัวอย่างการใช้ ไลบรารีเป็นทรัพยากร Dependency rust_library สามารถกำหนดได้ดังนี้ external/rust/binder_example/Android.bp:

rust_library {
    name: "libmyservice",
    srcs: ["src/lib.rs"],
    crate_name: "myservice",
    rustlibs: [
        "com.example.android.remoteservice-rust",
        "libbinder_rs",
    ],
}

โปรดทราบว่ารูปแบบชื่อโมดูลสำหรับไลบรารีที่ AIDL สร้างขึ้นซึ่งใช้ใน rustlibs คือชื่อโมดูล aidl_interface ตามด้วย -rust ในกรณีนี้ com.example.android.remoteservice-rust

จากนั้นจะอ้างอิงอินเทอร์เฟซ AIDL ได้ใน src/lib.rs ดังนี้

// Note carefully the AIDL crates structure:
// * the AIDL module name: "com_example_android_remoteservice"
// * next "::aidl"
// * next the AIDL package name "::com::example::android"
// * the interface: "::IRemoteService"
// * finally, the 'BnRemoteService' and 'IRemoteService' submodules

//! This module implements the IRemoteService AIDL interface
use com_example_android_remoteservice::aidl::com::example::android::{
  IRemoteService::{BnRemoteService, IRemoteService}
};
use binder::{
    BinderFeatures, Interface, Result as BinderResult, Strong,
};

/// This struct is defined to implement IRemoteService AIDL interface.
pub struct MyService;

impl Interface for MyService {}

impl IRemoteService for MyService {
    fn getPid(&self) -> BinderResult<i32> {
        Ok(42)
    }

    fn basicTypes(&self, _: i32, _: i64, _: bool, _: f32, _: f64, _: &str) -> BinderResult<()> {
        // Do something interesting...
        Ok(())
    }
}

สุดท้าย ให้เริ่มต้นบริการในไบนารีของ Rust ดังที่แสดงด้านล่าง

use myservice::MyService;

fn main() {
    // [...]
    let my_service = MyService;
    let my_service_binder = BnRemoteService::new_binder(
        my_service,
        BinderFeatures::default(),
    );
    binder::add_service("myservice", my_service_binder.as_binder())
        .expect("Failed to register service?");
    // Does not return - spawn or perform any work you mean to do before this call.
    binder::ProcessState::join_thread_pool()
}

ตัวอย่าง Async Rust AIDL

ส่วนนี้จะแสดงตัวอย่างสไตล์ Hello World ของการใช้ AIDL กับ Rust แบบไม่พร้อมกัน

ต่อจากตัวอย่าง RemoteService ไลบรารีแบ็กเอนด์ AIDL ที่สร้างขึ้น มีอินเทอร์เฟซแบบไม่พร้อมกันที่สามารถใช้เพื่อติดตั้งใช้งานเซิร์ฟเวอร์แบบไม่พร้อมกัน การใช้งานสำหรับอินเทอร์เฟซ AIDL RemoteService

อินเทอร์เฟซเซิร์ฟเวอร์แบบไม่พร้อมกันที่สร้างขึ้น IRemoteServiceAsyncServer สามารถสร้างได้ ดังต่อไปนี้

use com_example_android_remoteservice::aidl::com::example::android::IRemoteService::{
    BnRemoteService, IRemoteServiceAsyncServer,
};
use binder::{BinderFeatures, Interface, Result as BinderResult};

/// This struct is defined to implement IRemoteServiceAsyncServer AIDL interface.
pub struct MyAsyncService;

impl Interface for MyAsyncService {}

#[async_trait]
impl IRemoteServiceAsyncServer for MyAsyncService {
    async fn getPid(&self) -> BinderResult<i32> {
        //Do something interesting...
        Ok(42)
    }

    async fn basicTypes(&self, _: i32, _: i64, _: bool, _: f32, _: f64,_: &str,) -> BinderResult<()> {
        //Do something interesting...
        Ok(())
    }
}

การใช้งานเซิร์ฟเวอร์แบบไม่พร้อมกันสามารถเริ่มต้นได้ดังนี้

#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main() {
    binder::ProcessState::start_thread_pool();

    let my_service = MyAsyncService;
    let my_service_binder = BnRemoteService::new_async_binder(
        my_service,
        TokioRuntime(Handle::current()),
        BinderFeatures::default(),
    );

    binder::add_service("myservice", my_service_binder.as_binder())
        .expect("Failed to register service?");

    task::block_in_place(move || {
        binder::ProcessState::join_thread_pool();
    });
}

โปรดทราบว่า block_in_place เพื่อออกจากบริบทแบบไม่พร้อมกันซึ่งทำให้ join_thread_pool สามารถใช้ block_on ภายใน เนื่องจาก #[tokio::main] ทำการรวมโค้ด ในการโทรถึง block_on และ join_thread_pool อาจโทรหา block_on เมื่อจัดการธุรกรรมที่เข้ามาใหม่ กำลังโทรหา block_on จากภายใน block_on ทำให้เกิดความตื่นตระหนก การทำเช่นนี้อาจ ควรหลีกเลี่ยงโดยการสร้างรันไทม์ Tokio ด้วยตนเอง แทนการใช้ #[tokio::main] แล้วโทรไปที่ join_thread_pool นอกเมธอด block_on

นอกจากนี้ ไลบรารีที่สร้างขึ้นจากสนิมเสริมด้วยอินเทอร์เฟซที่ช่วยให้ กำลังปรับใช้ไคลเอ็นต์แบบไม่พร้อมกัน IRemoteServiceAsync สำหรับ RemoteService ซึ่งสามารถ นำไปปฏิบัติได้ดังนี้

use com_example_android_remoteservice::aidl::com::example::android::IRemoteService::IRemoteServiceAsync;
use binder_tokio::Tokio;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let binder_service = binder_tokio::wait_for_interface::<dyn IRemoteServiceAsync<Tokio>>("myservice");

    let my_client = binder_service.await.expect("Cannot find Remote Service");

    let result = my_client.getPid().await;

    match result {
        Err(err) => panic!("Cannot get the process id from Remote Service {:?}", err),
        Ok(p_id) => println!("PID = {}", p_id),
    }
}

โทรหา Rust จาก C

ตัวอย่างนี้แสดงวิธีเรียก Rust จาก C

ตัวอย่างไลบรารี Rust

กำหนดไฟล์ libsimple_printer ใน external/rust/simple_printer/libsimple_printer.rs ดังนี้

//! A simple hello world example that can be called from C

#[no_mangle]
/// Print "Hello Rust!"
pub extern fn print_c_hello_rust() {
    println!("Hello Rust!");
}

ไลบรารีของ Rust ต้องกำหนดส่วนหัวที่โมดูล C ที่เกี่ยวข้องสามารถดึงเข้ามาได้ ดังนั้นให้กำหนดส่วนหัว external/rust/simple_printer/simple_printer.h ดังนี้

#ifndef SIMPLE_PRINTER_H
#define SIMPLE_PRINTER_H

void print_c_hello_rust();


#endif

นิยามคำว่า external/rust/simple_printer/Android.bp ตามที่คุณเห็นที่นี่

rust_ffi {
    name: "libsimple_c_printer",
    crate_name: "simple_c_printer",
    srcs: ["libsimple_c_printer.rs"],

    // Define export_include_dirs so cc_binary knows where the headers are.
    export_include_dirs: ["."],
}

ตัวอย่างไบนารี C

กําหนด external/rust/c_hello_rust/main.c ดังนี้

#include "simple_printer.h"

int main() {
  print_c_hello_rust();
  return 0;
}

กําหนด external/rust/c_hello_rust/Android.bp ดังนี้

cc_binary {
    name: "c_hello_rust",
    srcs: ["main.c"],
    shared_libs: ["libsimple_c_printer"],
}

ขั้นตอนสุดท้าย ให้สร้างโดยการเรียกใช้ m c_hello_rust

การทำงานร่วมกันของ Rust-Java

ลัง jni ให้ความสามารถในการทำงานร่วมกันของ Rust กับ Java ผ่าน Java Native Interface (JNI) กำหนดคำจำกัดความของประเภทที่จำเป็นสำหรับ Rust ในการสร้าง ไลบรารีของ Rust cdylib ที่เสียบเข้ากับ JNI ของ Java โดยตรง (JNIEnv, JClass, JString และอื่นๆ) ซึ่งต่างจากการเชื่อมโยง C++ ที่ทำการโค้ดเจนผ่าน cxx ความสามารถในการทำงานร่วมกันของ Java ผ่าน JNI ไม่จำเป็นต้องมีขั้นตอนการสร้างโค้ด ระหว่างการสร้าง ดังนั้น จึงไม่จำเป็นต้องมีการสนับสนุนระบบบิลด์เป็นพิเศษ ชวา โค้ดจะโหลด cdylib ที่ได้จาก Rust เช่นเดียวกับไลบรารีแบบเนทีฟอื่นๆ

การใช้งาน

การใช้งานทั้งในโค้ด Rust และ Java ครอบคลุมอยู่ใน เอกสารประกอบเกี่ยวกับลัง jni โปรด ทำตามการเริ่มต้นใช้งาน จากตัวอย่างที่ให้ไว้ หลังจากเขียน src/lib.rs ให้กลับมาที่หน้านี้เพื่อ เรียนรู้วิธีสร้างไลบรารีด้วยระบบบิลด์ของ Android

คำจำกัดความของบิลด์

Java กำหนดให้ระบุไลบรารี Rust เป็น cdylib เพื่อให้สามารถ ที่โหลดแบบไดนามิก คำจำกัดความของไลบรารี Rust ใน Soong มีดังต่อไปนี้

rust_ffi_shared {
    name: "libhello_jni",
    crate_name: "hello_jni",
    srcs: ["src/lib.rs"],

    // The jni crate is required
    rustlibs: ["libjni"],
}

ไลบรารี Java แสดงไลบรารี Rust เป็นทรัพยากร Dependency ของ required วิธีนี้จะทำให้มั่นใจได้ว่ามีการติดตั้งไปยังอุปกรณ์ที่ควบคู่กับไลบรารี Java แม้ว่า วิธีนี้ไม่ใช่ทรัพยากร Dependency ของเวลาบิลด์

java_library {
        name: "libhelloworld",
        [...]
        required: ["libhellorust"]
        [...]
}

หรือคุณต้องรวมไลบรารี Rust ไว้ใน AndroidManifest.xml ให้เพิ่มไลบรารีลงใน uses_libs ดังนี้

java_library {
        name: "libhelloworld",
        [...]
        uses_libs: ["libhellorust"]
        [...]
}

การทำงานร่วมกันของ Rust–C++ โดยใช้ CXX

ลัง CXX ให้ FFI ที่ปลอดภัย ระหว่าง Rust และเซตย่อยของ C++ เอกสารประกอบของ CXX ยกตัวอย่างที่ดีของวิธีการทำงานโดยทั่วไป และเราขอแนะนำให้อ่านก่อน เพื่อทำความคุ้นเคยกับไลบรารี และวิธีการที่ไลบรารีดังกล่าวเชื่อม C++ กับ Rust ตัวอย่างต่อไปนี้จะแสดงวิธีใช้ใน Android

หากต้องการให้ CXX สร้างโค้ด C++ ที่ Rust เรียกใช้ ให้กำหนด genrule เป็น เรียกใช้ CXX และ cc_library_static เพื่อรวมกลุ่มนั้นลงในไลบรารี หากวางแผน เพื่อให้เรียกโค้ด Rust ใน C++ หรือใช้ประเภทที่แชร์ระหว่าง C++ และ Rust ให้กำหนด กฎรุ่นที่ 2 (เพื่อสร้างส่วนหัว C++ ที่มีการเชื่อมโยง Rust)

cc_library_static {
    name: "libcxx_test_cpp",
    srcs: ["cxx_test.cpp"],
    generated_headers: [
        "cxx-bridge-header",
        "libcxx_test_bridge_header"
    ],
    generated_sources: ["libcxx_test_bridge_code"],
}

// Generate the C++ code that Rust calls into.
genrule {
    name: "libcxx_test_bridge_code",
    tools: ["cxxbridge"],
    cmd: "$(location cxxbridge) $(in) > $(out)",
    srcs: ["lib.rs"],
    out: ["libcxx_test_cxx_generated.cc"],
}

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

เครื่องมือ cxxbridge ด้านบนเพื่อสร้างด้าน C++ ของบริดจ์ libcxx_test_cpp ไลบรารีแบบคงที่จะใช้เป็นทรัพยากร Dependency สำหรับไฟล์ปฏิบัติการ Rust ต่อไปนี้

rust_binary {
    name: "cxx_test",
    srcs: ["lib.rs"],
    rustlibs: ["libcxx"],
    static_libs: ["libcxx_test_cpp"],
}

ในไฟล์ .cpp และ .hpp ให้กำหนดฟังก์ชัน C++ ตามต้องการ โดยใช้ประเภท Wrapper CXX ตามต้องการ ตัวอย่างเช่น คําจํากัดความ cxx_test.hpp จะมีข้อมูลต่อไปนี้

#pragma once

#include "rust/cxx.h"
#include "lib.rs.h"

int greet(rust::Str greetee);

ขณะที่ cxx_test.cppมี

#include "cxx_test.hpp"
#include "lib.rs.h"

#include <iostream>

int greet(rust::Str greetee) {
  std::cout << "Hello, " << greetee << std::endl;
  return get_num();
}

หากต้องการใช้จาก Rust ให้กำหนดบริดจ์ CXX ใน lib.rs ดังต่อไปนี้

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        include!("cxx_test.hpp");
        fn greet(greetee: &str) -> i32;
    }
    extern "Rust" {
        fn get_num() -> i32;
    }
}

fn main() {
    let result = ffi::greet("world");
    println!("C++ returned {}", result);
}

fn get_num() -> i32 {
    return 42;
}