06 Android系統(tǒng)之添加java層系統(tǒng)服務(wù)

      網(wǎng)友投稿 1486 2025-04-02

      引入概念

      目前對android系統(tǒng)體系了解比較少,主要區(qū)分一下服務(wù)、系統(tǒng)服務(wù)這兩個概念

      Android服務(wù)是一個后臺運行的組件,執(zhí)行長時間運行且不需要用戶交互的任務(wù)。在android開發(fā)中作為一個應(yīng)用組件,通過繼承類extern Service來使用。

      Android系統(tǒng)服務(wù)。理解為隨著andorid系統(tǒng)啟動運行的service,分為本地守護(hù)進(jìn)程、Native系統(tǒng)服務(wù)和Java系統(tǒng)服務(wù)。

      有相同點更有不同點,但請不要把兩個概念弄混淆了!!!

      然后下面記錄一下添加自定義一個java系統(tǒng)服務(wù)的步驟,參考于qiushao大神的blog。基于android-10版本的AOSP源碼,

      添加服務(wù)

      1、 定義服務(wù)接口

      首先我們得定義我們的服務(wù)名是什么,提供什么樣的接口。在frameworks/base/core/java/android目錄下添加pure文件夾

      $ tree frameworks/base/core/java/android/pure frameworks/base/core/java/android/pure └── IHelloService.aidl # 使用 aidl 定義服務(wù)接口 0 directories, 1 file

      1

      2

      3

      4

      5

      定義接口文件IHelloService.aidl,模塊名IHelloService,接口hello將實現(xiàn)播放指定路徑的音頻文件

      package android.pure; interface IHelloService { void hello(String name); }

      1

      2

      3

      4

      5

      frameworks/base/Android.bp文件中找到模塊名framework-defaults,添加

      "core/java/android/pure/IHelloService.aidl",

      1

      此時,進(jìn)入 framework/base 目錄執(zhí)行 mm -j 命令編譯 framework.jar 模塊。

      編譯成功后,會在 out/soong/.intermediates/frameworks/base/framework/android_common/gen/aidl/frameworks/base/core/java/android/pure 目錄生成 IHelloService.java 這個文件

      2、 實現(xiàn)接口

      然后在frameworks/base/services/core/java/com/android/server下創(chuàng)建HelloService.java文件(接口實現(xiàn)) 內(nèi)容如下

      package com.android.server; import android.pure.IHelloService; import android.util.Log; public class HelloService extends IHelloService.Stub { private final String TAG = "HelloService"; public HelloService() { Log.d(TAG, "create hello service"); } @Override public void hello(String name) { Log.d(TAG, "hello " + name); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      3、 將服務(wù)添加到 ServiceManager

      修改 frameworks/base/services/java/com/android/server/SystemServer.java 文件,在startOtherServices方法里面增加以下代碼

      // add hello service traceBeginAndSlog("HelloService"); ServiceManager.addService("HelloService", new HelloService()); traceEnd();

      1

      2

      3

      4

      4、 編譯驗證 & 系統(tǒng)無法啟動

      現(xiàn)在已經(jīng)實現(xiàn)的HelloService接口模塊,并添加到ServiceManager。開始嘗試整編下android源碼

      $ source ./build/envsetup.sh # 導(dǎo)出環(huán)境變量(之前執(zhí)行過了) $ lunch product01-eng # 選擇Product $ make api-stubs-docs-update-current-api -j4 # 更新API接口 $ make -j4 # 編譯

      1

      2

      3

      4

      然后啟動emulator虛擬機,發(fā)現(xiàn)一直停留在logo界面,說明系統(tǒng)沒起來。。這時候可以adb調(diào)試,我們查看一下log記錄

      $ emulator # 發(fā)現(xiàn)android系統(tǒng)界面沒起來 ... ... $ adb shell logcat -b all > logSystem.txt # 抓取android層的 log ... ^C

      1

      2

      3

      4

      5

      6

      日志中檢索我們想要的關(guān)鍵字HelloService,發(fā)現(xiàn)

      04-02 01:24:25.871 2224 2224 I SystemServer: HelloService 04-02 01:24:25.871 1528 1528 I auditd : avc: denied { add } for service=HelloService pid=2224 uid=1000 scontext=u:r:system_server:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=0 04-02 01:24:25.871 2224 2224 E System : ****************************************** 04-02 01:24:25.871 2224 2224 E System : ************ Failure starting system services 04-02 01:24:25.871 2224 2224 E System : java.lang.SecurityException 04-02 01:24:25.871 2224 2224 E System : at android.os.BinderProxy.transactNative(Native Method) 04-02 01:24:25.871 2224 2224 E System : at android.os.BinderProxy.transact(BinderProxy.java:510) 04-02 01:24:25.871 2224 2224 E System : at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:156) 04-02 01:24:25.871 2224 2224 E System : at android.os.ServiceManager.addService(ServiceManager.java:192) 04-02 01:24:25.871 2224 2224 E System : at android.os.ServiceManager.addService(ServiceManager.java:161) 04-02 01:24:25.871 2224 2224 E System : at com.android.server.SystemServer.startOtherServices(SystemServer.java:920) 04-02 01:24:25.871 2224 2224 E System : at com.android.server.SystemServer.run(SystemServer.java:512) 04-02 01:24:25.871 2224 2224 E System : at com.android.server.SystemServer.main(SystemServer.java:349) 04-02 01:24:25.871 2224 2224 E System : at java.lang.reflect.Method.invoke(Native Method) 04-02 01:24:25.871 2224 2224 E System : at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 04-02 01:24:25.871 2224 2224 E System : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908) 04-02 01:24:25.871 2224 2224 D SystemServerTiming: HelloService took to complete: 1ms 04-02 01:24:25.871 2224 2224 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      然后定位到這一句04-02 01:24:25.871 1528 1528 I auditd : avc: denied { add } for service=HelloService pid=2224 uid=1000 scontext=u:r:system_server:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=0,不理解沒關(guān)系,google一下。發(fā)現(xiàn)是selinux方面的原因。

      為了進(jìn)一步驗證猜想,將系統(tǒng)中的selinux關(guān)掉試試。通過setenforce 0命令,重啟系統(tǒng)發(fā)現(xiàn)正常了,印證了之前的猜想,確實selinux配置的原因。

      $ adb shell setenforce 0 # 禁用selinux $ emulator -wipe-data # 擦掉data區(qū),重啟系統(tǒng)

      1

      2

      3

      4

      5、 設(shè)置selinux規(guī)則

      然后我們只需要添加這個自定義服務(wù)HelloService相關(guān)的 SELinux 規(guī)則。為了方便之后驗證,打開selinux

      $ adb shell setenforce 1 # 打開selinux

      1

      Android 10 的 selinux 規(guī)則是放在 system/sepolicy 目錄下的。不怎么了解SELinux規(guī)則,可以參考現(xiàn)有的系統(tǒng)服務(wù)的規(guī)則去添加,這里參考的是 network_time_update_service 服務(wù)。

      $ cd system/sepolicy # selinux規(guī)則在這個目錄 $ grep -nr network_time_update_service # 查找network_time_update_service服務(wù)相關(guān)的selinux配置

      1

      2

      涉及到的文件很多,有部分文件是不需要修改的,我們先把找到的所有 service.te 和 service_contexts 都參考 network_time_update_service 加上 HelloService 的配置。

      $ find -name service.te ./prebuilts/api/27.0/public/service.te # 需要 ./prebuilts/api/28.0/public/service.te # 需要 ./prebuilts/api/28.0/private/service.te ./prebuilts/api/29.0/public/service.te # 需要 ./prebuilts/api/29.0/private/service.te ./prebuilts/api/26.0/public/service.te # 需要 ./public/service.te # 需要 ./private/service.te $ find -name service_contexts ./reqd_mask/service_contexts ./prebuilts/api/27.0/private/service_contexts # 需要 ./prebuilts/api/28.0/private/service_contexts # 需要 ./prebuilts/api/29.0/private/service_contexts # 需要 ./prebuilts/api/26.0/private/service_contexts # 需要 ./private/service_contexts # 需要

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      其中

      service_contexts上添加HelloService u:object_r:HelloService:s0;

      service.te上添加type HelloService, system_server_service, service_manager_type;。

      6、 編譯驗證

      最后再次編譯一遍,啟動屆滿沒有什么問題了。adb進(jìn)入系統(tǒng)查看一下有沒有這個服務(wù)

      $ source ./build/envsetup.sh # 導(dǎo)出環(huán)境變量(之前執(zhí)行過了) $ lunch product01-eng # 選擇Product $ make api-stubs-docs-update-current-api -j4 # 更新API接口 $ make -j4 # 編譯 .... .... $ adb shell pure:/ # service list | grep HelloService 16 HelloService: [android.pure.IHelloService] pure:/ #

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      錯誤記錄

      1、 添加服務(wù)后,make -j4編譯系統(tǒng)報錯

      error: Added package android.pure [AddedPackage] Aborting: Found compatibility problems checking the public API against the API in /home/mi/source/android-10/frameworks/base/api/current.txt -e ****************************** You have tried to change the API from what has been previously approved. To make these errors go away, you have two choices: 1. You can add '@hide' javadoc comments to the methods, etc. listed in the errors above. 2. You can update current.txt by executing the following command: make api-stubs-docs-update-current-api To submit the revised current.txt to the main Android repository, you will need approval. ****************************** 09:05:24 ninja failed with: exit status 1 #### failed to build some targets (06:37 (mm:ss)) ####

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      解決思路: 沒有更新服務(wù)接口,編譯前執(zhí)行make api-stubs-docs-update-current-api -j4

      2、添加selinux后,編譯報錯

      SELinux: The following public types were found added to the policy without an entry into the compatibility mapping file(s) found in private/compat/V.v/V.v[.ignore].cil, where V.v is the latest API level. HelloService See examples of how to fix this: https://android-review.git.corp.google.com/c/platform/system/sepolicy/+/781036 https://android-review.git.corp.google.com/c/platform/system/sepolicy/+/852612 [ 20% 5/24] build out/target/product/pure/obj/ETC/treble_sepolicy_tests_28.0_intermediates/treble_sepolicy_tests_28.0 FAILED: out/target/product/pure/obj/ETC/treble_sepolicy_tests_28.0_intermediates/treble_sepolicy_tests_28.0 /bin/bash -c "(out/host/linux-x86/bin/treble_sepolicy_tests -l out/host/linux-x86/lib64/libsepolwrap.so -f out/target/product/pure/obj/ETC/plat_file_contexts_intermediates/plat_file_contexts -f out/target/product/pure/obj/ETC/vendor_file_contexts_intermediates/vendor_file_contexts -b out/target/product/pure/obj/ETC/built_plat_sepolicy_intermediates/built_plat_sepolicy -m out/target/product/pure/obj/ETC/treble_sepolicy_tests_28.0_intermediates/28.0_mapping.combined.cil -o out/target/product/pure/obj/ETC/treble_sepolicy_tests_28.0_intermediates/built_28.0_plat_sepolicy -p out/target/product/pure/obj/ETC/sepolicy_intermediates/sepolicy -u out/target/product/pure/obj/ETC/built_plat_sepolicy_intermediates/base_plat_pub_policy.cil --fake-treble ) && (touch out/target/product/pure/obj/ETC/treble_sepolicy_tests_28.0_intermediates/treble_sepolicy_tests_28.0 )" SELinux: The following public types were found added to the policy without an entry into the compatibility mapping file(s) found in private/compat/V.v/V.v[.ignore].cil, where V.v is the latest API level. HelloService See examples of how to fix this: https://android-review.git.corp.google.com/c/platform/system/sepolicy/+/781036 https://android-review.git.corp.google.com/c/platform/system/sepolicy/+/852612 21:45:48 ninja failed with: exit status 1 #### failed to build some targets (16 seconds) ####

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      06 Android系統(tǒng)之添加java層系統(tǒng)服務(wù)

      19

      20

      21

      解決思路: selinux沒配置好,沒有將配置覆蓋所有需要的te文件。檢查本地配置,按照上面的方法,在配置一下。

      3、啟動emulator之后,系統(tǒng)沒起來,同時log報錯

      hinzer@ubuntu:android-10$ emulator -wipe-data emulator: WARNING: Couldn't find crash service executable /home/hinzer/source/android-10/prebuilts/android-emulator/linux-x86_64/emulator64-crash-service emulator: WARNING: system partition size adjusted to match image file (3083 MB > 800 MB) qemu_ram_alloc_user_backed: call context mismatch in svga_surface_destroy # 這個錯誤 ....

      1

      2

      3

      4

      5

      6

      7

      8

      解決方法: 通過網(wǎng)絡(luò)查詢到解決方法,由于我這邊是VM上運行Ubuntu虛擬機,然后在跑emulator。我需要把VM設(shè)置中的顯示器中的圖形渲染關(guān)閉,這一操作需要關(guān)閉虛擬機,重啟后在運行emulator 驗證就沒有問題了。

      小結(jié)

      查看日志 使用logcat -b all,不清楚不要過濾處理,查看全部log

      不確定的情況下,可以系統(tǒng)上直接啟動/禁止selinux驗證猜想,然后進(jìn)一步調(diào)試

      參考資料

      Android系統(tǒng)開發(fā)入門-7.添加java層系統(tǒng)服務(wù)

      官方文檔 - 服務(wù)概覽

      官方文檔- SELinux

      Android 系統(tǒng)服務(wù)

      Android IDE Java

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:圖解 I/O 多路復(fù)用
      下一篇:Excel2010中上下單元格互換的操作方法(excel單元格怎么互換)
      相關(guān)文章
      亚洲伊人久久成综合人影院| 亚洲国产成人爱av在线播放| 在线观看亚洲天天一三视| 亚洲精品av无码喷奶水糖心| 亚洲一区二区三区在线观看蜜桃| 亚洲视频免费播放| 亚洲精品免费在线| 亚洲无限乱码一二三四区| 激情内射亚洲一区二区三区| 亚洲影院在线观看| 亚洲最新视频在线观看| 亚洲AV成人片色在线观看高潮 | 亚洲∧v久久久无码精品| 亚洲日韩精品无码专区网址| 国产AⅤ无码专区亚洲AV| 亚洲精品乱码久久久久久按摩| 亚洲国产精品久久久天堂| 久久香蕉国产线看观看亚洲片| 久久亚洲精品成人777大小说| 亚洲av丰满熟妇在线播放| 91亚洲国产成人精品下载| 亚洲图片校园春色| 国产精品亚洲专区在线观看| 亚洲色大成网站www永久男同| 亚洲av无码成人影院一区| 国产偷国产偷亚洲高清在线| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲日本乱码一区二区在线二产线 | 久久亚洲欧洲国产综合| 久久精品国产69国产精品亚洲| 亚洲乱亚洲乱淫久久| 亚洲成人福利在线| 亚洲熟妇丰满xxxxx| 亚洲A∨精品一区二区三区| 中文字幕亚洲图片| 亚洲伦理一区二区| 亚洲精品456人成在线| jzzijzzij在线观看亚洲熟妇| 亚洲日本一区二区三区在线不卡| 国产自偷亚洲精品页65页| 亚洲四虎永久在线播放|