需求
想内置应用到 “/data/” 用户区,让其可卸载
系统android 8 (O)
android 11可查看另外一篇《android 11®预装APP到 data/app 目录》
测试过的平台:MTK
集成步骤
如果发现不管怎么改都无效,请静下心来,clean一次把out删除再试一下,有些时候错误的编译残留会导致怎么改都无效.
假设是添加myapplication.apk到sdk中的, 烧录后要装到/data/区(注意此方法恢复出厂后app会消失)
- 创建目录packages/apps/myapplication/,并进入这个目录
- 把编译好的myapplication.apk放到这个目录下
- 修改mk
在下面两个任意文件中修改PRODUCT_PACKAGES加入新增模块名称即可。
build/target/product/core.mk
device/XXXX/device.mk 根据实际项目决定
在PRODUCT_PACKAGES下添加myapplication
PRODUCT_PACKAGES += myapplication
- 创建Android.mk配置文件
注意这里使用的shell cp的方式把apk拷贝到data中,如果不这么写,可能会导致编译过程签名被改变,导致内置失败,这个问题后面会详细讲.
# ifneq ($(strip $(FEATURE_BN_NOT_BUILD_APP)),true)
LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := myapplication
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
# copy apk to /data/app
$(shell mkdir -p $(TARGET_OUT_DATA_APPS)/app/$(LOCAL_MODULE))
$(shell cp $(LOCAL_PATH)/$(LOCAL_SRC_FILES) $(TARGET_OUT_DATA_APPS)/app/$(LOCAL_MODULE))
# endif
这种写法不需要额外在mk中处理so库,默认在apk包中是自带库的,在设备启动过程会自动解压到data下的相应目录中.
- 修改frameworks中源码
如果不修改,会有烧录固件启动data为空的问题,在启动过程会提示检测data中的app签名失败,然后会自动删除app,
在Android11中需要修改cpp及java的代码,Android8只需要修改java中源码.
在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.mk错误写法引起的问题
这里列个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下。
这种写法,编译后,在out目录下能搜索到myapplication.apk,但是烧录到设备后,启动找不到这个app
运行过程出错:Failed to scan /data/app/: Failed to collect certificates from xxx
设备会自动删除app
把out下的apk拷贝出来用adb install命令也装不了,
Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES: Failed to collect certificates from /data/app/vmdl1194648165.tmp/base.apk: META-INF/CERT.SF indicates /data/app/vmdl1194648165.tmp/base.apk is signed using APK Signature Scheme v2, but no such signature was found. Signature stripped?]
可见编译过程apk被改动了。
APP依赖system中lib的问题
建议data的apk中自带lib,如果依赖/system/lib/下的库,
则需要设置成/data/app下apk可访问/system/lib/下的对应库,否则运行会崩溃出错:
java.lang.UnsatisfiedLinkError: dlopen failed: library “/system/lib/libmmkv.so” needed or dlopened by “/system/lib/libnativeloader.so” is not accessible for the namespace “classloader-namespace”
- 一种修改方法
diff --git a/core/libnativeloader/native_loader.cpp b/core/libnativeloader/native_loader.cpp
index 809160c..d3af503 100644
--- a/core/libnativeloader/native_loader.cpp
+++ b/core/libnativeloader/native_loader.cpp
@@ -102,7 +102,7 @@ static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classload
// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
// System.load() with an absolute path which is outside of the classloader library search path.
// This list includes all directories app is allowed to access this way.
-static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
+static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand:/system/lib";
static bool is_debuggable() {
char debuggable[PROP_VALUE_MAX];
- 另外一种方法
修改sdk下的
system/core/rootdir/etc/public.libraries.android.txt
把需要的库加到这个文件中
比如添加
libmmkv.so
libnativeloader.so
作者:帅得不敢出门 原创文章谢绝转载收录