Android 框架層AIDL 添加接口

文章目錄

      • AIDL的原理
      • 構建AIDL的流程
      • 往凍結的AIDL中加接口

AIDL的原理

可以利用ALDL定義客戶端與服務均認可的編程接口,以便二者使用進程間通信 (IPC) 進行相互通信。在 Android 中,一個進程通常無法訪問另一個進程的內存。因此,為進行通信,進程需將其對象分解成可供操作系統理解的原語,并將其編組為可供您操作的對象。編寫執行該編組操作的代碼較為繁瑣,因此 Android 會使用 AIDL 為您處理此問題。

AIDL 可以理解成是一個范式, 通過這個范式編寫接口文件, 然后利用Android的AIDL工具 會生成繼承binder所需要能力的頭文件。

構建AIDL的流程

以automotive的audiocontrol模塊為例

  1. 編寫AIDL接口文件,編寫Android.bp, 通過AIDL 生成頭文件
    其aidl的文件位于下面的目錄
hardware/interfaces/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/

編譯會在下面的目錄生成實現binder通信的接口文件。
接口文件有java cpp ndk三種類型。 使得能夠被不同的客戶端和服務端的代碼引用到。

out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-V2-cpp-source/gen/include/android/hardware/automotive/audiocontrol$ ls
AudioFocusChange.h    BnFocusListener.h     BpDuckingInfo.h    IAudioControl.h
BnAudioControl.h      BnMutingInfo.h        BpFocusListener.h  IFocusListener.h
BnAudioFocusChange.h  BpAudioControl.h      BpMutingInfo.h     MutingInfo.h
BnDuckingInfo.h       BpAudioFocusChange.h  DuckingInfo.h
  1. 實現service,實現service對應的bin,以及rc,注冊服務到servicemananger
    AudioContro 實現的demo bin位于hardware/interfaces/automotive/audiocontrol/aidl/default
    目錄下,編譯會生成
    android.hardware.automotive.audiocontrol-service.example這樣的bin 這個bin在 audiocontrol-default.rc 中啟動。
    當然服務端的是 就是把audiocontrol的服務注冊到servicemanger中。
    std::shared_ptr<AudioControl> audioControl = ::ndk::SharedRefBase::make<AudioControl>();const std::string instance = std::string() + AudioControl::descriptor + "/default";binder_status_t status =AServiceManager_addService(audioControl->asBinder().get(), instance.c_str());CHECK_EQ(status, STATUS_OK);

服務的名字在audiocontrol-default.xml中定義為
android.hardware.automotive.audiocontrol.IAudioControl/default

  1. 實現client,獲取service,調用service相關的接口。

clinet 端 主要是通過名字從serviceManager 中獲取到audioControl的服務。然后通過服務調用其接口。
如下通過getService 獲取服務,然后通過名字獲取IAudioControl對象。然后就可以調用其函數了

    private static final String AUDIO_CONTROL_SERVICE ="android.hardware.automotive.audiocontrol.IAudioControl/default";private IBinder mBinder;private IAudioControl mAudioControl;private boolean mListenerRegistered = false;private AudioControlDeathRecipient mDeathRecipient;static @Nullable IBinder getService() {return Binder.allowBlocking(ServiceManager.waitForDeclaredService(AUDIO_CONTROL_SERVICE));}AudioControlWrapperAidl(IBinder binder) {mBinder = Objects.requireNonNull(binder);mAudioControl = IAudioControl.Stub.asInterface(binder);}IBinder binder = AudioControlWrapperAidl.getService();
if (binder != null) {return new AudioControlWrapperAidl(binder);
}@Overridepublic void onAudioFocusChange(@AttributeUsage int usage, int zoneId, int focusChange) {if (Slogf.isLoggable(TAG, Log.DEBUG)) {Slogf.d(TAG, "onAudioFocusChange: usage " + usageToString(usage)+ ", zoneId " + zoneId + ", focusChange " + focusChange);}try {String usageName = usageToXsdString(usage);mAudioControl.onAudioFocusChange(usageName, zoneId, focusChange);} catch (RemoteException e) {throw new IllegalStateException("Failed to query IAudioControl#onAudioFocusChange", e);}}

往凍結的AIDL中加接口

按照Android規則來說 發布之后的AIDL接口是不能修改的。 有相應的Freeze AIDL APIs處理。 從提交記錄看 freeze 的操作是加hash值。

