美文网首页
Android AAR权限检测脚本

Android AAR权限检测脚本

作者: 砺雪凝霜 | 来源:发表于2021-07-05 15:40 被阅读0次
    
    
    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权限检测脚本

          本文链接:https://www.haomeiwen.com/subject/rchiultx.html