Some tools and knowledge used by APP

1. View the versionCode of android

Generally read from androidmanifest.xml, use the tools AXMLPrinter.jar and AXMLPrinter2.jar online, the author of the maven warehouse is the same person

AXMLPrinter2.jar apk analyzes the APK file and obtains the package name, version number and icon in the APK file. It is a very powerful tool, and once again I feel the power of batch processing. It can decompile the binary XML file (binary xml file) compiled by android and output and save it in plain text. It is one of the necessary tools for APK decompilation and modification. For example, if you need to check the permission and name of the apk installation package, you can use AXMLPrinter2 to decompile androidmanifest.xml to view it in plain text. The decompilation speed is very fast and easy to use, and the .XML file can be compiled smoothly.
CMD execution command: java -jar AXMLPrinter.jar AndroidManifest.xml 

java code:

@Slf4j
public class ApkUtil {
    private static final String DIMENSION_UNITS[] = { "px", "dip", "sp", "pt", "in", "mm", "", "" };
    private static final String FRACTION_UNITS[]  = { "%", "%p", "", "", "", "", "", "" };
    private static final float  RADIX_MULTS[]     = { 0.00390625F, 3.051758E-005F, 1.192093E-007F, 4.656613E-010F };
    
    /********
     * 使用axmlprinter2-1.21.jar解析
     *******/
    public static Map<String, Object> readAPK(File apkFile) {
        ZipFile zipFile = null;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            zipFile = new ZipFile(apkFile);
            Enumeration<?> enumeration = zipFile.entries();

            ZipEntry zipEntry = null;
            while (enumeration.hasMoreElements()) {
                zipEntry = (ZipEntry) enumeration.nextElement();
                if (zipEntry.isDirectory()) {

                } else {
                    if ("androidmanifest.xml".equals(zipEntry.getName().toLowerCase())) {
                        AXmlResourceParser parser = new AXmlResourceParser();
                        parser.open(zipFile.getInputStream(zipEntry));

                        while (true) {
                            int type = parser.next();
                            if (type == XmlPullParser.END_DOCUMENT) {
                                break;
                            }
                            String name = parser.getName();
                            if (null != name && name.toLowerCase().equals("manifest")) {
                                for (int i = 0; i != parser.getAttributeCount(); i++) {
                                    if ("versionName".equals(parser.getAttributeName(i))) {
                                        String versionName = getAttributeValue(parser, i);
                                        if (null == versionName) {
                                            versionName = "";
                                        }
                                        map.put("versionName", versionName);
                                    } else if ("package".equals(parser.getAttributeName(i))) {
                                        String packageName = getAttributeValue(parser, i);
                                        if (null == packageName) {
                                            packageName = "";
                                        }
                                        map.put("package", packageName);
                                    } else if ("versionCode".equals(parser.getAttributeName(i))) {
                                        String versionCode = getAttributeValue(parser, i);
                                        if (null == versionCode) {
                                            versionCode = "";
                                        }
                                        map.put("versionCode", versionCode);
                                    }
                                }
                                break;
                            }
                        }
                    }

                }
            }
            zipFile.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                log.error("error", e);
            }
        }
        return map;
    }

    public static String readAPK(File apkFile, String attributeName) {
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(apkFile);
            Enumeration<?> enumeration = zipFile.entries();

            ZipEntry zipEntry = null;
            while (enumeration.hasMoreElements()) {
                zipEntry = (ZipEntry) enumeration.nextElement();
                if (zipEntry.isDirectory()) {

                } else {
                    if ("androidmanifest.xml".equals(zipEntry.getName().toLowerCase())) {
                        AXmlResourceParser parser = new AXmlResourceParser();
                        parser.open(zipFile.getInputStream(zipEntry));

                        while (true) {
                            int type = parser.next();
                            if (type == XmlPullParser.END_DOCUMENT) {
                                break;
                            }
                            String name = parser.getName();
                            if (null != name && name.toLowerCase().equals("manifest")) {
                                for (int i = 0; i != parser.getAttributeCount(); i++) {
                                    if (attributeName.equals(parser.getAttributeName(i))) {
                                        String value = getAttributeValue(parser, i);
                                        return value;
                                    }
                                }
                                break;
                            }
                        }
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static String getAttributeValue(AXmlResourceParser parser, int index) {
        int type = parser.getAttributeValueType(index);
        int data = parser.getAttributeValueData(index);
        if (type == TypedValue.TYPE_STRING) {
            return parser.getAttributeValue(index);
        }
        if (type == TypedValue.TYPE_ATTRIBUTE) {
            return String.format("?%s%08X", getPackage(data), data);
        }
        if (type == TypedValue.TYPE_REFERENCE) {
            return String.format("@%s%08X", getPackage(data), data);
        }
        if (type == TypedValue.TYPE_FLOAT) {
            return String.valueOf(Float.intBitsToFloat(data));
        }
        if (type == TypedValue.TYPE_INT_HEX) {
            return String.format("0x%08X", data);
        }
        if (type == TypedValue.TYPE_INT_BOOLEAN) {
            return data != 0 ? "true" : "false";
        }
        if (type == TypedValue.TYPE_DIMENSION) {
            return Float.toString(complexToFloat(data)) + DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
        }
        if (type == TypedValue.TYPE_FRACTION) {
            return Float.toString(complexToFloat(data)) + FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
        }
        if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
            return String.format("#%08X", data);
        }
        if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
            return String.valueOf(data);
        }
        return String.format("<0x%X, type 0x%02X>", data, type);
    }

    private static String getPackage(int id) {
        if (id >>> 24 == 1) {
            return "android:";
        }
        return "";
    }

    // / ILLEGAL STUFF, DONT LOOK :)
    private static float complexToFloat(int complex) {
        return (float) (complex & 0xFFFFFF00) * RADIX_MULTS[(complex >> 4) & 3];
    }





    public static void main(String args[]) {
        String path = "D:\\公司\\z直营\\魔豆\\发布压缩包\\com.example.myapplicationentry.apk";
        System.out.println(readAPK(new File(path)));
    }
}

 

 

 

 

Guess you like

Origin blog.csdn.net/lan861698789/article/details/115561596