本文主要是介绍手把手教你aosp13/14上实现PRODUCT_BOOT_JARS/boot jar(有源码)-千里马framework,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景:
经常设备厂商可能开发一些功能时候会有一些和芯片厂商打交道,比如mtk,海思,amlogic,高通啥的。这些芯片商一些相关功能比如Tv啥的,可能接口各不一样。但是对于设备厂商肯定想要统一的接口对于上层,不可能让设备厂商app根据每个平台出个不一样app。
这种会大大增加app维护成本,故是不推荐的方案。
是否可以把Tv相关app需要接口都做成公共接口jar,然后这个接口jar自己去实现各个平台差异,即每个平台都会有不同的实现,但是对于Tv这个app来说并不关心,因为他只依赖一个公共接口jar就行,至于你这个接口jar具体实现自己根据平台自己来实现。类似依赖是中间件jar,中间层,apk只依赖中间层接口,具体差异中间层自己去平台对接。
但是这个中间件怎么集成好呢?如果依旧编译到apk里面那就和老方案就没啥大差别,一样会让Tv这个apk需要频繁打包,如果中间件的jar放到公共framework的部分,那么这个时候apk就不需要进行针对平台单独打包,所以这个时候就需要学习今天问题重点:
手把手教你实现aosp14上的boot jar。
先直观认识boot jar:
字面意思理解就是一开机启动系统就已经自动加载好了的jar包
具体验证方式:输入$BOOTCLASSPATH命令
emulator_x86_64:/system/etc/permissions # $BOOTCLASSPATH
/system/bin/sh: /apex/com.android.art/javalib/core-oj.jar:/apex/com.android.art/javalib/core-libart.jar:/apex/com.android.art/javalib/okhttp.jar:/apex/com.android.art/javalib/bouncycastle.jar:/apex/com.android.art/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/framework-graphics.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/boot_jar.jar:/system/framework/ims-common.jar:/apex/com.android.i18n/javalib/core-icu4j.jar:/apex/com.android.adservices/javalib/framework-adservices.jar:/apex/com.android.adservices/javalib/framework-sdksandbox.jar:/apex/com.android.appsearch/javalib/framework-appsearch.jar:/apex/com.android.btservices/javalib/framework-bluetooth.jar:/apex/com.android.configinfrastructure/javalib/framework-configinfrastructure.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar:/apex/com.android.devicelock/javalib/framework-devicelock.jar:/apex/com.android.healthfitness/javalib/framework-healthfitness.jar:/apex/com.android.ipsec/javalib/android.net.ipsec.ike.jar:/apex/com.android.media/javalib/updatable-media.jar:/apex/com.android.mediaprovider/javalib/framework-mediaprovider.jar:/apex/com.android.ondevicepersonalization/javalib/framework-ondevicepersonalization.jar:/apex/com.android.os.statsd/javalib/framework-statsd.jar:/apex/com.android.permission/javalib/framework-permission.jar:/apex/com.android.permission/javalib/framework-permission-s.jar:/apex/com.android.scheduling/javalib/framework-scheduling.jar:/apex/com.android.sdkext/javalib/framework-sdkextensions.jar:/apex/com.android.tethering/javalib/framework-connectivity.jar:/apex/com.android.tethering/javalib/framework-connectivity-t.jar:/apex/com.android.tethering/javalib/framework-tethering.jar:/apex/com.android.uwb/javalib/framework-uwb.jar:/apex/com.android.virt/javalib/framework-virtualization.jar:/apex/com.android.wifi/javalib/framework-wifi.jar: inaccessible or not found
实现业务jar包和预制到system/framework目录
framework/base路径下做一个简单的接口实现jar包
commit 10f6d3189f290820f1f7d2b2f88a8ca66855cd56 (HEAD -> android-14.0.0_r15)
Author: Your Name <you@example.com>
Date: Wed Mar 27 22:58:39 2024 +0800add boot jar modifyChange-Id: I27645a05b65c743c8200b4a3b190d6d6908caeeediff --git a/cmds/boot_jar/Android.bp b/cmds/boot_jar/Android.bp
new file mode 100644
index 000000000000..0ca613d3b077
--- /dev/null
+++ b/cmds/boot_jar/Android.bp
@@ -0,0 +1,10 @@
+// Copyright 2008 The Android Open Source Project
+//
+
+java_library {
+ name: "boot_jar",
+ srcs: [
+ "src/**/*.java",
+ ],
+ installable: true,
+}
\ No newline at end of file
diff --git a/cmds/boot_jar/src/com/hello/BootTest.java b/cmds/boot_jar/src/com/hello/BootTest.java
new file mode 100644
index 000000000000..9f1a90d774ec
--- /dev/null
+++ b/cmds/boot_jar/src/com/hello/BootTest.java
@@ -0,0 +1,7 @@
+package com.hello;
+
+public class BootTest {
+ public void testBootJar(){
+ android.util.Log.i("lsm","testBootJar --- realPart");
+ }
+}
\ No newline at end of file
接下来还有关键步骤预制成bootjar
路径build/make/target/product/default_art_config.mk
test@test:~/disk2/aosp14/build/make/target/product$ git diff
diff --git a/target/product/default_art_config.mk b/target/product/default_art_config.mk
index d970203470..72033c1b77 100644
--- a/target/product/default_art_config.mk
+++ b/target/product/default_art_config.mk
@@ -45,6 +45,7 @@ PRODUCT_BOOT_JARS += \ext \telephony-common \voip-common \
+ boot_jar \ims-common# APEX boot jars. Keep the list sorted by module names and then library names.
到这里boot_jar这个jar包就被放入到system/framework目录了
需要添加相关的白名单
build/soong下面对package_allowed_list进行相关包名添加
test@test:~/disk2/aosp14/build/soong$ git diff
diff --git a/scripts/check_boot_jars/package_allowed_list.txt b/scripts/check_boot_jars/package_allowed_list.txt
index 869fd3f83..c00aa29b5 100644
--- a/scripts/check_boot_jars/package_allowed_list.txt
+++ b/scripts/check_boot_jars/package_allowed_list.txt
@@ -254,3 +254,7 @@ com\.google\.i18n\.phonenumbers# Packages used for Android in Chrome OSorg\.chromium\.arcorg\.chromium\.arc\..*
+###################################################
+# Packages used for com.hello
+com\.hello
+com\.hello\..*
\ No newline at end of file
测试程序
上面只是写好了一个bootjar需要开始验证是否可以使用这个bootjar
1、验证一下系统启动后是否已经有了这个jar
emulator_x86_64:/system/etc/permissions # $BOOTCLASSPATH
/system/bin/sh: /apex/com.android.art/javalib/core-oj.jar:/apex/com.android.art/javalib/core-libart.jar:/apex/com.android.art/javalib/okhttp.jar:/apex/com.android.art/javalib/bouncycastle.jar:/apex/com.android.art/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/framework-graphics.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/boot_jar.jar:/system/framework/ims-common.jar:/apex/com.android.i18n/javalib/core-icu4j.jar:/apex/com.android.adservices/javalib/framework-adservices.jar:/apex/com.android.adservices/javalib/framework-sdksandbox.jar:/apex/com.android.appsearch/javalib/framework-appsearch.jar:/apex/com.android.btservices/javalib/framework-bluetooth.jar:/apex/com.android.configinfrastructure/javalib/framework-configinfrastructure.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar:/apex/com.android.devicelock/javalib/framework-devicelock.jar:/apex/com.android.healthfitness/javalib/framework-healthfitness.jar:/apex/com.android.ipsec/javalib/android.net.ipsec.ike.jar:/apex/com.android.media/javalib/updatable-media.jar:/apex/com.android.mediaprovider/javalib/framework-mediaprovider.jar:/apex/com.android.ondevicepersonalization/javalib/framework-ondevicepersonalization.jar:/apex/com.android.os.statsd/javalib/framework-statsd.jar:/apex/com.android.permission/javalib/framework-permission.jar:/apex/com.android.permission/javalib/framework-permission-s.jar:/apex/com.android.scheduling/javalib/framework-scheduling.jar:/apex/com.android.sdkext/javalib/framework-sdkextensions.jar:/apex/com.android.tethering/javalib/framework-connectivity.jar:/apex/com.android.tethering/javalib/framework-connectivity-t.jar:/apex/com.android.tethering/javalib/framework-tethering.jar:/apex/com.android.uwb/javalib/framework-uwb.jar:/apex/com.android.virt/javalib/framework-virtualization.jar:/apex/com.android.wifi/javalib/framework-wifi.jar: inaccessible or not found
看看是否包含上面设置的boot_jar.jar的这个包
确定有了之后在进入下一步进行app层面调用,注意这里调用又分为系统应用和普通第三方应用:
1、系统应用的情况,这种最为简单,因为不需要考虑相关权限访问的问题
路径packages/apps/Launcher3:
修改patch如下
diff --git a/Android.bp b/Android.bp
index a7edf2a9b9..1be5c49586 100644
--- a/Android.bp
+++ b/Android.bp
@@ -295,6 +295,7 @@ android_library {resource_dirs: [],libs: ["framework-statsd",
+ "boot_jar",],// Note the ordering here is important when it comes to resource// overriding. We want the most specific resource overrides defined
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 66690431ec..a0e0fe6eb4 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -591,7 +591,6 @@ public class Launcher extends StatefulActivity<LauncherState>setTitle(R.string.home_screen);mStartupLatencyLogger.logEnd(LAUNCHER_LATENCY_STARTUP_ACTIVITY_ON_CREATE);}
-/*** Create {@link StartupLatencyLogger} that only collects launcher startup latency metrics* without sending them anywhere. Child class can override this method to create logger
@@ -2120,6 +2119,8 @@ public class Launcher extends StatefulActivity<LauncherState>mTouchInProgress = false;break;}
+ android.util.Log.i("lsm","Launcher dispatchTouchEvent event = " + ev);
+ new com.hello.BootTest().testBootJar();TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);return super.dispatchTouchEvent(ev);}
(END)
这里只要触摸了Launcher就会有相关的打印如下
03-27 23:33:16.197 1216 1216 I lsm : testBootJar --- realPart
03-27 23:33:16.339 1216 1216 I lsm : Launcher dispatchTouchEvent event = MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=836.9385, y[0]=1386.8682, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=315711, downTime=315567, deviceId=4, source=0xd002, displayId=0, eventId=973480578 }
03-27 23:33:16.340 1216 1216 I lsm : testBootJar --- realPart
2、第三方应用情况
这种相比系统应用主要会多一个访问系统hidden api的权限问题,这里的话就可以参考另一个文章:
https://blog.csdn.net/learnframework/article/details/124440160
本文章更多详细代码和资料需要购买课程获取
hal+perfetto+surfaceflinger
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
私聊作者+v(androidframework007)
其他课程七件套专题:
点击这里
https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw
视频试看:
https://www.bilibili.com/video/BV1wc41117L4/
这篇关于手把手教你aosp13/14上实现PRODUCT_BOOT_JARS/boot jar(有源码)-千里马framework的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!