import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler
public class PermissionHandler extends DefaultHandler {
public HashSet<String> permissions;
public PermissionHandler(){
permissions = new HashSet<>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// 接收元素开始的通知。
if ("uses-permission".equals(localName)) {
//如果当前运行的节点名称与设定需要读取的节点名称相同,则实例化HashMap
permissions.add(attributes.getValue("android:name"));
}else if("uses-permission-sdk-23".equals(localName)){
permissions.add(attributes.getValue("android:name"));
}else if("uses-permission-sdk-m".equals(localName)){
permissions.add(attributes.getValue("android:name"));
}
}
public HashSet<String> getPermissions(){
return permissions;
}
}
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import com.android.SdkConstants
import static com.android.build.gradle.internal.publishing.AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH
import static com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactScope.ALL
import static com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.AAR
import javax.xml.parsers.SAXParserFactory
import groovy.json.JsonOutput
gradle.projectsEvaluated {
def factory = SAXParserFactory.newInstance()
factory.setNamespaceAware(true)
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true)
factory.setFeature("http://xml.org/sax/features/xmlns-uris", true)
factory.setValidating(false)
def permission_extension = project.extensions.getByName("android")
//assembleDebug
permission_extension.applicationVariants.all { variant ->
variant.getPreBuildProvider().get().doLast{
HashMap<String, Set<String>> permission_map = new HashMap<String, Set<String>>()
variant.variantData.scope.getArtifactFileCollection(RUNTIME_CLASSPATH, ALL, AAR).files.forEach(){
String aar_file_path = it.absolutePath
//排除libs目录下的aar的影响
//!aar_file_path.contains(project_path)
// /Users/lianjia/.gradle/caches/modules-2/files-2.1/com.lianjia.common.android/lib_vr/2.11.6/41f182428c6c4f13cfe2d5023c90cd1db3efb024/lib_vr-2.11.6.aar
def component_name = null
if (!aar_file_path.contains(project.path)){
def paths = aar_file_path.split('/')
def name = paths[-4]
def group = paths[-5]
component_name = group + ":" + name
}else{
component_name = it.name//aar文件
}
ZipFile aarFile
InputStream source
try {
aarFile = new ZipFile(aar_file_path)
ZipEntry zipEntry = aarFile.getEntry(SdkConstants.FN_ANDROID_MANIFEST_XML)
source = aarFile.getInputStream(zipEntry)
def handler = new PermissionHandler()
factory.newSAXParser().parse(source, handler)
HashSet<String> permissions = handler.getPermissions()
if(permissions.size() > 0 && component_name != null){
permission_map.put(component_name, permissions)
}
}catch(IOException e){
e.printStackTrace()
}finally{
try {
if (aarFile != null) {
aarFile.close();
}
if(source != null) {
source.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
//产生html文件
def json = JsonOutput.toJson(permission_map)
String jsonStr = JsonOutput.prettyPrint(json)
File dir = new File(project.projectDir.absolutePath + "/dependencies")
if(!dir.exists()){
dir.mkdir()
}
def inputFile = new File(dir.absolutePath, "permission.json")
if(inputFile.exists()){
inputFile.delete()
}
inputFile.createNewFile()
inputFile.withWriter('utf-8') { writer ->
writer.write jsonStr
}
}
}
}
Android AAR权限检测脚本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一般检测root权限分两种情况: 第一种:检测应用是否拥有root权限,这种检测可以直接运行su命令,然后一般会有...
- 权限检测生效条件: targetSdkVersion 以及 compileSdkVersion 升级到 23 及以...
- 在做Android开发过程中,兼容问题一直都是安卓攻城狮们的常见的一个问题,各种手机品牌的兼容。还有API版本高低...