Android Rust पैटर्न

इस पेज पर, Android लॉगिंग के बारे में जानकारी दी गई है. इसमें Rust AIDL का एक उदाहरण दिया गया है. साथ ही, इसमें C से Rust को कॉल करने का तरीका और CXX का इस्तेमाल करके Rust/C++ इंटरऑप के लिए निर्देश दिए गए हैं.

Android लॉगिंग

यहां दिए गए उदाहरण में, logcat (उपयोगकर्ता के डिवाइस पर) या stdout (होस्ट पर) में लॉग मैसेज करने का तरीका बताया गया है.

अपने Android.bp मॉड्यूल में, liblogger और liblog_rust को डिपेंडेंसी के तौर पर जोड़ें:

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!");
}

इसका मतलब है कि ऊपर दिखाई गई दो डिपेंडेंसी (liblogger और liblog_rust) जोड़ें, init तरीके को एक बार कॉल करें (ज़रूरत पड़ने पर, इसे एक से ज़्यादा बार कॉल किया जा सकता है) और दिए गए मैक्रो का इस्तेमाल करके मैसेज लॉग करें. कॉन्फ़िगरेशन के संभावित विकल्पों की सूची देखने के लिए, logger crate देखें.

logger crate, लॉग करने के लिए एपीआई उपलब्ध कराता है. कोड, डिवाइस पर चल रहा है या होस्ट पर (जैसे, होस्ट-साइड टेस्ट का हिस्सा), इसके आधार पर मैसेज, android_logger या env_logger का इस्तेमाल करके लॉग किए जाते हैं.

Rust AIDL का उदाहरण

इस सेक्शन में, Rust के साथ AIDL का इस्तेमाल करने का एक उदाहरण दिया गया है. यह उदाहरण, Hello World-स्टाइल में है.

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 मॉड्यूल डिपेंडेंसी के तौर पर इस्तेमाल कर सकते हैं. जनरेट की गई लाइब्रेरी को डिपेंडेंसी के तौर पर इस्तेमाल करने के उदाहरण के तौर पर, 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",
    ],
}

ध्यान दें कि rustlibs में इस्तेमाल की गई, AIDL से जनरेट की गई लाइब्रेरी के मॉड्यूल के नाम का फ़ॉर्मैट, aidl_interface मॉड्यूल का नाम और उसके बाद -rust होता है. इस मामले में, com.example.android.remoteservice-rust है.

इसके बाद, src/lib.rs में AIDL इंटरफ़ेस को इस तरह रेफ़र किया जा सकता है:

// 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()
}

एसिंक Rust AIDL का उदाहरण

