当前位置:   article > 正文

android 系统 裁剪APK和SO库_裁剪过的so

裁剪过的so

android 系统 裁剪APK和SO库

Android 系统的裁剪

接触android Framework 层开发没多久,最近学习了一下系统的裁剪的工作,一般系统裁剪分为 APK、so、jar 库和服务的裁剪,目前只学习了APK和so库的裁剪,这里做一下学习记录

APK裁剪

因为安卓系统里内置了很多的APK,包括一些平台也会内置一些APK在系统里面,有些不需要的我们就可以把它不编译到系统里来,至于那些APK可以不用,这个可以根据自己项目的需求来,刚开始学的可以用adb 在system/app 或者 system/priv-app 目录下找

  1. 想要把app编译到系统,都需要在.mk文件中添加,而PRODUCT_PACKAGES 就是添加APK的变量,我用RK自己的一个APP(RKUpdateService)做为例子,先找到APK在哪里添加的,因为RK的应用都在vendor/rockchip/common/apps/目录下,可以使用grep -nr “RKUpdateService” vendor/rockchip/common/apps/ 搜索:
    搜索结果
    在app.mk就可以看到添加APK的代码
    在这里插入图片描述
    正常来说把RKUpdateService 这行注释掉就可以完成裁剪的操作,但是不好统一管理,如果删除了多个需要恢复的时候又得一个一个去找很不方便,在网上参考了这篇文章,把APK进行了统一的管理

  2. 首先先找到系统在哪里添加所有的APK,使用grep 搜索 PRODUCT_PACKAGES 在build/core/main.mk文件中

ifdef FULL_BUILD
# The base list of modules to build for this product is specified
# by the appropriate product definition file, which was included
# by product_config.mk.
product_MODULES := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES)
# Filter out the overridden packages before doing expansion
product_MODULES := $(filter-out $(foreach p, $(product_MODULES), \
    $(PACKAGES.$(p).OVERRIDES)), $(product_MODULES))
# Filter out executables as well
product_MODULES := $(filter-out $(foreach m, $(product_MODULES), \
    $(EXECUTABLES.$(m).OVERRIDES)), $(product_MODULES))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里把所有的使用PRODUCT_PACKAGES 添加的APK 都找统一找出来,可以根据这个添加一个宏,把要删除的APK统一起来

  1. 新建一个.mk文件,在项目目录下新建了一个evb_del.mk文件
    在这里插入图片描述
    如果是新建的需要添加到编译文件中,不然不会编译,可以随便找一个能编译的.mk文件,我在项目目录下的.mk文件中添加:

在这里插入图片描述

然后在build/core/product.mk文件中添加PRODUCT_DEL_PACKAGES宏
在这里插入图片描述
在新建的evb_del.mk文件中把要删除的APK添加进来
在这里插入图片描述
最后需要在main.mk把PRODUCT_DEL_PACKAGES的APK移除出去

ifdef FULL_BUILD
  # The base list of modules to build for this product is specified
  # by the appropriate product definition file, which was included
  # by product_config.mk.
  product_MODULES := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES)
  # Filter out the overridden packages before doing expansion
  product_MODULES := $(filter-out $(foreach p, $(product_MODULES), \
      $(PACKAGES.$(p).OVERRIDES)), $(product_MODULES))
  # Filter out executables as well
  product_MODULES := $(filter-out $(foreach m, $(product_MODULES), \
      $(EXECUTABLES.$(m).OVERRIDES)), $(product_MODULES))

  #裁剪app----------------------------------
  product_sub_MODULES := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_DEL_PACKAGES)
  product_MODULES := $(filter-out $(foreach p, $(product_sub_MODULES), \
      $(p)), $(product_MODULES))
  #---------------------------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

最后这样编译后再到system/app目录下应该已经没有上面的APK了

SO库的裁剪

  1. so库的裁剪基本跟apk的裁剪逻辑类似,需要注意的是系统添加so库的代码在build/core/Makefile文件中
