需要的东西
- 一个不带google apis的模拟器
- AOSP的签名
1. 创建模拟器
选择一个不带google apis的模拟器系统,Android版本里是Android Open Source Project,也就是AOSP编译出来的系统镜像
2. 启动模拟器
# 列出所有模拟器,找到你刚创建的模拟器
emulator -list-avds
# 启动模拟器,指定系统可以写入,emulator -avd @<YourAVDNameHere> -writable-system
emulator @Pixel_XL_API_31 -writable-system
3. 下载AOSP签名
找到你模拟器系统对应的AOSP源码,这里需要“科学”,比如我是Android12的版本,签名在
https://android.googlesource.com/platform/build/+/refs/heads/android12-release/target/product/security/
需要用到的就是这两个文件platform.pk8、platform.x509.pem
4. 对你的apk进行签名,并推送到/system/app
adb root
adb remount
apksigner sign --key platform.pk8 --cert platform.x509.pem <YourAPK>
adb shell mkdir /system/app/<YourAppFolder>
adb push <YourAPK> /system/app/<YourAppFolder>/
adb reboot
注意,你的apk需要在AndroidManifest.xml指明android:sharedUserId="android.uid.system"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system"
package="<YourPackageNameHere>">
签名也可以存成keystore进行签名,参考:https://github.com/getfatday/keytool-importkeypair
5. 判断是否是system app
adb shell 命令行判断
adb shell ps -A | grep <YourPackageNameHere>
app内代码判断
public static boolean isSystemApplication(Context context, String packageName){
PackageManager mPackageManager = context.getPackageManager();
try {
final PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, PackageManager.GET_CONFIGURATIONS);
if((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)!=0){
return true;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return false;
}