diff --git a/automotive/audiocontrol/aidl/Android.bp b/automotive/audiocontrol/aidl/Android.bp
index 7a947d3ab..4acfd82d6 100644
--- a/automotive/audiocontrol/aidl/Android.bp
+++ b/automotive/audiocontrol/aidl/Android.bp
@@ -19,4 +19,5 @@ aidl_interface {
sdk_version: "module_current",
},
},
+ versions: ["1"],
}
diff --git a/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash b/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash
new file mode 100644
index 000000000..c4bb36b47
--- /dev/null
+++ b/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash
@@ -0,0 +1 @@
+ba2a7caca61683385b3b100e4faab1b4139fc547

看提交記錄加接口的地方:

  1. /aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl
  2. /aidl/default/AudioControl.h
  3. /aidl/default/AudioControl.cpp
diff --git a/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl b/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl
index 4b03af11a..3a0224557 100644
--- a/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl
+++ b/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidldiff --git a/automotive/audiocontrol/aidl/default/AudioControl.cpp b/automotive/audiocontrol/aidl/default/AudioControl.cpp
index 748947cb2..b076d0128 100644
--- a/automotive/audiocontrol/aidl/default/AudioControl.cpp
+++ b/automotive/audiocontrol/aidl/default/AudioControl.cppdiff --git a/automotive/audiocontrol/aidl/default/AudioControl.h b/automotive/audiocontrol/aidl/default/AudioControl.h
index cf5694762..ab0b1b305 100644
--- a/automotive/audiocontrol/aidl/default/AudioControl.h
+++ b/automotive/audiocontrol/aidl/default/AudioControl.h
  • 出現AIDL修改報錯
Above AIDL file(s) has changed and this is NEVER allowed on a release platform
(i.e., PLATFORM_VERSION_CODENAME is REL). If a device is shipped with this
change by ignoring this message, it has a high risk of breaking later when a
module using the interface is updated, e.g., Maineline modules.
11:47:01 ninja failed with: exit status 1

報錯的原因解釋:
stability :此接口的穩定性承諾的可選標志。目前僅支持"vintf" 。如果未設置,則對應于在此編譯上下文中具有穩定性的接口(因此此處加載的接口只能與一起編譯的東西一起使用,例如在 system.img 上)。如果將其設置為"vintf" ,則這對應于穩定性承諾:接口必須在使用期間保持穩定。

解決: 所有的AIDL有關地方的接口都要增加。

  • 出現hash校驗錯誤:
hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocont FAILED: out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-api/checkhash_1.timestamp if [ $(cd 'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1' && { find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version; } | sha1sum | cut -d " " -f 1) = $(read -r <'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash' hash extra; printf %s $hash) ]; then touch out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-api/checkhash_1.timestamp; else cat 'system/tools/aidl/build/message_check_integrity.txt' && exit 1; fi ############################################################################### # ERROR: Modification detected of stable AIDL API file # ############################################################################### Above AIDL file(s) has changed, resulting in a different hash. Hash values may be checked at runtime to verify interface stability. If a device is shipped with this change by ignoring this message, it has a high risk of breaking later when a module using the interface is updated, e.g., Mainline modules. 16:41:52 ninja failed with: exit status 1

錯誤原因:

構建過程未能驗證 AIDL 文件的哈希值,表明發生了修改。
哈希檢查腳本:bash

if [ $(cd 'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1' && { find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version; } | sha1sum | cut -d " " -f 1) = $(read -r <'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash' hash extra; printf %s $hash) ]; then touch out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-api/checkhash_1.timestamp; else cat 'system/tools/aidl/build/message_check_integrity.txt' && exit 1; fi

這是檢查 AIDL 文件哈希值是否與預期哈希值匹配的腳本。如果哈希值匹配,構建過程將繼續進行;否則,將引發錯誤。錯誤消息:makefile
ERROR: Modification detected of stable AIDL API file

  • 修改hash值。
    根據報錯的提交腳本。使用下面的腳本在對應的目錄下生成hash 值, 將這個hash值替換到.hash文件即可
    目錄hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1
{ find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version; } | sha1sum | cut -d " " -f 1
  • 出現這個錯誤

Android will be dropped but finished with status UNKNOWN_TRANSACTION
需要push 所有system/lib底下有關的audiocontrol的so、

