权限定义
在frameworks/base/core/res/AndroidManifest.xml 中新增:
<permission android:name="android.permission.XXXXXXXXXXXXXXXXXX"
android:label="@string/permlab_xxxx"
android:description="@string/permdesc_xxxx"
android:protectionLevel="signature|privileged" />
在res/values/strings.xml中添加字符串资源permlab_xxxx、permdesc_xxxx。
protectionLevel中指定的“signature|privileged”表示只有使用Framework的platform签名才能被授予该权限。
编译
$ source build/envsetup.sh
$ lunch
$ mmm frameworks/base/core/res -j8
[100% 361/361] Install: out/target/product/bengal/system/framework/framework-res.apk
$ adb root
$ adb remount
$ adb push out/target/product/bengal/system/framework/framework-res.apk /system/framework/framework-res.apk
$ adb shell sync
$ adb reboot
在App的manifest中声明权限
<uses-permission android:name="android.permission.XXXXXXXXXXXXXXXXXX"></uses-permission>
在App的build.gradle中配置platform签名
android {
。。。
signingConfigs {
release {
storeFile file("./sign/myplatform.jks")
storePassword '111111'
keyAlias 'myplatform'
keyPassword '111111'
}
debug {
storeFile file("./sign/myplatform.jks")
storePassword '111111'
keyAlias 'myplatform'
keyPassword '111111'
}
}
。。。
}
myplatform.jks文件的生成参考:https://www.jianshu.com/p/7ac171669f57
也可以配置Android.mk文件,使apk随系统编译时使用platform签名。
在Framework层做权限验证
以cpp代码为例:
#include <binder/IPCThreadState.h>
#include <binder/PermissionController.h>
android::PermissionController pc;
android::String16 perm("android.permission.XXXXXXXXXXXXXXXXXX");
pid_t pid = android::IPCThreadState::self()->getCallingPid();
uid_t uid = android::IPCThreadState::self()->getCallingUid();
bool hasPermisstion = pc.checkPermission(perm, pid, uid);
if (hasPermisstion) {
ALOGI("hasPermisstion");
} else {
ALOGI("hasNotPermisstion");
}