इस पृष्ठ में एंड्रॉइड लॉगिंग के बारे में जानकारी है, एक जंग एआईडीएल उदाहरण प्रदान करता है, आपको सी से जंग को कॉल करने का तरीका बताता है, और सीएक्सएक्स का उपयोग करके जंग/सी ++ इंटरऑप के लिए निर्देश प्रदान करता है।
एंड्रॉइड लॉगिंग
निम्न उदाहरण दिखाता है कि आप संदेशों को logcat
(ऑन-डिवाइस) या stdout
(ऑन-होस्ट) में कैसे लॉग कर सकते हैं।
अपने Android.bp
मॉड्यूल में, निर्भरता के रूप में liblogger
और liblog_rust
जोड़ें:
rust_binary {
name: "logging_test",
srcs: ["src/main.rs"],
rustlibs: [
"liblogger",
"liblog_rust",
],
}
अगला, अपने जंग स्रोत में यह कोड जोड़ें:
use log::{debug, error, Level};
fn main() {
let init_success = logger::init(
logger::Config::default()
.with_tag_on_device("mytag")
.with_min_level(Level::Trace),
);
debug!("This is a debug message.");
error!("Something went wrong!");
}
यही है, ऊपर दिखाए गए दो निर्भरताओं को जोड़ें ( liblogger
और liblog_rust
), init
विधि को एक बार कॉल करें (यदि आवश्यक हो तो आप इसे एक से अधिक बार कॉल कर सकते हैं), और प्रदान किए गए मैक्रोज़ का उपयोग करके संदेशों को लॉग करें। संभावित कॉन्फ़िगरेशन विकल्पों की सूची के लिए लॉगर क्रेट देखें।
आप जो लॉग करना चाहते हैं उसे परिभाषित करने के लिए लकड़हारा क्रेट एक एपीआई प्रदान करता है। कोड ऑन-डिवाइस या ऑन-होस्ट (जैसे होस्ट-साइड टेस्ट का हिस्सा) चल रहा है या नहीं, इसके आधार पर संदेश android_logger या env_logger का उपयोग करके लॉग किए जाते हैं।
जंग एआईडीएल उदाहरण
यह खंड जंग के साथ एआईडीएल का उपयोग करने का एक हैलो वर्ल्ड-शैली का उदाहरण प्रदान करता है।
शुरुआती बिंदु के रूप में एंड्रॉइड डेवलपर गाइड एआईडीएल अवलोकन अनुभाग का उपयोग करके, IRemoteService.aidl
फ़ाइल में निम्नलिखित सामग्री के साथ external/rust/binder_example/aidl/com/example/android/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
मॉड्यूल को परिभाषित करें। आपको रस्ट बैकएंड को स्पष्ट रूप से सक्षम करना होगा क्योंकि यह डिफ़ॉल्ट रूप से सक्षम नहीं है।
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_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
में उपयोग की जाने वाली एआईडीएल-जेनरेट की गई लाइब्रेरी के लिए मॉड्यूल नाम प्रारूप -rust
के बाद aidl_interface
मॉड्यूल नाम है; इस मामले में, 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 com_example_android_remoteservice::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(())
}
}
अंत में, सेवा को रस्ट बाइनरी में शुरू करें जैसा कि नीचे दिखाया गया है:
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()
}
सी से जंग बुला रहा है
यह उदाहरण दिखाता है कि 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!");
}
रस्ट लाइब्रेरी को उन हेडर को परिभाषित करना चाहिए जो आश्रित सी मॉड्यूल में खींच सकते हैं, इसलिए 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 include_dirs so cc_binary knows where the headers are.
include_dirs: ["."],
}
उदाहरण सी बाइनरी
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
कॉल करके निर्माण करें।
रस्ट-जावा इंटरॉप
jni
क्रेट जावा नेटिव इंटरफेस (जेएनआई) के माध्यम से जावा के साथ रस्ट इंटरऑपरेबिलिटी प्रदान करता है। यह रस्ट के लिए आवश्यक प्रकार की परिभाषाओं को परिभाषित करता है ताकि रस्ट cdylib
लाइब्रेरी का निर्माण किया जा सके जो सीधे जावा के जेएनआई ( JNIEnv
, JClass
, JString
, और इसी तरह) में प्लग हो। सी ++ बाइंडिंग के विपरीत जो cxx
के माध्यम से कोडजेन का प्रदर्शन करते हैं, जेएनआई के माध्यम से जावा इंटरऑपरेबिलिटी को निर्माण के दौरान कोड-जेनरेशन चरण की आवश्यकता नहीं होती है। इसलिए इसे विशेष बिल्ड-सिस्टम समर्थन की आवश्यकता नहीं है। जावा कोड किसी अन्य मूल पुस्तकालय की तरह रस्ट-प्रदत्त cdylib
लोड करता है।
प्रयोग
रस्ट और जावा कोड दोनों में उपयोग jni
क्रेट प्रलेखन में शामिल है। कृपया वहां दिए गए प्रारंभ करना उदाहरण का पालन करें। src/lib.rs
लिखने के बाद, एंड्रॉइड के बिल्ड सिस्टम के साथ लाइब्रेरी बनाने का तरीका जानने के लिए इस पेज पर वापस लौटें।
बिल्ड परिभाषा
जावा को रस्ट लाइब्रेरी को cdylib
के रूप में प्रदान करने की आवश्यकता है ताकि इसे गतिशील रूप से लोड किया जा सके। सूंग में रस्ट लाइब्रेरी की परिभाषा इस प्रकार है:
rust_ffi_shared {
name: "libhello_jni",
crate_name: "hello_jni",
srcs: ["src/lib.rs"],
// The jni crate is required
rustlibs: ["libjni"],
}
जावा लाइब्रेरी रस्ट लाइब्रेरी को एक required
निर्भरता के रूप में सूचीबद्ध करती है; यह सुनिश्चित करता है कि यह जावा लाइब्रेरी के साथ डिवाइस पर स्थापित है, भले ही यह बिल्ड-टाइम निर्भरता नहीं है:
java_library {
name: "libhelloworld",
[...]
required: ["libhellorust"]
[...]
}
वैकल्पिक रूप से, यदि आपको AndroidManifest.xml
फ़ाइल में रस्ट लाइब्रेरी शामिल करनी है, तो लाइब्रेरी को uses_libs
में निम्नानुसार जोड़ें:
java_library {
name: "libhelloworld",
[...]
uses_libs: ["libhellorust"]
[...]
}
रस्ट–सी++ इंटरऑप CXX का उपयोग करना
CXX क्रेट रस्ट और C++ के उपसमुच्चय के बीच सुरक्षित FFI प्रदान करता है। CXX प्रलेखन अच्छे उदाहरण देता है कि यह सामान्य रूप से कैसे काम करता है और हम पुस्तकालय से परिचित होने के लिए पहले इसे पढ़ने का सुझाव देते हैं और जिस तरह से यह C++ और Rust को पाटता है। निम्न उदाहरण दिखाता है कि Android में इसका उपयोग कैसे करें।
सीएक्सएक्स को सी ++ कोड उत्पन्न करने के लिए जिसे रस्ट कॉल करता है, एक पुस्तकालय में बंडल करने के लिए सीएक्सएक्स और एक cc_library_static
को आमंत्रित करने के लिए एक genrule
परिभाषित करें। यदि आप C++ को रस्ट कोड कॉल करने की योजना बनाते हैं, या C++ और रस्ट के बीच साझा किए गए प्रकारों का उपयोग करते हैं, तो एक दूसरे जेनरूल को परिभाषित करें (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"],
}
cxxbridge
टूल का उपयोग पुल के C++ पक्ष को उत्पन्न करने के लिए ऊपर किया गया है। libcxx_test_cpp
स्टैटिक लाइब्रेरी का उपयोग हमारे रस्ट निष्पादन योग्य के लिए निर्भरता के रूप में किया जाता है:
rust_binary {
name: "cxx_test",
srcs: ["lib.rs"],
rustlibs: ["libcxx"],
static_libs: ["libcxx_test_cpp"],
}
.cpp
और .hpp
फ़ाइलों में, CXX रैपर प्रकार का इच्छानुसार उपयोग करते हुए, C++ फ़ंक्शंस को अपनी इच्छानुसार परिभाषित करें। उदाहरण के लिए, एक cxx_test.hpp
परिभाषा में निम्नलिखित शामिल हैं:
#pragma once
#include "rust/cxx.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();
}
रस्ट से इसका उपयोग करने के लिए, एक 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;
}