背景:
原文参考:系统开发必会技巧:framework层面依赖第三方静态jar
在framework开发中经常可能会有一些需求需要依赖一些新加入的代码,但是一些代码可能是第三方厂商提供,不一定会提供对应的代码,只能提供对应的jar包方式,所以就有需求对让framework依赖一些静态jar包方式。

这个应该算比较常见简单framework相关的技能要求,不过在实现过程中也有遇到一些坑,所以这里记录一下方便各位学员后续直接跟着马哥就不用再走弯路。
实战过程:
环境:aosp15 源码环境
1、准备好对应的第三方jar包
这里大家可以自行准备好一个jar包库,这里我们采用android studio编译一个jar包,具体编译步骤如下:
在File --> New --> New Module,然后显示如下

创建好Module后,再添加对应的代码
package com.example.mylibrary;
import android.util.Log;
public class AudioDemoUtil {
public static void testAudio() {
Log.i("lsm888","AudioDemoUtil testAudio");
}
}
在进行编译出对应的jar包,编译就是普通的编译apk既可以,然后会在如下目录生成对应的jar包,对它可以进行文件重命名既可以。

2、把jar放到系统目录定义到Android.bp
重点在看看这个bp
frameworks/base/libs/ssk/Android.bp
//
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
// SPDX-license-identifier-GPL-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
java_import {
name: "ssk-jar",
host_supported: false, // 通常框架库不需要在主机上运行
installable: false, // 预编译JAR通常不可安装
jars: ["bootloader.jar"],
}
3、在framework的代码中使用jar包对应的类
这里在Activity的onCreate中加入这个jar包的方法,这样执行成功就有对应log打印
+import com.example.mylibrary.AudioDemoUtil;
import static java.lang.Character.MIN_VALUE;
import android.annotation.AnimRes;
@@ -1816,7 +1815,7 @@ public class Activity extends ContextThemeWrapper
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
-
+ AudioDemoUtil.testAudio();
if (mLastNonConfigurationInstances != null) {
mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
4、framework对应Android.bp加入对jar的依赖
因为目的就是让framework的代码可以依赖这个jar包,所以肯定要让framework编译时候对这个有依赖
java_defaults {
name: "framework-minus-apex-defaults",
//...省略
static_libs: [
//...省略
"ssk-jar", //新添加的jar库
5、解决相关的编译报错
在前面四步完成后,在进行编译后发现有如下
“whose package n
ame “com.example.mylibrary” is empty ”
的报错,具体完整提示如下:
f input list: 3b493c8bee4d5f08705b797df09bc75b8c36603bc2fbff69aa385f417db33ba9
Error: out/soong/.intermediates/frameworks/base/framework-minus-apex/android_common/7bd916565615329d50364be05485f3c9/aligned/framework.jar contains class file com.example.mylibrary.AudioDemoUtil, whose package n
ame "com.example.mylibrary" is empty or not in the allow list build/soong/scripts/check_boot_jars/package_allowed_list.txt of packages allowed on the bootclasspath.
21:08:23 ninja failed with: exit status 1
这里比较好是编译报错同时也提醒我们怎么修改了,大概提示我们需要在build/soong/scripts/check_boot_jars/package_allowed_list.txt文件中添加相关包名。
test@test:/media/test/49a0eb6b-f410-4eed-9e0a-952e3c75d2b2/home/test/aosp15/build/soong/scripts$ git diff
diff --git a/scripts/check_boot_jars/package_allowed_list.txt b/scripts/check_boot_jars/package_allowed_list.txt
index 47eae0769..21a6d10f1 100644
--- a/scripts/check_boot_jars/package_allowed_list.txt
+++ b/scripts/check_boot_jars/package_allowed_list.txt
@@ -253,8 +253,10 @@ com\.google\.android\..*
com\.google\.vr\.platform.*
com\.google\.i18n\.phonenumbers\..*
com\.google\.i18n\.phonenumbers
-
###################################################
# Packages used for Android in Chrome OS
org\.chromium\.arc
org\.chromium\.arc\..*
+com\.example\.mylibrary
+com\.example\.mylibrary\..*
添加后再进行编译就可以正常编译成功了

运行结果:
每次操作打开一个Activity让执行onCreate相关方法,就会有如下打印:

更多framework实战开发干货,请关注下面“千里马学框架”
4817

被折叠的 条评论
为什么被折叠?



