android 11(R)预装APP到data/app目录

android高版本,默认是无法预装app到/data/app目录下的,因为用低版本的方法去修改的话烧录后会无法开机。

步骤

假设是添加myapplication.apk到sdk中的, 烧录后要装到/data/区

  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. 修改build/make/target/product/handheld_product.mk
    在PRODUCT_PACKAGES下添加myapplication
PRODUCT_PACKAGES += myapplication

编译成功标志

编译过程会有类似打印,说明编译进去了
[ 99% 2339/2362] Install: out/target/product/ums512_1h10/data/app/myapplication/myapplication.apk

然后会在out/target/product/xx不同平台目录不一样xx/ 目录下生成userdata.img
生成的pac镜像中就会带这个userdata的数据

问题

烧录后会出错, 无法正常开机,会自动进Recovery模式
错误提示:
Can’t load Android system. You’r data may be corrupt.If you continue to get this message, you mayneed to perform a factory data reset and erase all user data stored on this devices.

REASON IS [set_policy_failed:/data/app]

解决

从目录加密策略读取和设置上入手
有多两种修改方法

这处两种都要先修改
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

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 b96e4b378..f5fd73c5c 100755
--- a/base/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/base/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -12138,11 +12138,12 @@ public class PackageManagerService extends PackageManagerServiceExAbs
                                     + " but expected at " + known.codePathString
                                     + "; ignoring.");
                         }
-                    } else {
+                    } /*else {
                         throw new PackageManagerException(INSTALL_FAILED_INVALID_INSTALL_LOCATION,
                                 "Application package " + pkg.getPackageName()
                                 + " not found; ignoring.");
                     }
+                    */
                 }
             }

一种方法

修改system/core/init/fscrypt_init_extensions.cpp
中的FscryptSetDirectoryPolicy()返回为true

diff --git a/core/init/fscrypt_init_extensions.cpp b/core/init/fscrypt_init_extensions.cpp
index fbd8189..a6406d9 100644
--- a/core/init/fscrypt_init_extensions.cpp
+++ b/core/init/fscrypt_init_extensions.cpp
@@ -149,5 +149,6 @@ bool FscryptSetDirectoryPolicy(const std::string& ref_basename, FscryptAction ac
         delete_dir_contents(dir);
         return SetPolicyOn(ref_basename, dir);
     }
-    return false;
+    //return false;
+    return true;
 }

这个改法比较暴力,把目录加密策略都跳过了。

另一种改法

单独把app目录加上路过名单
system/core/init/util.cpp

diff --git a/core/init/util.cpp b/core/init/util.cpp
index 7c863ae..53e4df6 100644
--- a/core/init/util.cpp
+++ b/core/init/util.cpp
@@ -461,7 +461,7 @@ static FscryptAction FscryptInferAction(const std::string& dir) {
     std::vector<std::string> directories_to_exclude = {
             "lost+found", "system_ce", "system_de", "misc_ce",     "misc_de",
             "vendor_ce",  "vendor_de", "media",     "data",        "user",
-            "user_de",    "apex",      "preloads",  "app-staging", "gsi",
+            "user_de",    "apex",      "preloads",  "app-staging", "gsi", "app",
     };
     for (const auto& d : directories_to_exclude) {
         if ((prefix + d) == dir) {

修改system/core/rootdir/init.rc

diff --git a/core/rootdir/init.rc b/core/rootdir/init.rc
index ceb223f..3973f68 100644
--- a/core/rootdir/init.rc
+++ b/core/rootdir/init.rc
@@ -658,7 +658,7 @@ on post-fs-data
     mkdir /data/app-ephemeral 0771 system system encryption=Require
     mkdir /data/app-asec 0700 root root encryption=Require
     mkdir /data/app-lib 0771 system system encryption=Require
-    mkdir /data/app 0771 system system encryption=Require
+    mkdir /data/app 0771 system system encryption=None
     mkdir /data/property 0700 root root encryption=Require
     mkdir /data/tombstones 0775 system system encryption=Require
     mkdir /data/vendor/tombstones 0771 root root

作者:帅得不敢出门

你可能感兴趣的:(Android平台,android,java,开发语言)