【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_SRC_FILES := CarSkin.apk
LOCAL_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 := true
LOCAL_PRODUCT_MODULE := true
include $(BUILD_PREBUILT)


# Test
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := qqmusic
LOCAL_MODULE_CLASS := APPS
#可以为user、eng、tests、optional,optional代表在任何版本下都编译
LOCAL_MODULE_TAGS := optional
LOCAL_BUILT_MODULE_STEM := package.apk
LOCAL_DEX_PREOPT := nostripping
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
#不设置或者设置为false,安装位置为system/app,如果设置为true,则安装位置为system/priv-app
LOCAL_PRIVILEGED_MODULE := true
#可以为testkey、platform、shared、media、PRESIGNED(使用原签名),platform代表为系统应用
LOCAL_CERTIFICATE :=  platform
#覆盖安装
#LOCAL_OVERRIDES_PACKAGES := DeviceTest RKDeviceTest
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_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",
    //srcs: ["app/src/main/java/**/*.java", "soong/BuildConfig.java", "soong/FirebaseApplication.java"],
    //resource_dirs: ["app/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,
    
    //导入 src 文件内容
    srcs: [
        "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/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_us
es_libraries.status ) && (build/soong/scripts/manifest_check.py     --enfo
rce-uses-libraries     --enforce-uses-libraries-status out/target/common/obj/
APPS/qqmusic_intermediates/enforce_uses_libraries.status     --aapt out/hos
t/linux-x86/bin/aapt                         packages/apps/qqmusic/
qqmusic.apk )"
error: mismatch in the <uses-library> 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_U
SES_LIBRARY_CHECK=true (this will set compiler filter "verify" and disable AOT-c
ompilation in dexpreopt)
  - to temporarily disable the check for the whole product, set PRODUCT_BR
OKEN_VERIFY_USES_LIBRARIES := true in the product makefiles
  - to fix the check, make build system properties coherent with the manif
est
  - see build/make/Changes.md for details

12: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 installed
LOCAL_MODULE := SogouInput
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED

LOCAL_ENFORCE_USES_LIBRARIES := false

include $(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.textproto
FAILED: 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 10
adb 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" ./ -rn
grep "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 := ABC
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := ABC.apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_PRIVILEGED_MODULE := false
LOCAL_CERTIFICATE := platform
include $(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)步骤就行

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>