इस सेक्शन में, एसिंक Rust के साथ AIDL का इस्तेमाल करने का एक उदाहरण दिया गया है. यह उदाहरण, Hello World-स्टाइल में है.

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::main]` का इस्तेमाल करने के बजाय, tokio रनटाइम को मैन्युअल तरीके से बनाकर भी इससे बचा जा सकता है. इसके बाद, `block_on` तरीके के बाहर `join_thread_pool` को कॉल किया जा सकता है.

इसके अलावा, rust बैकएंड से जनरेट की गई लाइब्रेरी में एक ऐसा इंटरफ़ेस शामिल होता है जिससे एसिंक क्लाइंट 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),
    }
}

C से Rust को कॉल करना

इस उदाहरण में, C से Rust को कॉल करने का तरीका बताया गया है.

Rust लाइब्रेरी का उदाहरण

external/rust/simple_printer/libsimple_printer.rs में libsimple_printer फ़ाइल को इस तरह तय करें:

//! 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 crate, Java Native Interface (JNI) के ज़रिए, Java के साथ Rust की इंटरऑपरेबिलिटी उपलब्ध कराता है. यह Rust के लिए ज़रूरी टाइप की परिभाषाएं तय करता है, ताकि Rust, Rust cdylib लाइब्रेरी बना सके. इसे सीधे Java के JNI (JNIEnv, JClass, JString, वगैरह) में प्लग किया जा सकता है. C++ बाइंडिंग के उलट, जो cxx के ज़रिए कोड जनरेट करती हैं, JNI के ज़रिए Java इंटरऑपरेबिलिटी के लिए, बिल्ड के दौरान कोड जनरेट करने की ज़रूरत नहीं होती. इसलिए, इसे खास बिल्ड-सिस्टम की ज़रूरत नहीं होती. Java कोड, Rust से उपलब्ध कराई गई cdylib को किसी भी नेटिव लाइब्रेरी की तरह लोड करता है.

इस्तेमाल

Rust और Java, दोनों के कोड में इस्तेमाल करने का तरीका, jni crate के दस्तावेज़ में बताया गया है. कृपया वहां दिए गए, शुरू करने के तरीके वाले उदाहरण को फ़ॉलो करें. src/lib.rs लिखने के बाद, इस पेज पर वापस आएं, ताकि Android के बिल्ड सिस्टम के साथ लाइब्रेरी बनाने का तरीका जाना जा सके.

बिल्ड की परिभाषा

Java के लिए, Rust लाइब्रेरी को cdylib के तौर पर उपलब्ध कराना ज़रूरी है, ताकि इसे डाइनैमिक तरीके से लोड किया जा सके. Soong में Rust लाइब्रेरी की परिभाषा यह है:

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

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

Java लाइब्रेरी, Rust लाइब्रेरी को required डिपेंडेंसी के तौर पर लिस्ट करती है. इससे यह पक्का होता है कि Java लाइब्रेरी के साथ-साथ, इसे डिवाइस पर इंस्टॉल किया जाए. भले ही, यह बिल्ड-टाइम डिपेंडेंसी न हो:

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

इसके अलावा, अगर आपको AndroidManifest.xml फ़ाइल में Rust लाइब्रेरी शामिल करनी है, तो लाइब्रेरी को uses_libs में इस तरह जोड़ें:

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

CXX का इस्तेमाल करके Rust–C++ इंटरऑप

CXX crate, Rust और C++ के सबसेट के बीच सुरक्षित FFI उपलब्ध कराता है. CXX के दस्तावेज़ में, यह सामान्य तौर पर कैसे काम करता है, इसके अच्छे उदाहरण दिए गए हैं. हम सुझाव देते हैं कि लाइब्रेरी और C++ और Rust के बीच ब्रिज बनाने के तरीके के बारे में जानने के लिए, इसे पहले पढ़ें. यहां दिए गए उदाहरण में, Android में इसका इस्तेमाल करने का तरीका बताया गया है.

CXX से C++ कोड जनरेट कराने के लिए, जिसे Rust कॉल करता है, CXX को शुरू करने के लिए genrule और उसे लाइब्रेरी में बंडल करने के लिए cc_library_static तय करें. अगर आपको C++ से Rust कोड को कॉल करना है या C++ और Rust के बीच शेयर किए गए टाइप का इस्तेमाल करना है, तो दूसरा genrule तय करें. इससे, Rust बाइंडिंग वाला C++ हेडर जनरेट होगा.

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"],
}

ऊपर, ब्रिज का C++ साइड जनरेट करने के लिए, cxxbridge टूल का इस्तेमाल किया गया है. इसके बाद, libcxx_test_cpp स्टैटिक लाइब्रेरी को, हमारे Rust एक्ज़ीक्यूटेबल के लिए डिपेंडेंसी के तौर पर इस्तेमाल किया जाता है:

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

.cpp और .hpp फ़ाइलों में, C++ फ़ंक्शन को अपनी पसंद के मुताबिक तय करें. इसके लिए, 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 से इसका इस्तेमाल करने के लिए, lib.rs में CXX ब्रिज को इस तरह तय करें:

#[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;
}