//在这里把使用 PRODUCT_COPY_FILES so库统一添加 
unique_product_copy_files_pairs :=
$(foreach cf,$(PRODUCT_COPY_FILES), \
    $(if $(filter $(unique_product_copy_files_pairs),$(cf)),,\
        $(eval unique_product_copy_files_pairs += $(cf))))

unique_product_copy_files_destinations :=
$(foreach cf,$(unique_product_copy_files_pairs), \
    $(eval _src := $(call word-colon,1,$(cf))) \
    $(eval _dest := $(call word-colon,2,$(cf))) \
    $(call check-product-copy-files,$(cf)) \
    $(if $(filter $(unique_product_copy_files_destinations),$(_dest)), \
        $(info PRODUCT_COPY_FILES $(cf) ignored.), \
        $(eval _fulldest := $(call append-path,$(PRODUCT_OUT),$(_dest))) \
        $(if $(filter %.xml,$(_dest)),\
            $(eval $(call copy-xml-file-checked,$(_src),$(_fulldest))),\
            $(eval $(call copy-one-file,$(_src),$(_fulldest)))) \
        $(eval ALL_DEFAULT_INSTALLED_MODULES += $(_fulldest)) \
        $(eval unique_product_copy_files_destinations += $(_dest))))

#$(warning , $(unique_product_copy_files_destinations))

unique_product_copy_files_pairs :=
unique_product_copy_files_destinations :=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

后续的逻辑和APK的一样

  1. 在build/core/product.mk添加,PRODUCT_DEL_SO宏
    在这里插入图片描述

  2. 在前面新建的evb_del.mk文件中添加so库

PRODUCT_DEL_SO += \
   vendor/rockchip/common/apps/RKUpdateService/lib/arm/librockchip_update_jni.so:system/lib/librockchip_update_jni.so
  • 1
  • 2
  1. 在Makefile文件中把需要裁剪的so库移除出去

#获取要删除的so库------------------------------
unique_product_dle_files_pairs := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_DEL_SO)
#$(warning , "unique_product_dle_files_pairs Build")
#$(warning , $(unique_product_dle_files_pairs))
#--------------------------------------------

unique_product_copy_files_pairs :=
$(foreach cf,$(PRODUCT_COPY_FILES), \
    $(if $(filter $(unique_product_copy_files_pairs),$(cf)),,\
        $(eval unique_product_copy_files_pairs += $(cf))))
#过滤 so库 PRODUCT_DEL_SO ----------------------------
unique_product_copy_files_pairs := $(filter-out $(foreach p, $(unique_product_dle_files_pairs), \
      $(p)), $(unique_product_copy_files_pairs))

#$(warning , $(unique_product_copy_files_pairs))
#--------------------------------------------------
unique_product_copy_files_destinations :=
$(foreach cf,$(unique_product_copy_files_pairs), \
    $(eval _src := $(call word-colon,1,$(cf))) \
    $(eval _dest := $(call word-colon,2,$(cf))) \
    $(call check-product-copy-files,$(cf)) \
    $(if $(filter $(unique_product_copy_files_destinations),$(_dest)), \
        $(info PRODUCT_COPY_FILES $(cf) ignored.), \
        $(eval _fulldest := $(call append-path,$(PRODUCT_OUT),$(_dest))) \
        $(if $(filter %.xml,$(_dest)),\
            $(eval $(call copy-xml-file-checked,$(_src),$(_fulldest))),\
            $(eval $(call copy-one-file,$(_src),$(_fulldest)))) \
        $(eval ALL_DEFAULT_INSTALLED_MODULES += $(_fulldest)) \
        $(eval unique_product_copy_files_destinations += $(_dest))))

#$(warning , $(unique_product_copy_files_destinations))

unique_product_copy_files_pairs :=
unique_product_copy_files_destinations :=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

主要的修改都标识出来了,需要注意的是,有的so库在lib和lib64都有添加,所以要裁剪的时候要把两个都添加进来,不然在lib删了,lib64还是会有
在这里插入图片描述
在evb_del.mk中把lib64的也添加进来
在这里插入图片描述
只是个人学习记录,亲测是可以完成裁剪的操作

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号