How to parse the AndroidManifest.xml file inside an .apk package

前端 未结 16 1297
暖寄归人
暖寄归人 2020-11-22 10:32

This file appears to be in a binary XML format. What is this format and how can it be parsed programmatically (as opposed to using the aapt dump tool in the SDK)?

16条回答
  •  [愿得一人]
    2020-11-22 10:42

    I have been running with the Ribo code posted above for over a year, and it has served us well. With recent updates (Gradle 3.x) though, I was no longer able to parse the AndroidManifest.xml, I was getting index out of bounds errors, and in general it was no longer able to parse the file.

    Update: I now believe that our issues was with upgrading to Gradle 3.x. This article describes how AirWatch had issues and can be fixed by using a Gradle setting to use aapt instead of aapt2 AirWatch seems to be incompatible with Android Plugin for Gradle 3.0.0-beta1

    In searching around I came across this open source project, and it's being maintained and I was able to get to the point and read both my old APKs that I could previously parse, and the new APKs that the logic from Ribo threw exceptions

    https://github.com/xgouchet/AXML

    From his example this is what I'm doing

      zf = new ZipFile(apkFile);
    
      //Getting the manifest
      ZipEntry entry = zf.getEntry("AndroidManifest.xml");
      InputStream is = zf.getInputStream(entry);
    
         // Read our manifest Document
         Document manifestDoc = new CompressedXmlParser().parseDOM(is);
    
         // Make sure we got a doc, and that it has children
         if (null != manifestDoc && manifestDoc.getChildNodes().getLength() > 0) {
            //
            Node firstNode = manifestDoc.getFirstChild();
    
            // Now get the attributes out of the node
            NamedNodeMap nodeMap = firstNode.getAttributes();
    
            // Finally to a point where we can read out our values
            versionName = nodeMap.getNamedItem("android:versionName").getNodeValue();
            versionCode = nodeMap.getNamedItem("android:versionCode").getNodeValue();
         }
    

提交回复
热议问题