【Java】Android解析apk文件中的AndroidManifest.xml

該ApkUtil的主要功能是通過解析AndroidManifest.xml,獲取apk的版本號(即versionCode)和名字(versionName)。

import android.util.TypedValue;
import brut.androlib.res.decoder.AXmlResourceParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.XmlPullParser;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ApkUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApkUtil.class);

    private static final float RADIX_MULTS[] = {0.00390625F, 3.051758E-005F, 1.192093E-007F, 4.656613E-010F};

    private static final String DIMENSION_UNITS[] = {"px", "dip", "sp", "pt", "in", "mm", "", ""};

    private static final String FRACTION_UNITS[] = {"%", "%p", "", "", "", "", "", ""};

    /**
     * 獲取apk信息
     *
     * @param apkPath
     * @return
     */
    public static String[] getApkInfo(String apkPath) throws Exception {
        // apk信息的返回結果
        final String[] apkResult = new String[3];
        ZipFile zipFile = null;
        try {
            // 獲得一個解壓文件對象
            zipFile = new ZipFile(apkPath);
            // 將解壓文件對象轉列舉對象
            final Enumeration enumeration = zipFile.entries();
            ZipEntry zipEntry = null;
            // 遍歷列舉對象元素
            while (enumeration.hasMoreElements()) {
                // 獲得一個解壓條目對象
                zipEntry = (ZipEntry) enumeration.nextElement();
                if (zipEntry.isDirectory()) {

                } else {
                    // 獲得名爲AndroidManifest.xml的文件
                    if ("AndroidManifest.xml".equalsIgnoreCase(zipEntry.getName())) {
                        final AXmlResourceParser parser = new AXmlResourceParser();
                        parser.open(zipFile.getInputStream(zipEntry));
                        // 遍歷文件裏的內容
                        while (true) {
                            final int type = parser.next();
                            if (type == XmlPullParser.END_DOCUMENT) {
                                break;
                            }
                            switch (type) {
                                // 滿足條件開始遍歷內容提取需要的信息
                                case XmlPullParser.START_TAG: {
                                    for (int i = 0; i != parser.getAttributeCount(); ++i) {
                                        if ("package".equals(parser.getAttributeName(i))) {
                                            apkResult[0] = ApkUtil.getAttributeValue(parser, i);
                                        } else if ("versionCode".equals(parser.getAttributeName(i))) {
                                            apkResult[1] = ApkUtil.getAttributeValue(parser, i);
                                        } else if ("versionName".equals(parser.getAttributeName(i))) {
                                            apkResult[2] = ApkUtil.getAttributeValue(parser, i);
                                        }

                                    }
                                }
                            }
                        }
                    }

                }
            }
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (final IOException e) {
                    LOGGER.error("Zipfile close fail.", e);
                }
            }
        }

        return apkResult;
    }

    private static String getAttributeValue(AXmlResourceParser parser, int index) {
        final int type = parser.getAttributeValueType(index);
        final int data = parser.getAttributeValueData(index);
        if (type == TypedValue.TYPE_STRING) {
            return parser.getAttributeValue(index);
        }
        if (type == TypedValue.TYPE_ATTRIBUTE) {
            return String.format("?%s%08X", ApkUtil.getPackage(data), data);
        }
        if (type == TypedValue.TYPE_REFERENCE) {
            return String.format("@%s%08X", ApkUtil.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(ApkUtil.complexToFloat(data))
                    + ApkUtil.DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
        }
        if (type == TypedValue.TYPE_FRACTION) {
            return Float.toString(ApkUtil.complexToFloat(data))
                    + ApkUtil.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 "";
    }

    public static float complexToFloat(int complex) {
        return (complex & 0xFFFFFF00) * ApkUtil.RADIX_MULTS[complex >> 4 & 3];
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章