Android11编译导入PRODUCT_BOOT_JARS

一曲冷凌霜 提交于 2020-12-05 15:50:38

Android P以下版本导入PRODUCT_BOOT_JARS

Android编译导入PRODUCT_BOOT_JARS

PRODUCT_PACKAGES += \
   com.king.test.module \
PRODUCT_BOOT_JARS += \
    com.king.test.module \

增加package白名单

源码文件build/make/core/tasks/check_boot_jars/package_allowed_list.txt增加jar包名白名单

###################################################
#e.g. com.king.test.module-dex.jar
com\.king\.test
com\.king\.test\..*

编写jar包Android.mk文件(此jar包为dex文件)

LOCAL_PATH := $(call my-dir)
#e.g. com.king.test.module-dex.jar 
include $(CLEAR_VARS)
LOCAL_MODULE := com.king.test.module
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := com.king.test.module-dex.jar
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
include $(BUILD_PREBUILT)

make编译

$make –j8

Android 11 版本导入PRODUCT_BOOT_JARS

编写jar包Android.bp文件(此jar包为class文件)

使用androidmk命令把Android.mk文件生成Android.bp

$androidmk Android.mk > Android.bp

生成后的文件如下:

java_import {
   
   
    name: "com.king.test.module",
    jars: ["com.king.test.module-class.jar"],
}

Android.bp增加java_library

如上Android.bp文件会编译报错:

internal error: failed to find a dex jar path for module ’ com.king.test.module’, note that some jars may be filtered out by module constraints

增加java_library定义,installable 为 true ,否则会同样错误,如下:

java_import {
   
   
    name: "com.king.test.module_jar",
    jars: ["com.king.test.module-class.jar"],
}
java_library {
   
   
    name: "com.king.test.module",
    static_libs: [
        " com.king.test.module_jar",
    ],
    installable: true,
}

jar包为dex文件编译运行出错处理

如果jar包是已经编译好的dex文件而不是纯class文件,在Android11上编译会有以下问题,处理方式可参见说明

check_boot_jars.py 检查出错

Error: out/target/common/obj/JAVA_LIBRARIES/ com.king.test.module_intermediates/classes.jar does not contain any class files.

解决方法: 修改build/make/core/tasks/check_boot_jars/check_boot_jars.py,注释"if classes == 0"的判断
在这里插入图片描述
也可java_library 增加空的java文件,如下

//    srcs: [
//        "src/**/*.java",
//    ],

不过此方法会后面编译出错:

Error: Cannot create android app from an archive ‘out/soong/.intermediates/vendor/king/ com.king.test.module/android_common/combined/com.king.test.module.jar’ containing both DEX and Java-bytecode content

merge_zips合并jar包出错

merge_zips.go:752: Duplicate path classes.dex found in out/soong/.intermediates/vendor/king/com.king.test.module/android_common/dex/classes.dex.jar!classes.dex and out/soong/.intermediates/vendor/king/com.king.test.module_jar/android_common/combined/com.king.test.module_jar.jar

解决方法: 修改build/soong/cmd/merge_zips/merge_zips.go,修改参数ignore-duplicates为true
在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!