【Android 12 AOSP学习】Android内置第三方apk到系统
这篇文章将介绍如何在Android系统中内置第三方apk
一、准备工作
- 编译好的Android源码
- Android apk文件:也就是安卓应用安装包
二、步骤
(1)在源码packages/apps
目录下以我们需要内置的APK名字来创建一个新的文件夹
以 qqmusic.apk 为例,在Android_12_AOSP/packages/apps
目录下新建qqmusic文件夹
cd Android_12_AOSP/packages/apps/mkdir qqmusic
(2)将 qqmusic.apk 放入建好的文件夹中,在文件夹中创建 Android.mk 文件
cd Android_12_AOSP/packages/apps/qqmusic/touch Android.mk
(3)编辑 Android.mk 文件内容,内容如下
LOCAL_PATH:=$(call my-dir)include $(CLEAR_VARS)LOCAL_class="lazy" data-src_FILES := CarSkin.apkLOCAL_MODULE_CLASS := APPS#可以为user、eng、tests、optional,optional代表在任何版本下都编译LOCAL_MODULE_TAGS := optional#编译模块的名称LOCAL_MODULE := CarSkin#可以为testkey、platform、shared、media、PRESIGNED(使用原签名),platform代表为系统应用LOCAL_CERTIFICATE := PRESIGNED#不设置或者设置为false,安装位置为system/app,如果设置为true,则安装位置为system/priv-app?LOCAL_PRIVILEGED_MODULE := false#module的后缀,可不设置LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)# 关闭预编译,不会生成OAT文件LOCAL_DEX_PREOPT := trueLOCAL_PRODUCT_MODULE := trueinclude $(BUILD_PREBUILT)
或
# TestLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := qqmusicLOCAL_MODULE_CLASS := APPS#可以为user、eng、tests、optional,optional代表在任何版本下都编译LOCAL_MODULE_TAGS := optionalLOCAL_BUILT_MODULE_STEM := package.apkLOCAL_DEX_PREOPT := nostrippingLOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)#不设置或者设置为false,安装位置为system/app,如果设置为true,则安装位置为system/priv-appLOCAL_PRIVILEGED_MODULE := true#可以为testkey、platform、shared、media、PRESIGNED(使用原签名),platform代表为系统应用LOCAL_CERTIFICATE := platform#覆盖安装#LOCAL_OVERRIDES_PACKAGES := DeviceTest RKDeviceTestLOCAL_class="lazy" data-src_FILES := $(LOCAL_MODULE).apkLOCAL_MULTILIB := 32#LOCAL_REQUIRED_MODULES :=#JNI_LIBS :=#$(foreach FILE,$(shell find $(LOCAL_PATH)/lib/ -name *.so), $(eval JNI_LIBS += $(FILE)))#LOCAL_PREBUILT_JNI_LIBS := $(subst $(LOCAL_PATH),,$(JNI_LIBS))include $(BUILD_PREBUILT)
预装带源码的APP到系统
Android.bp文件
// Android依赖导入android_library_import { name: "com.google.android.material_material", aars: ["material-1.6.0.aar"], sdk_version: "current", min_sdk_version: "14", static_libs: [ "androidx.annotation_annotation", "androidx.appcompat_appcompat", "androidx.cardview_cardview", "androidx.coordinatorlayout_coordinatorlayout", "androidx-constraintlayout_constraintlayout", "androidx.core_core", "androidx.dynamicanimation_dynamicanimation", "androidx.annotation_annotation-experimental", "androidx.fragment_fragment", "androidx.lifecycle_lifecycle-runtime", "androidx.recyclerview_recyclerview", "androidx.transition_transition", "androidx.vectordrawable_vectordrawable", "androidx.viewpager2_viewpager2", "androidx.drawerlayout_drawerlayout", ],}//// Build rule for app.//android_app { //name: "DebuggingRestrictionController", //class="lazy" data-srcs: ["app/class="lazy" data-src/main/java*.java", "soong/BuildConfig.java", "soong/FirebaseApplication.java"], //resource_dirs: ["app/class="lazy" data-src/main/res"], //manifest: "soong/AndroidManifest.xml", //sdk_version: "system_current", //certificate: "platform", //privileged: true, //optimize: { // enabled: true, // proguard_flags_files: ["app/proguard-rules.pro"], //}, name: "CarSkin", //使用以下构建标记向 product 分区中安装模块。 product_specific: true, //导入 class="lazy" data-src 文件内容 class="lazy" data-srcs: [ "class="lazy" data-src*.java" ], //资源导入模块,如果aar包中带资源文件,需要将aar包解压拷贝资源文件,在源码根目录下新建一个 res-lottie 文件夹,将资源文件拷入此目录 //resource_dirs: [ // "res-lottie", //], libs: [ "res-lottie" ], //libs: [], sdk_version: "current", min_sdk_version: "14", target_sdk_version: "current", //platform_apis:true, certificate: "platform", //privileged: true, //system_ext_specific: true, //导入manifest文件 manifest: "app/class="lazy" data-src/main/AndroidManifest.xml", //添加manifest文件模块 //additional_manifests: [ // "AndroidManifest-common.xml", //], //导入静态库 static_libs: [ "com.google.android.material_material" ], //apptflags: [ // "--extra-packages", // "com.google.android.material" //导入aar包包名,用于生成 R 文件 //],}
(4)下面我们需要将将创建的qq音乐编译模块加入到编译系统,我们找到 /Android_12_AOSP/build/make/target/product/handheld_product.mk
文件,在文件下面这个位置加入qqmusic
PRODUCT_PACKAGES += \ apps \ qqmusic
进入源码根目录,重新编译
cd Android_12_AOSP/source build/envsetup.sh # 导入环境lunch 12 # 选择系统make -j6 # 编译make snod # 快速打包生成最新的system.img文件
问题记录:
注意:每次内置应用或修改mk文件时,一定要重新编译 system.img 然后在启动模拟器
错误1
FAILED: out/target/common/obj/APPS/qqmusic_intermediates/enforce_uses_libraries.status/bin/bash -c "(rm -f out/target/common/obj/APPS/qqmusic_intermediates/enforce_uses_libraries.status ) && (build/soong/scripts/manifest_check.py --enforce-uses-libraries --enforce-uses-libraries-status out/target/common/obj/APPS/qqmusic_intermediates/enforce_uses_libraries.status --aapt out/host/linux-x86/bin/aapt packages/apps/qqmusic/qqmusic.apk )"error: mismatch in the tags between the build system and the manifest: - required libraries in build system: [] vs. in the manifest: [] - optional libraries in build system: [] vs. in the manifest: [org.apache.http.legacy] - tags in the manifest (packages/apps/qqmusic/qqmusic.apk): uses-library-not-required:'org.apache.http.legacy'note: the following options are available: - to temporarily disable the check on command line, rebuild with RELAX_USES_LIBRARY_CHECK=true (this will set compiler filter "verify" and disable AOT-compilation in dexpreopt) - to temporarily disable the check for the whole product, set PRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true in the product makefiles - to fix the check, make build system properties coherent with the manifest - see build/make/Changes.md for details12:19:02 ninja failed with: exit status 1
解决方法
修改Android.mk,添加LOCAL_ENFORCE_USES_LIBRARIES := false
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)# Module name should match apk name to be installedLOCAL_MODULE := SogouInputLOCAL_MODULE_TAGS := optionalLOCAL_class="lazy" data-src_FILES := $(LOCAL_MODULE).apkLOCAL_MODULE_CLASS := APPSLOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)LOCAL_CERTIFICATE := PRESIGNEDLOCAL_ENFORCE_USES_LIBRARIES := falseinclude $(BUILD_PREBUILT)
错误2
输入命令报错
adb install -r /home/lai/下载/SogouInput_v11.7_Build_40e311fcaf0_20220714_android_sweb.apk
adb: failed to install /home/lai/下载/SogouInput_v11.7_Build_40e311fcaf0_20220714_android_sweb.apk: Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113]
问题当前的app使用的native libraries与模拟器的CPU架构不一致所导致的
错误3
[100% 1/1] Creating out/target/product/generic_car_x86/VerifiedBootParams.textprotoFAILED: out/target/product/generic_car_x86/VerifiedBootParams.textproto/bin/bash -c "(export AVBTOOL=out/host/linux-x86/bin/avbtool; device/generic/goldfish/tools/mk_vbmeta_boot_params.sh out/target/product/generic_car_x86/vbmeta.img out/target/product/generic_car_x86/system.img out/target/product/generic_car_x86/VerifiedBootParams.textproto)"/home/lai/Android_12_AOSP/out/host/linux-x86/bin/avbtool: Given image does not look like a vbmeta image./home/lai/Android_12_AOSP/out/host/linux-x86/bin/avbtool: Given image does not look like a vbmeta image.expr: 语法错误:“+” 后缺少参数ninja: build stopped: subcommand failed.14:25:23 ninja failed with: exit status 1#### failed to build some targets (04:34 (mm:ss)) ####
错误4
问题:系统卡在开机动画进不去
原因:内置一个apk时,因为软件签名问题,导致系统服务启动不了
解决方法:将软件删掉重新编译就行
过程中常用的命令
通过adb安装应用
adb install -r /home/lai/下载/SogouInput_v11.7_Build_40e311fcaf0_20220714_android_sweb.apk
通过adb查看安装的应用包名
adb shell pm list packages #只显示应用包名 adb shell pm list packages -f #显示应用包名及路径adb shell dumpsys package > ./package.txt
adb shell am switch-user 10adb shell pm list users
执行make clean
将会会删除out/目录下所有内容。
make clean
执行make clobber
效果等同于make clean
,也是删除out/目录下所有内容。
make clobber
执行make installclean
将会删除installclean_files
,installclean_files
make installclean
查询手机cpu命令行:
adb shell getprop ro.product.cpu.abi
开机日志的获取:
adb logcat -b main -b system -b radio -b events -v time > bootLog.log
adb 日志获取
adb logcat
通过关键字查找文件所在位置
grep "Calendar" ./ -rngrep "Contacts" ./ -rn
自定义内置apk流程
(1)在源码根目录 /vendor
下新建一个test
文件夹
mkdir test
(2)在 test
文件目录下新建一个 test.mk
文件
touch test.mk
(3)在源码test
目录下以我们需要内置的APK名字来创建一个新的文件夹
- 以 ABC.apk为例,在
test
目录下新建ABC文件夹
mkdir ABC
(4)将 ABC.apk 放入ABC
文件夹中,在文件夹ABC
中创建 Android.mk 文件
touch Android.mk
Android.mk
文件的内容如下:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := ABCLOCAL_MODULE_TAGS := optionalLOCAL_class="lazy" data-src_FILES := ABC.apkLOCAL_MODULE_CLASS := APPSLOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)LOCAL_PRIVILEGED_MODULE := falseLOCAL_CERTIFICATE := platforminclude $(BUILD_PREBUILT)
(5)打开我们建好的 test.mk
文件,在里面加入以下内容,将ABC模块加入到编译系统
PRODUCT_PACKAGES += \ ABC \ user
(6)将我们建的 test.mk
加入到编译环境中,这样我们后面需要新加入模块就能直接在test.mk里配置了
我们在源码根目录下找到 device/rockchip/rk3588/device.mk
文件
注:这个路径以及上面的
/vendor
目录一般是项目中存放内置apk的路径,不同项目路径可能不同,根据自己实际路径来选择,一般都是在源码根目录的/device
目录下
将下面内容加入到文件如下对应位置
#$(call inherit-product-if-exists, vendor/rockchip/common/npu/npu.mk)$(call inherit-product-if-exists, vendor/test/test.mk) # 我们配置的文件,加入进来$(call inherit-product-if-exists, vendor/firefly/firefly.mk)
(7)最后我们进入源码根目录重新编译源码
进入源码根目录,重新编译
source build/envsetup.sh # 导入环境lunch # 选择系统,选择自己的系统make installclean # 清除out目录下对应板文件夹中的内容make -j6 # 编译,如果这一步生成了最新的镜像文件,那不需要进行下一步了make snod # 快速打包生成最新的system.img文件
后面我们如果需要继续添加新apk,只需要重复(3)(4)(5)(7)步骤就行
来源地址:https://blog.csdn.net/etrospect/article/details/128235015
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341