android系统开发必会技巧:手把手教你framework层面依赖第三方静态jar

背景:

原文参考:系统开发必会技巧: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实战开发干货,请关注下面“千里马学框架”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值