使用版本化接口接口方法在運行時,當嘗試在舊服務器上調用新方法時,新客戶端會收到錯誤或異常,具體取決于后端。
cpp后端獲取::android::UNKNOWN_TRANSACTION 。
ndk后端獲取STATUS_UNKNOWN_TRANSACTION 。
java后端獲取android.os.RemoteException并顯示一條消息,說明 API 未實現。

總結: 在凍結的AIDL接口上面加新的接口 需要做的步驟。 但是強烈不建議這么做,可以自己單獨實現一個AIDL接口、AIDL的服務、以及上層的實現

  1. 修改AIDL文件 添加接口
  2. 計算Hash,修改hash 值
  3. 編譯push 生成的so
  4. 在應用上層獲取服務就可以調用到新的接口了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/165854.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/165854.shtml
英文地址,請注明出處:http://en.pswp.cn/news/165854.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

卷積神經網絡(AlexNet)鳥類識別

文章目錄 一、前言二、前期工作1. 設置GPU&#xff08;如果使用的是CPU可以忽略這步&#xff09;2. 導入數據3. 查看數據 二、數據預處理1. 加載數據2. 可視化數據3. 再次檢查數據4. 配置數據集 三、AlexNet (8層&#xff09;介紹四、構建AlexNet (8層&#xff09;網絡模型五、…

微信小程序image組件圖片設置最大寬度 寬高自適應

問題描述&#xff1a;在使用微信小程序image組件的時候&#xff0c;在不確定圖片寬高情況下 想給一個最大寬度讓圖片自適應&#xff0c;按比例&#xff0c;image的widthfiex和heightFiex并不能滿足&#xff08;只指定最大寬/高并不會生效&#xff09; 問題解決&#xff1a;使用…

居家適老化設計第二十九條---衛生間之花灑

無電源 燈光顯示 無障礙扶手型花灑 以上產品圖片均來源于淘寶 侵權聯系刪除 居家適老化衛生間的花灑通常具有以下特點和功能&#xff1a;1. 高度可調節&#xff1a;適老化衛生間花灑可通過調節高度&#xff0c;滿足不同身高的老年人使用需求&#xff0c;避免彎腰或過高伸展造…

【開源】基于Vue.js的固始鵝塊銷售系統

項目編號&#xff1a; S 060 &#xff0c;文末獲取源碼。 \color{red}{項目編號&#xff1a;S060&#xff0c;文末獲取源碼。} 項目編號&#xff1a;S060&#xff0c;文末獲取源碼。 目錄 一、摘要1.1 項目介紹1.2 項目錄屏 二、功能模塊2.1 數據中心模塊2.2 鵝塊類型模塊2.3 固…

qgis添加xyz柵格瓦片

方式1&#xff1a;手動一個個添加 左側瀏覽器-XYZ Tiles-右鍵-新建連接 例如添加高德瓦片地址 https://wprd01.is.autonavi.com/appmaptile?langzh_cn&size1&style7&x{x}&y{y}&z{z} 雙擊即可呈現 收集到的一些圖源&#xff0c;僅供參考&#xff0c;其中一…

【C++學習手札】模擬實現list

? &#x1f3ac;慕斯主頁&#xff1a;修仙—別有洞天 ??今日夜電波&#xff1a;リナリア—まるりとりゅうが 0:36━━━━━━?&#x1f49f;──────── 3:51 &#x1f504; ?? ? ??…

聊聊httpclient的staleConnectionCheckEnabled

