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

前端 未结 16 1017
暖寄归人
暖寄归人 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:53

    apk-parser, https://github.com/caoqianli/apk-parser, a lightweight impl for java, with no dependency for aapt or other binarys, is good for parse binary xml files, and other apk infos.

    ApkParser apkParser = new ApkParser(new File(filePath));
    // set a locale to translate resource tag into specific strings in language the locale specified, you set locale to Locale.ENGLISH then get apk title 'WeChat' instead of '@string/app_name' for example
    apkParser.setPreferredLocale(locale);
    
    String xml = apkParser.getManifestXml();
    System.out.println(xml);
    
    String xml2 = apkParser.transBinaryXml(xmlPathInApk);
    System.out.println(xml2);
    
    ApkMeta apkMeta = apkParser.getApkMeta();
    System.out.println(apkMeta);
    
    Set locales = apkParser.getLocales();
    for (Locale l : locales) {
        System.out.println(l);
    }
    apkParser.close();
    

提交回复
热议问题