对Android权限一些新的理解

对Android权限一些新的理解

进来由于业务需求,公司的apk希望拥有较高界别的系统权限,所以对android权限这块查找了不少资料,对权限有了一些新的认识和理解。

1、android权限等级

     android4.4之后把权限分为几个等级,网上都说把权限分为了四个等级,分别是normal、dangerous、signature、signatureOrSystem,这几个等级分别是用android:protectionLevel属性来标记的。

     normal权限是普通权限,不会对系统造成危害的,不需要用户授权,apk安装的时候系统自动授权。

     dangerous是危险权限,系统不会自动对apk授予这些权限,需要apk运行时动态申请这些权限。

     signature,按照官方资料的解释,A permission that the system grants only if the requestingapplication is signed with the same certificate as the application thatdeclared the permission. If the certificates match, the system automaticallygrants the permission without notifying the user or asking for the user'sexplicit approval. 中文翻译过来就是需要与声明该权限的应用所有签名一样,才能获取该权限。

     signatureOrSystem,按照官方资料的解释,A permission that the system grants only to applications that are inthe Android system image or that are signed with the same certificate as theapplication that declared the permission. Please avoid using this option, asthe signature protection level should be sufficient for most needs and worksregardless of exactly where applications are installed. The"signatureOrSystem" permission is used for certain special situationswhere multiple vendors have applications built into a system image and need toshare specific features explicitly because they are being built together. 中文翻译过来就是,需要系统签名或者声明该权限的app的数字证书来签名。这一级别适用于非常特殊的情况,比如多个供应商需要通过系统影像共享功能时。其实这个级别的权限一般的开发者基本用不到,看了下源码,声明为signatureOrSystem一共就两个:

<permissionandroid:name="android.permission.ACCESS_UCE_PRESENCE_SERVICE"

       android:permissionGroup="android.permission-group.PHONE"

       android:protectionLevel="signatureOrSystem"/>

   <!-- @hide Allows an application to Access UCE-OPTIONS.

        <p>Protection level: dangerous

   -->

   <permissionandroid:name="android.permission.ACCESS_UCE_OPTIONS_SERVICE"

       android:permissionGroup="android.permission-group.PHONE"

       android:protectionLevel="signatureOrSystem"/>

其实,看过源码就知道,android系统权限声明的时候protectionLevel不止这四个,还有signature|privileged,这个权限是特权app拥有的权限,什么是特权app呢,是比普通系统app级别更高的一种系统app。现在系统app的安装目录有两个:system/app和system/priv-app。System/app里面的是普通系统app,System/priv-app里面的是特权app,特权app比普通系统app拥有的权限更多。

2、如何获取到系统应用权限

经过本人的多次测试(只在android6.0以上的机器测过),要想获得一些系统权限,比如MODIFY_PHONE_STATE和WRITE_APN_SETTINGS权限,把app预制到system/app下面基本上没什么卵用,signature|privileged标记的权限获取不到,甚至一些@systemapi注释的api也调用不了,就剩下用户无法卸载的作用了,感觉里面的应用称为预制应用比较合适,不能再叫做系统应用了。而把我的app放到system/priv-app里面一切都ok了。同时我也验证过用系统签名签过的第三方应用,可以获取到signature|privileged标记的权限,即系统签名的应用的权限等级应该和特权应用的权限等级是一样的。可见作为第三方应用,要想获取系统应用的同等的权限,跟设备厂商关系铁的话,当然预制进去是最佳方案,关系不够铁的话,只能用系统签名了。

猜你喜欢

转载自blog.csdn.net/baidu_27196493/article/details/78253753