玩转Android10源码开发定制(八)内置Apk到系统

1.Android.mk文件说明

 

       Android.mk是Android提供的一个makefile文件,可以将源文件分组为模块。用来引用的头文件目录、需要编译的*.c/*.cpp文件、jni源文件、指定编译生成*.so共享库文件或者*.a静态库文件,可以定义一个或多个模块,也可以多个模块中使用同一个源文件。

      为方便模块编译,编译系统设置了很多模块描述环境变量和宏定义,如下列举一些常用的。

   模块描述环境变量:

  • LOCAL_SRC_FILES:

               当前模块包含的源文件;

  • LOCAL_MODULE:

                 当前模块的名称;

  • LOCAL_PACKAGE_NAME

                 当前APK应用的名称;

  • LOCAL_C_INCLUDES:

                   C/C++所需的头文件路径;

  • LOCAL_STATIC_LIBRARIES:

                   当前模块在静态链接时需要的静态链接库名;

  • LOCAL_SHARED_LIBRARIES:

                   当前模块在运行时依赖的动态链接库名;

  • LOCAL_STATIC_JAVA_LIBRARIES:

                    当前模块依赖的Java静态库;

  • LOCAL_JAVA_LIBRARIES:

                   当前模块依赖的Java共享库;

  • LOCAL_CERTIFICATE:

                   当前应用的签名方式,比如使用系统签名platform。

  • LOCAL_MODULE_TAGS:

           表示该模块编译的标识,可能值为eng,user,tests或optional(默认值)。值具体表示:

               user:指该模块只在user版本下才编译 

              eng:指该模块只在eng版本下才编译

              tests:指该模块只在tests版本下才编译

              optional:指该模块在所有版本下都编译

   

  定义的 include 变量:

  •    include $(BUILD_SHARED_LIBRARY) 

           表示需要编译为so动态链接库。

  •    include $(BUILD_JAVA_LIBRARY) 

           表示需要编译为java动态库。

  •    include $(BUILD_JAVA_STATIC_LIBRARY) 

           表示需要编译为java静态库。

  •    include $(BUILD_STATIC_LIBRARY )

         表示需要编译为so静态库。 

  • include $(BUILD_EXECUTABLE)

         表示编译为Native C可执行程序  

  • include $(BUILD_PREBUILT)

             表示源文件已经是编译好的,只需要进行复制操作

    

 

     定义的函数宏,原型为 $(call ),以下列举几个:

  • $(call my-dir)

              获取当前文件夹路径;

  • $(call all-java-files-under, )

           获取指定目录下的所有Java文件;

  • $(call all-c-files-under,)

            获取指定目录下的所有C文件;

  • $(call all-Iaidl-files-under, )

            获取指定目录下的所有AIDL文件;

     

       2.提供几个Android.mk模板​​​​​​​

#编译so静态库    LOCAL_PATH := $(call my-dir)    include $(CLEAR_VARS)    LOCAL_MODULE = libtest LOCAL_SRC_FILES = testxxx.c    LOCAL_C_INCLUDES = $(INCLUDES)    LOCAL_SHARED_LIBRARIES := libcutils      include $(BUILD_STATIC_LIBRARY)        #编译为so动态库链接库    LOCAL_PATH := $(call my-dir)    include $(CLEAR_VARS)    LOCAL_MODULE = libtest  LOCAL_SRC_FILES = testxx.c    LOCAL_C_INCLUDES = $(INCLUDES)    LOCAL_SHARED_LIBRARIES := libcutils       include $(BUILD_SHARED_LIBRARY)      #编译java动态库LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_MODULE := testjarinclude $(BUILD_JAVA_LIBRARY)
#编译java静态库LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_MODULE := testjarinclude $(BUILD_STATIC_JAVA_LIBRARY)       #编译为可执行文件  LOCAL_PATH := $(call my-dir)    include $(CLEAR_VARS)    LOCAL_MODULE := hellos    LOCAL_STATIC_LIBRARIES := libhellos    LOCAL_SHARED_LIBRARIES :=    LOCAL_LDLIBS += -ldl    LOCAL_CFLAGS := $(L_CFLAGS)    LOCAL_SRC_FILES := mains.c    LOCAL_C_INCLUDES := $(INCLUDES)    include $(BUILD_EXECUTABLE)        #使用动态库    LOCAL_PATH := $(call my-dir)    include $(CLEAR_VARS)    LOCAL_MODULE := hellod    LOCAL_MODULE_TAGS := debug    LOCAL_SHARED_LIBRARIES := libc libcutils libhellod    LOCAL_LDLIBS += -ldl    LOCAL_CFLAGS := $(L_CFLAGS)    LOCAL_SRC_FILES := maind.c    LOCAL_C_INCLUDES := $(INCLUDES)    include $(BUILD_EXECUTABLE)       #预编so动态链接库到指定目录LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  LOCAL_MODULE := libtest.so  LOCAL_MODULE_CLASS := SHARED_LIBRARIES  LOCAL_MODULE_PATH := $(ANDROID_OUT_SHARED_LIBRARIES)  LOCAL_SRC_FILES := lib/$(LOCAL_MODULE )  OVERRIDE_BUILD_MODULE_PATH := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)  include $(BUILD_PREBUILT)  
#预编二进制文件到系统include $(CLEAR_VARS)LOCAL_MODULE := testbinLOCAL_SRC_FILES := testbinLOCAL_MODULE_CLASS := EXECUTABLESLOCAL_MODULE_TAGS := optionalinclude $(BUILD_PREBUILT)# 内置Apk文件到系统LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := adbinputmethodLOCAL_MODULE_CLASS := APPSLOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)LOCAL_MODULE_TAGS := optionalLOCAL_PRIVILEGED_MODULE := trueLOCAL_BUILT_MODULE_STEM := package.apkLOCAL_SRC_FILES := $(LOCAL_MODULE).apk# LOCAL_CERTIFICATE := platformLOCAL_CERTIFICATE := PRESIGNEDinclude $(BUILD_PREBUILT)

3.源码中内置apk到系统

      此处以内置搜狗输入法到系统为例说明。

    (1). 创建存放内置App的目录,此处我创建为packages/myapps/sougou,如下所示:​​​​​​​

qiang@ubuntu:~/lineageOs$ pwd/home/qiang/lineageOsqiang@ubuntu:~/lineageOs$ mkdir -p packages/myapps/sougouqiang@ubuntu:~/lineageOs$ 

       (2).将搜狗输入法apk复制到packages/myapps/sougou,并创建内置apk到系统的Android.mk。如下所示:​​​​​​​

qiang@ubuntu:~/lineageOs/packages/myapps/sougou$ pwd/home/qiang/lineageOs/packages/myapps/sougouqiang@ubuntu:~/lineageOs/packages/myapps/sougou$ ls -latotal 58480drwxrwxrwx 2 qiang qiang     4096 1月   1 18:22 .drwxrwxr-x 3 qiang qiang     4096 1月   2 02:25 ..-rwxrw-rw- 1 qiang qiang      755 1月   1 18:29 Android.mk-rwxrw-rw- 1 qiang qiang 59870272 11月  3 07:35 sougou.apk

     对应的Android.mk内容为:​​​​​​​

# ///ADD START# ///ADD END# 设置当前工作路径LOCAL_PATH:= $(call my-dir)# 清除变量值include $(CLEAR_VARS)# 生成的模块名称LOCAL_MODULE := sougou# 生成的模块类型LOCAL_MODULE_CLASS := APPS# 生成的模块后缀名,此处为apkLOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)# 设置模块tag,tags取值可以为:user debug eng tests optional# optional表示全平台编译LOCAL_MODULE_TAGS := optional# LOCAL_PRIVILEGED_MODULE := trueLOCAL_BUILT_MODULE_STEM := package.apk# 设置源文件LOCAL_SRC_FILES := $(LOCAL_MODULE).apk# LOCAL_CERTIFICATE := platform# 设置签名,此处表示保持apk原有签名LOCAL_CERTIFICATE := PRESIGNED# 此处表示预编译方式include $(BUILD_PREBUILT)

    (3)将创建的搜狗输入法编译模块配置到编译系统,这个非常重要,如果不加入编译镜像默认是不会编译刚添加的模块。lineageOs中,可以在如下路径中添加Settings模块的地方追加sougou。

文件位置:

build/make/target/product/handheld_product.mk

        添加sougou内容之后的部分内容如下:

PRODUCT_PACKAGES += \    Browser2 \    Calendar \    Camera2 \    Contacts \    DeskClock \    Gallery2 \    Launcher3QuickStep \    Music \    OneTimeInitializer \    Provision \    QuickSearchBox \    Settings \    SettingsIntelligence \    StorageManager \    SystemUI \    WallpaperCropper \    frameworks-base-overlays \    sougou

   (4)编译刷机​​​​​​​

qiang@ubuntu:~/lineageOs/build/make/target/product$ crootqiang@ubuntu:~/lineageOs$ breakfast oneplus3Looking for dependencies in device/oneplus/oneplus3Looking for dependencies in device/oppo/commondevice/oppo/common has no additional dependencies.Looking for dependencies in kernel/oneplus/msm8996kernel/oneplus/msm8996 has no additional dependencies.============================================PLATFORM_VERSION_CODENAME=RELPLATFORM_VERSION=10LINEAGE_VERSION=17.1-20210102-UNOFFICIAL-oneplus3TARGET_PRODUCT=lineage_oneplus3TARGET_BUILD_VARIANT=userdebugTARGET_BUILD_TYPE=releaseTARGET_ARCH=arm64TARGET_ARCH_VARIANT=armv8-aTARGET_CPU_VARIANT=kryoTARGET_2ND_ARCH=armTARGET_2ND_ARCH_VARIANT=armv8-aTARGET_2ND_CPU_VARIANT=kryoHOST_ARCH=x86_64HOST_2ND_ARCH=x86HOST_OS=linuxHOST_OS_EXTRA=Linux-5.4.0-58-generic-x86_64-Ubuntu-20.04.1-LTSHOST_CROSS_OS=windowsHOST_CROSS_ARCH=x86HOST_CROSS_2ND_ARCH=x86_64HOST_BUILD_TYPE=releaseBUILD_ID=QQ3A.200805.001OUT_DIR=outPRODUCT_SOONG_NAMESPACES=vendor/oneplus/oneplus3 device/oneplus/oneplus3 vendor/nxp/opensource/pn5xx hardware/qcom-caf/msm8996============================================qiang@ubuntu:~/lineageOs$ brunch oneplus3Looking for dependencies in device/oneplus/oneplus3Looking for dependencies in device/oppo/commondevice/oppo/common has no additional dependencies.Looking for dependencies in kernel/oneplus/msm8996kernel/oneplus/msm8996 has no additional dependencies.

4.效果展示

   

玩转Android10源码开发定制(八)内置Apk到系统_第1张图片

玩转Android10源码开发定制(八)内置Apk到系统_第2张图片

 

 

关注公众号,及时获取更新内容:

图片

 

你可能感兴趣的:(安卓10系统源码开发定制,android,java,ndk,android,studio,flutter)