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版本高低...