使用android studio开发system app比使用notepad++、记事本之类的工具要方便很多,所以有时候我们需要验证一个系统功能的时候就可以不用整编安卓源码或者单编安卓源码了,并具使用android studio有一个优势就是可以直接单步调试
1.app/build.gradle添加platform.jks签名
android {
signingConfigs {
platform {
storeFile file('keystore/yy3568/platform.jks')
storePassword '123456'
keyAlias 'platform'
keyPassword '123456'
}
}
defaultConfig {
signingConfig signingConfigs.platform
}
buildTypes {
release {
signingConfig signingConfigs.platform
}
debug {
signingConfig signingConfigs.platform
}
}
}
2.AndroidManifest.xml根节点添加如下内容:
android:sharedUserId="android.uid.system"
如果manifest中添加了如上属性,但是build.gradle中配置的不是platform.jks,那么在run的时候将会报错,无法run起来,报如下错误:
3.platform.jks制作方法
openssl pkcs8 -inform DER -nocrypt -in "platform.pk8" -out "key"
openssl pkcs12 -export -in "platform.x509.pem" -inkey "key" -out "p12" -password pass:123456 -name platform
openssl x509 -noout -fingerprint -in "platform.x509.pem"
keytool -importkeystore -deststorepass 123456 -destkeystore "platform.jks" -srckeystore "p12" -srcstoretype PKCS12 -srcstorepass 123456
openssl需使用openssl1.1.1w版本,不能使用openssl3.2.0版本,否则会一直提示密钥库密码错误
4.验证自开发app是否是system app方法
try {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
int largeMemoryClass = activityManager.getLargeMemoryClass();
Class<?> clz = Class.forName("android.app.ActivityManager");
Method getPackageImportance = clz.getDeclaredMethod("getPackageImportance", String.class);
getPackageImportance.setAccessible(true);
Object invoke = getPackageImportance.invoke(activityManager, "com.android.system_app_demo");
Log.e(TAG, "onClick: " + invoke);
Toast.makeText(this, "调用系统接口成功", Toast.LENGTH_SHORT).show();
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
Toast.makeText(this, "调用系统接口失败", Toast.LENGTH_SHORT).show();
}
后言
如果你觉得这篇文章帮助到了你,欢迎打赏