序 本文主要研究一下httpclient的staleConnectionCheckEnabled staleConnectionCheckEnabled org/apache/http/client/config/RequestConfig.java public class RequestConfig implements Cloneable {public static final RequestConfig DEFAULT new Builder().build();pr…

【ARM 嵌入式 編譯 Makefile 系列 18 -- Makefile 中的 export 命令詳細介紹】

文章目錄 Makefile 中的 export 命令詳細介紹Makefile 使用 export導出與未導出變量的區別示例&#xff1a;導出變量以供子 Makefile 使用 Makefile 中的 export 命令詳細介紹 在 Makefile 中&#xff0c;export 命令用于將變量從 Makefile 導出到由 Makefile 啟動的子進程的環…

qgis添加wms服務

例如添加geoserver的wms服務 左右瀏覽器-WMS/WMTS-右鍵-新建連接 URL添加geoserver的wms地址 http://{ip}:{port}/geoserver/{workspace}/wms 展開wms目錄&#xff0c;雙擊相應圖層即可打開

Spark---基于Yarn模式提交任務

Yarn模式兩種提交任務方式 一、yarn-client提交任務方式 1、提交命令 ./spark-submit --master yarn --class org.apache.spark.examples.SparkPi ../examples/jars/spark-examples_2.11-2.3.1.jar 100 或者 ./spark-submit --master yarn–client --class org.apache.s…

三菱PLC應用[集錦]

三菱PLC應用[集錦] 如何判斷用PNP還是NPN的個人工作心得 10&#xff5e;30VDC接近開關與PLC連接時&#xff0c;如何判斷用PNP還是NPN的個人工作心得: 對于PLC的開關量輸入回路。我個人感覺日本三菱的要好得多&#xff0c;甚至比西門子等赫赫大名的PLC都要實用和可靠&#xff01…

vulnhub4

靶機地址: https://download.vulnhub.com/admx/AdmX_new.7z 信息收集 fscan 掃一下 ┌──(kali?kali)-[~/Desktop/Tools/fscan] └─$ ./fscan_amd64 -h 192.168.120.138 ___ _ / _ \ ___ ___ _ __ __ _ ___| | __ / /_\/____/ __|/ …

LeetCode | 622. 設計循環隊列

LeetCode | 622. 設計循環隊列 OJ鏈接 思路&#xff1a; 我們這里有一個思路&#xff1a; 插入數據&#xff0c;bank往后走 刪除數據&#xff0c;front往前走 再插入數據&#xff0c;就循環了 那上面這個方法可行嗎&#xff1f; 怎么判斷滿&#xff0c;怎么判斷空&#xff1…

模電知識點總結(二)二極管

系列文章目錄 文章目錄 系列文章目錄二極管二極管電路分析方法理想模型恒壓降模型折線模型小信號模型高頻/開關 二極管應用整流限幅/鉗位開關齊納二極管變容二極管肖特基二極管光電器件光電二極管發光二極管激光二極管太陽能電池 二極管 硅二極管&#xff1a;死區電壓&#xf…

今年注冊電氣工程師考試亂象及就業前景分析

1、注冊電氣工程師掛靠價格 # 2011年以前約為5萬一年&#xff0c;2011年開始強制實施注冊電氣執業制度&#xff0c;證書掛靠價格開始了飛漲&#xff0c;2013年達到巔峰&#xff0c;供配電15萬一年&#xff0c;發輸變電20-25萬一年&#xff0c;這哪里是證書&#xff0c;簡直就是…

Docker kill 命令

docker kill&#xff1a;殺死一個或多個正在運行的容器。 語法&#xff1a; docker kill [OPTIONS] CONTAINER [CONTAINER...]OPTIONS說明&#xff1a; -s&#xff1a;向容器發送一個信號 描述&#xff1a; docker kill子命令會殺死一個或多個容器。容器內的主進程被發送S…

C語言數組的距離(ZZULIOJ1200:數組的距離)

題目描述 已知元素從小到大排列的兩個數組x[]和y[]&#xff0c; 請寫出一個程序算出兩個數組彼此之間差的絕對值中最小的一個&#xff0c;這叫做數組的距離 。 輸入&#xff1a;第一行為兩個整數m, n(1≤m, n≤1000)&#xff0c;分別代表數組f[], g[]的長度。第二行有m個元素&a…

如何在Simulink中使用syms?換個思路解決報錯:Function ‘syms‘ not supported for code generation.

問題描述 在Simulink中的User defined function使用syms函數&#xff0c;報錯simulink無法使用外部函數。 具體來說&#xff1a; 我想在Predefined function定義如下符號函數作為輸入信號&#xff0c;在后續模塊傳入函數參數賦值&#xff0c;以實現一次定義多次使用&#xf…

014:MyString

題目 描述 補足MyString類&#xff0c;使程序輸出指定結果 #include <iostream> #include <string> #include <cstring> using namespace std; class MyString {char * p; public:MyString(const char * s) {if( s) {p new char[strlen(s) 1];strcpy(p,…

最小二乘線性回歸

? 線性回歸&#xff08;linear regression&#xff09;&#xff1a;試圖學得一個線性模型以盡可能準確地預測實際值的輸出。 以一個例子來說明線性回歸&#xff0c;假設銀行貸款會根據 年齡 和 工資 來評估可放款的額度。即&#xff1a; ? 數據&#xff1a;工資和年齡&…