權(quán)限定義
在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簽名才能被授予該權(quán)限。
編譯
$ 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中聲明權(quán)限
<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文件的生成參考:http://www.itdecent.cn/p/7ac171669f57
也可以配置Android.mk文件,使apk隨系統(tǒng)編譯時使用platform簽名。
在Framework層做權(quán)限驗證
以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");
}