Android 源码集成可卸载 APP

android系统包含三类APP: 1、可自由卸载APP安装在 /data/app目录下。 2、系统APP放在 /system/app目录。 3、特权APP放在 /system/priv-app目录。

系统编译后,打包前, /data分区不起作用,因此系统打包前,可以先将APP全部拷贝到 /system分区的 /system/usr/app目录下。

1、拷贝APP暂存到 /system/usr/app 目录

在 /build/target/product/base_product.mk中添加如下命令, 会将 /apps/apps目录下的所有文件拷贝到 /system/usr/app ,将 shell脚本拷贝到 /system/bin目录

bash 复制代码
# 拷贝APP
PRODUCT_COPY_FILES += $(call find-copy-subdir-files,*,/data1/rom/android/lineageOS2/apps/apps,/system/usr/app)
# 拷贝shell
PRODUCT_COPY_FILES += $(call find-copy-subdir-files,*,/data1/rom/android/lineageOS2/apps/shell,/system/bin)

2、绕过拷贝 .apk文件时校验

注释 /build/core/Makefile 如下 apk拷贝报错代码

bash 复制代码
define check-product-copy-files
$(if $(filter %.apk, $(1)),$(error \
    Prebuilt apk found in PRODUCT_COPY_FILES: $(1), use BUILD_PREBUILT instead!))
endef

3、系统启动执行init.rc脚本时,拷贝APP到 /data分区

在 init.rc文件中定义如下service , 并在 on boot 事件中 执行 preinstall

bash 复制代码
service preinstall /system/bin/preinstall.sh
    class main
    user root
    group root
    oneshot
 
 
on boot
    xxxxx
    xxxxx
    .....
    start preinstall

4、shell 脚本文件如下

bash 复制代码
#!/system/bin/sh
 
CUSTOMIZED_APK=/system/usr/app
DATA_APK=/data/app
 
echo "CUSTOMIZED_APK=${CUSTOMIZED_APK}"
#获取是否已经预安装过标记位
PREINSTALL_RESULT=`getprop persist.sys.preinstall.value`
echo "PREINSTALL_RESULT=${PREINSTALL_RESULT}"
apk_files=""
#判断标记位是否为空,为空则没有预装过。然后将所有apk均copy到data/app下面。
 
if [ -z "${PREINSTALL_RESULT}" ]; then
 
cd ${CUSTOMIZED_APK}
 
apk_files=$(ls *.apk )
 
echo "apks files = ${apk_files}"
 
for apkfile in $apk_files
 
do
 
echo " apkfiles = ${apkfile} "
 
cp -vf ${CUSTOMIZED_APK}/${apkfile} ${DATA_APK}/${apkfile}
 
echo "start copy "
 
chmod 777 ${DATA_APK}/${apkfile}
 
done
 
#设置标记位
setprop persist.sys.preinstall.value 1
 
 
# 拷贝 设备改机文件
cp /system/etc/device.json /data/system/device.json
chmod 666 /data/system/device.json
# 拷贝adb公钥
cp /system/etc/adb_keys /data/system/adb_keys
chown -R shell:shell /data/system/adb_keys
chmod 666 /data/system/adb_keys
 
cd ../..
 
fi

5、添加selinux权限

file_contexts文件: system/sepolicy/private/file_contexts 添加下面一行:

bash 复制代码
/system/bin/preinstall.sh  u:object_r:preinstall_exec:s0

同目录新建文件 preinstall.te 内容如下:

bash 复制代码
type preinstall, domain;
type preinstall_exec, exec_type, file_type;
 
init_daemon_domain(preinstall)
#全部默认允许
permissive preinstall;

修改 init.te 允许读取并执行 preinstall:

bash 复制代码
#默认全部允许
allow init preinstall_exec:file {read open getattr execute};

android 11 系统 在 system/sepolicy/prebuilts/api/30.0/private 目录同步修改

TODO

最后一次拉取的android11源码 将 apk拷贝到 /data/app目录后开机没有扫描安装,原因待分析

FIXED :

在 PackageManagerService. assertPackageIsValid 方法中 ,如下代码段:

java 复制代码
// If we're only installing presumed-existing packages, require that the
// scanned APK is both already known and at the path previously established
// for it.  Previously unknown packages we pick up normally, but if we have an
// a priori expectation about this package's install presence, enforce it.
// With a singular exception for new system packages. When an OTA contains
// a new system package, we allow the codepath to change from a system location
// to the user-installed location. If we don't allow this change, any newer,
// user-installed version of the application will be ignored.
if ((scanFlags & SCAN_REQUIRE_KNOWN) != 0) {
    if (mExpectingBetter.containsKey(pkg.getPackageName())) {
        logCriticalInfo(Log.WARN,
                "Relax SCAN_REQUIRE_KNOWN requirement for package "
                        + pkg.getPackageName());
    } else {
        PackageSetting known = mSettings.getPackageLPr(pkg.getPackageName());
        if (known != null) {
            if (DEBUG_PACKAGE_SCANNING) {
                Log.d(TAG, "Examining " + pkg.getCodePath()
                        + " and requiring known paths " + known.codePathString
                        + " & " + known.resourcePathString);
            }
            if (!pkg.getCodePath().equals(known.codePathString)
                    || !pkg.getCodePath().equals(known.resourcePathString)) {
                throw new PackageManagerException(INSTALL_FAILED_PACKAGE_CHANGED,
                        "Application package " + pkg.getPackageName()
                        + " found at " + pkg.getCodePath()
                        + " but expected at " + known.codePathString
                        + "; ignoring.");
            }
        } else {
            // throw new PackageManagerException(INSTALL_FAILED_INVALID_INSTALL_LOCATION,
            //         "Application package " + pkg.getPackageName()
            //         + " not found; ignoring.");
        }
    }
}

注释掉 INSTALL_FAILED_INVALID_INSTALL_LOCATION 异常。

同时,修改selinux权限: untrusted_app_all 增加 execute 权限:

bash 复制代码
#允许app读取/data/system目录文件 允许app 执行 data/app-libm目录下的可执行文件
allow untrusted_app_all system_data_file:file { open read execute};
相关推荐
游戏开发爱好者83 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
王码码20353 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
黑码哥3 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓3 小时前
[JDBC]元数据
android
独行soc3 小时前
2026年渗透测试面试题总结-17(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
金融RPA机器人丨实在智能3 小时前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿3 小时前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc4 小时前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
王码码20354 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_915106324 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview