裁剪应用
1. 修改 build/core/product.mk
添加PRODUCT_DEL_PACKAGES变量的声明
新增一行_product_single_value_vars += PRODUCT_DEL_PACKAGES
# The first API level this product shipped with
_product_single_value_vars += PRODUCT_SHIPPING_API_LEVEL
_product_single_value_vars += PRODUCT_DEL_PACKAGES # 新增
_product_list_vars += VENDOR_PRODUCT_RESTRICT_VENDOR_FILES
_product_list_vars += VENDOR_EXCEPTION_MODULES
2. 修改 build/core/main.mk
修改 product-installed-files 函数的定义,增加两行:
$(eval _pif_dels :=
(
c
a
l
l
g
e
t
−
p
r
o
d
u
c
t
−
v
a
r
,
(call get-product-var,
(callget−product−var,(1),PRODUCT_DEL_PACKAGES))
$(eval _pif_modules := $(filter-out $(_pif_dels), $(_pif_modules))) \
$(eval ### Filter out the overridden packages and executables before doing expansion) \
$(eval _pif_overrides := $(call module-overrides,$(_pif_modules))) \
$(eval _pif_modules := $(filter-out $(_pif_overrides), $(_pif_modules))) \
$(eval _pif_dels := $(call get-product-var,$(1),PRODUCT_DEL_PACKAGES)) \
$(eval _pif_modules := $(filter-out $(_pif_dels), $(_pif_modules))) \
$(eval ### Resolve the :32 :64 module name) \
3. 在相应的设备产品mk文件中添加用PRODUCT_DEL_PACKAGES属性来控制模块删除
一般编辑的文件路径为device/公司名/产品名/device.mk, 比如rk3399中:
device/rockchip/rk3399/device.mk
我用的模拟器,使用 lunch sdk_pc_x86_64-userdebug,文件位置为:
aosp/build/target/board/emulator_x86_64/device.mk
PRODUCT_SOONG_NAMESPACES += device/generic/goldfish # for libwifi-hal-emu
PRODUCT_SOONG_NAMESPACES += device/generic/goldfish-opengl # for goldfish deps.
ifdef NET_ETH0_STARTONBOOT
PRODUCT_VENDOR_PROPERTIES += net.eth0.startonboot=1
endif
# Ensure we package the BIOS files too.
PRODUCT_HOST_PACKAGES += \
bios.bin \
vgabios-cirrus.bin \
# 要裁剪的应用
PRODUCT_DEL_PACKAGES += \
messaging \
Dialer \
Contacts \
Email \
Camera2 \
Browser2 \
Calendar \
DocumentsUI \
Music \
MusicFX \
ExactCalculator \
4. 进行编译
source build/envsetup.sh
# lunch sdk_pc_x86_64-userdebug # 模拟器
lunch xxx-userdebug #xxx表示对应的产品名
make
裁剪Service,打印机服务
aosp/frameworks/base/services/java/com/android/server/SystemServer.java
1. SystemServer定义boolean变量,从全局属性读取配置
boolean disablePrinter = SystemProperties.getBoolean("config.disable_printer", false);
2. SystemServer根据属性判断是否启动服务
if (!disablePrinter && mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);
}
3. device.mk增加配置参数
在PRODUCT_PROPERTY_OVERRIDES中增加 config.disable_printer 配置
位置:aosp/build/target/board/emulator_x86_64/device.mk
# 裁剪应用
PRODUCT_DEL_PACKAGES += \
messaging \
Dialer \
Contacts \
Email \
Camera2 \
Browser2 \
Calendar \
DocumentsUI \
Music \
MusicFX \
ExactCalculator \
# printer服务是否开启
PRODUCT_PROPERTY_OVERRIDES += \
config.disable_printer=true
5. 屏蔽SystemFeature
查找含有<feature name=“android.software.print” /> 的文件,
通过aosp/frameworks/base/core/java/com/android/server/SystemConfig.java的readAllPermissions方法可知,会在以下文件夹获取打印特性配置文件。
- /system/etc/permission
- /system/etc/sysconfig
- /vendor/etc/permission
- /vendor/etc/sysconfig
- /oem/etc/permission
- /oem/etc/sysconfig
- /odm/etc/permission
- /odm/etc/sysconfig
- /product/etc/permission
- /product/etc/sysconfig
- /system_ext/etc/permission
- /system_ext/etc/sysconfig
查找具体位置,是/vendor/etc/permissions/handheld_core_hardware.xml文件中设置
cd /vendor/etc
find ./ -type f -exec grep -rn "software.print" {} \;
再查找源文件,发现是在frameworks/native/data/etc/handheld_core_hardware.xml文件中,
注释掉 <feature name=“android.software.print” />
find ./ -name "*.mk" -exec grep -rn "handheld_core_hardware" {} \;
6. 修改汇总
![在这里插入图片描述](https://img-blog.csdnimg.cn/64c3934f4d8e4ab5b40a11a277c0c6b5.png#pic_center
7. 重新编译,刷机
删掉out目录下的build.prop,重新编译system(或者直接修改build.prop然后make snod)
通过build.prop生成过程的分析,可知可以修改buildinfo.sh;、 system.prop;、ADDITIONAL_BUILD_PROPERTIES或PRODUCT_PROPERTY_OVERRIDES。建议改system.prop或PRODUCT_PROPERTY_OVERRIDES
8. 修改系统文件改变属性值
emulator -writable-system
adb root
adb remount
adb disable-verity
adb reboot
# 待手机重启
adb root
adb remount
修改config.disable_printer属性
adb pull /system/build.prop
# 修改导出的build.prop,末尾增加
####################################
# from variable PRODUCT_SYSTEM_DEFAULT_PROPERTIES
####################################
# Auto-added by post_process_props.py
persist.sys.usb.config=adb
config.disable_printer=false # 增加
# end of file
adb push build.prop /system/build.prop
adb reboot
修改handheld_core_hardware.xml文件
位置:/vendor/etc/permissions/handheld_core_hardware.xml
9. 测试
adb shell service list # 服务列表
adb shell service check printer # 检查服务是否开启
Android属性之build.prop生成过程分析
裁剪SystemServer服务以及关闭SystemFeature
adb remount fails - mount: ‘system’ not in /proc/mounts
关于如何修改android系统system文件夹下build.prop文件参数
lunch 新增一个菜单项
写给应用开发的 Android Framework 教程——玩转 AOSP 篇之添加 Product
如何为 AOSP 的 lunch 新增一个菜单项
注意,AndroidProducts.mk文件内容rice14大小写,否则Warning: Cannot display lunch menu。
文件BoardConfig.mk需要在device/jelly/rice14/rice14/目录下。
PRODUCT_MAKEFILES := \
$(LOCAL_DIR)/rice14.mk
COMMON_LUNCH_CHOICES := \
rice14-eng \
rice14-userdebug \
rice14-user
添加 C/C++、Java 可执行程序,添加 C/C++、Java 库,系统 App 源码添加
参见
写给应用开发的 Android Framework 教程
Android 13 内置三方应用app
Android #include <jni.h> file not found
原因为 S 上默认去掉了 JINI_H_INCLUDE 定义,若要彻底修改,需要更改build路径下go文件,较为繁琐,另一种方法是修改jni路径下Android.bp或Android.mk文件
# android.mk:
LOCAL_HEADER_LIBRARIES := jni_headers
# android.bp:
header_libs: ["jni_headers"],
ld.lld: error: undefined symbol: __android_log_print
Android.bp中增加
shared_libs: [
"liblog",
],
host_ldlibs: [
"-llog",
],
error: device/jelly/rice14/JNIApp/jni/Android.bp:1:1: dependency “libmymath” of “libmyjnilib” missing variant:
由于libmyjnilib的Android.bp文件中配有sdk_version: “current”,而libmymath的Android.bp文件中没有配置sdk_version: "current"导致
aosp/device/jelly/rice14/JNIApp/jni/Android.bp
cc_library_shared {
name: "libmyjnilib",
srcs: ["native.cpp"],
static_libs: [
],
shared_libs: [
"liblog",
"libmymath"
],
header_libs: ["jni_headers"],
host_ldlibs: [
"-llog",
],
cflags: [
"-Wall",
"-Werror",
],
stl: "none",
sdk_version: "current",
product_specific: true,
}
aosp/device/jelly/rice14/libmymath/Android.bp
cc_library_shared {
name: "libmymath",
srcs: ["my_math.cpp"],
export_include_dirs: ["."],
sdk_version: "current",
product_specific: true,
}
the APK is signed using APK Signature Scheme v2 but no such signature was found. Signature stripped?
android_app_import {
name: "Xxxx",
apk: "xxxx-5.3.3.apk",
// presigned: true, // 使用原来签名
certificate: "platform", // 指定APK的签名方式
dex_preopt: { // 关闭预编译,不会生成OAT文件
enabled: false,
},
}
集成第三方app时,因为使用presigned:true的原因,打包后的apk文件签名有问题,导致启动Android系统时加载App失败,设置改为certificate: "platform"即可。
# 签名校验
Sdk/build-tools/34.0.0/apksigner verify -v /data/aosp/out/target/product/rice14/system/app/Xxxx/Xxxx.apk
添加自定义系统服务
1. 定义AIDL
aosp/frameworks/base/core/java/android/bean/IBeanManager.aidl
aosp/frameworks/base/core/java/android/bean/Person.aidl
2. 生成binder相关的文件
make update-api
make framework
3. 实现Server端
aosp/frameworks/base/services/core/java/com/android/server/bean/BeanManagerService.java
BeanManagerService extends IBeanManager.Stub
可使用Lifecycle继承aosp/frameworks/base/services/core/java/com/android/server/SystemService.java来管理生命周期
4. 定义服务管理类
aosp/frameworks/base/core/java/android/bean/BeanManager.java
5. 将服务配置到系统
aosp/frameworks/base/core/java/android/content/Context.java
定义:public static final String BEAN_SERVICE = “bean”;
增加ServiceName:@StringDef(suffix = { “_SERVICE” }, value ={…BEAN_SERVICE…} )
aosp/frameworks/base/core/java/android/app/SystemServiceRegistry.java
6. 注册服务,配置开机启动
注册服务
aosp/frameworks/base/core/java/android/app/SystemServiceRegistry.java
registerService(Context.BEAN_SERVICE, BeanManager.class,
new CachedServiceFetcher<BeanManager>() {
@Override
public BeanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow(Context.BEAN_SERVICE);
IBeanManager service = IBeanManager.Stub.asInterface(b);
return new BeanManager(ctx, service);
}});
开机启动:
aosp/frameworks/base/services/java/com/android/server/SystemServer.java
在startOtherServices方法中加入启动
mSystemServiceManager.startService(BeanManagerService.Lifecycle.class);
7. 配置Selinux权限
data/aosp/device/jelly/rice14/sepolicy/common/service.te
定义类型:
type bean_service,service_manager_type;
aosp/device/jelly/rice14/sepolicy/common/service_contexts
定义上下文:
bean u:object_r:bean_service:s0
aosp/device/jelly/rice14/sepolicy/common/system_server.te
设置规则:
allow system_server bean_service:service_manager { add };
allow priv_app bean_service:service_manager { find };
8. Client端调用
aosp/packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
BeanManager beanManager = (BeanManager) getSystemService(Context.BEAN_SERVICE);
以上Service运行在system_server进程中,若需要运行在独立进程,
则需要创建一个App,在Application中创建添加服务,无需在startOtherServices中启动服务。
MelonManagerService mService = new MelonManagerService(getApplicationContext());
ServiceManager.addService(Context.MELON_SERVICE, mService, false, DUMP_FLAG_PRIORITY_DEFAULT);
App的manifest中配置参数
android:sharedUserId="android.uid.system"
coreApp="true"
android:directBootAware="true"
android:persistent="true"
参考
关于Selinux详解
SeLinux权限介绍
android Selinux 之 platform_app,system_app,priv_app,untrusted_app
make selinux_policy
adb push $(PRODUCT_OUT)/system/etc/selinux /system/etc/
adb push $(PRODUCT_OUT)/vendor/etc/selinux /vendor/etc/
adb reboot
其他
# 加入白名单
PRODUCT_ARTIFACT_PATH_REQUIREMENT_ALLOWED_LIST += \
system/app/Xxxx/Xxxx.apk \
# 加入应用
PRODUCT_PACKAGES += \
hello \
busybox \
Hellojava \
FirstSystemApp \
JNIApp \
Xxxx \
常用命令
adb shell pm list package -f # 应用和对应的包
find QSSI.12/out/target/product/qssi/ -name QtiDialer | xargs rm -rf # 查找,删除文件
Android.bp和Android.mk的对应关系在文件内:
aosp/build/soong/androidmk/androidmk/android.go
模块 | make 命令 | mmm 命令 |
---|---|---|
init | make init | mmm system/core/init |
zygote | make app_process | mmm frameworks/base/cmds/app_process |
system_server | make services | mmm frameworks/base/services |
java framework | make framework-minus-apex | mmm frameworks/base |
res framework | make framework-res | mmm frameworks/base/core/res |
jni framework | make libandroid_runtime | mmm frameworks/base/core/jni |
binder | make libbinder | mmm frameworks/native/libs/binder |
我们也可以使用 allmod 指令查看所有的 module,再使用 make 指令编译我们需要的模块。