android 8(O)预装APP到data/app目录

需求

想内置应用到/data/用户区,让其可卸载
系统android 8(O)
android 11可查看另外一篇《android 11®预装APP到data/app目录》

集成步骤

假设是添加myapplication.apk到sdk中的, 烧录后要装到/data/区(注意此方法恢复出厂后app会消失)

  1. 创建目录packages/apps/myapplication/,并进入这个目录
  2. 创建Android.mk配置文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Module name should match apk name to be installed
LOCAL_MODULE := myapplication
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_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
include $(BUILD_PREBUILT)

LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS) 由这行来控制安装到data/app下。

  1. 把编译好的myapplication.apk放到这个目录下
  2. 修改mk
    在下面两个任意文件中修改PRODUCT_PACKAGES加入新增模块名称即可。
    build/target/product/core.mk
    device/XXXX/device.mk 根据实际项目决定

在PRODUCT_PACKAGES下添加myapplication

PRODUCT_PACKAGES += myapplication

问题

编译后,在out目录下能搜索到myapplication.apk,但是烧录到设备后,启动找不到这个app

解决

在sdk下搜索PackageManagerService.java,不同的平台可能位置不一样,
比如
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
在PackageManagerService.java文件中搜索关键字not found; ignoring,把它对应的抛出异常的
代码屏蔽掉

diff --git a/base/services/core/java/com/android/server/pm/PackageManagerService.java b/base/services/core/java/com/android/server/pm/PackageManagerService.java
index 238a70f6..f380a66e 100644
--- a/base/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/base/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -12095,11 +12095,11 @@ public class PackageManagerService extends PackageManagerServiceExAbs
                                     + " but expected at " + known.codePathString
                                     + "; ignoring.");
                         }
-                    } else {
+                    } /*else {
                         throw new PackageManagerException(INSTALL_FAILED_INVALID_INSTALL_LOCATION,
                                 "Application package " + pkg.packageName
                                 + " not found; ignoring.");
-                    }
+                    }*/
                 }
             }

作者:帅得不敢出门

你可能感兴趣的:(Android平台,android)