@Test public void testGetMinSdkVersionFromBinaryAndroidManifest() throws Exception { ByteBuffer manifest = getAndroidManifest("original.apk"); assertEquals(23, ApkUtils.getMinSdkVersionFromBinaryAndroidManifest(manifest)); }
public static int getMinSdkVersionFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws MinSdkVersionException { try { int result = 1; AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 2) && ("uses-sdk".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { int minSdkVersion = 1; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeNameResourceId(i) == MIN_SDK_VERSION_ATTR_ID) { int valueType = parser.getAttributeValueType(i); switch (valueType) { case AndroidBinXmlParser.VALUE_TYPE_INT: minSdkVersion = parser.getAttributeIntValue(i); break; case AndroidBinXmlParser.VALUE_TYPE_STRING: minSdkVersion = getMinSdkVersionForCodename( parser.getAttributeStringValue(i)); break; default: throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android" + ": unsupported value type in " + ANDROID_MANIFEST_ZIP_ENTRY_NAME + "'s" + " minSdkVersion" + ". Only integer values supported."); } break; } } result = Math.max(result, minSdkVersion); } eventType = parser.next(); } return result; } catch (AndroidBinXmlParser.XmlParserException e) { throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android platform version" + ": malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } }
ApkUtils { public static int getMinSdkVersionFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws MinSdkVersionException { try { int result = 1; AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 2) && ("uses-sdk".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { int minSdkVersion = 1; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeNameResourceId(i) == MIN_SDK_VERSION_ATTR_ID) { int valueType = parser.getAttributeValueType(i); switch (valueType) { case AndroidBinXmlParser.VALUE_TYPE_INT: minSdkVersion = parser.getAttributeIntValue(i); break; case AndroidBinXmlParser.VALUE_TYPE_STRING: minSdkVersion = getMinSdkVersionForCodename( parser.getAttributeStringValue(i)); break; default: throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android" + ": unsupported value type in " + ANDROID_MANIFEST_ZIP_ENTRY_NAME + "'s" + " minSdkVersion" + ". Only integer values supported."); } break; } } result = Math.max(result, minSdkVersion); } eventType = parser.next(); } return result; } catch (AndroidBinXmlParser.XmlParserException e) { throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android platform version" + ": malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } }
ApkUtils { public static int getMinSdkVersionFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws MinSdkVersionException { try { int result = 1; AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 2) && ("uses-sdk".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { int minSdkVersion = 1; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeNameResourceId(i) == MIN_SDK_VERSION_ATTR_ID) { int valueType = parser.getAttributeValueType(i); switch (valueType) { case AndroidBinXmlParser.VALUE_TYPE_INT: minSdkVersion = parser.getAttributeIntValue(i); break; case AndroidBinXmlParser.VALUE_TYPE_STRING: minSdkVersion = getMinSdkVersionForCodename( parser.getAttributeStringValue(i)); break; default: throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android" + ": unsupported value type in " + ANDROID_MANIFEST_ZIP_ENTRY_NAME + "'s" + " minSdkVersion" + ". Only integer values supported."); } break; } } result = Math.max(result, minSdkVersion); } eventType = parser.next(); } return result; } catch (AndroidBinXmlParser.XmlParserException e) { throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android platform version" + ": malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } private  ApkUtils(); }
ApkUtils { public static int getMinSdkVersionFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws MinSdkVersionException { try { int result = 1; AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 2) && ("uses-sdk".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { int minSdkVersion = 1; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeNameResourceId(i) == MIN_SDK_VERSION_ATTR_ID) { int valueType = parser.getAttributeValueType(i); switch (valueType) { case AndroidBinXmlParser.VALUE_TYPE_INT: minSdkVersion = parser.getAttributeIntValue(i); break; case AndroidBinXmlParser.VALUE_TYPE_STRING: minSdkVersion = getMinSdkVersionForCodename( parser.getAttributeStringValue(i)); break; default: throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android" + ": unsupported value type in " + ANDROID_MANIFEST_ZIP_ENTRY_NAME + "'s" + " minSdkVersion" + ". Only integer values supported."); } break; } } result = Math.max(result, minSdkVersion); } eventType = parser.next(); } return result; } catch (AndroidBinXmlParser.XmlParserException e) { throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android platform version" + ": malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } private  ApkUtils(); static ZipSections findZipSections(DataSource apk); static void setZipEocdCentralDirectoryOffset(
            ByteBuffer zipEndOfCentralDirectory, long offset); static ApkSigningBlock findApkSigningBlock(DataSource apk, ZipSections zipSections); static ByteBuffer getAndroidManifest(DataSource apk); static int getMinSdkVersionFromBinaryAndroidManifest(
            ByteBuffer androidManifestContents); static boolean getDebuggableFromBinaryAndroidManifest(
            ByteBuffer androidManifestContents); static String getPackageNameFromBinaryAndroidManifest(
            ByteBuffer androidManifestContents); }
ApkUtils { public static int getMinSdkVersionFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws MinSdkVersionException { try { int result = 1; AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 2) && ("uses-sdk".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { int minSdkVersion = 1; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeNameResourceId(i) == MIN_SDK_VERSION_ATTR_ID) { int valueType = parser.getAttributeValueType(i); switch (valueType) { case AndroidBinXmlParser.VALUE_TYPE_INT: minSdkVersion = parser.getAttributeIntValue(i); break; case AndroidBinXmlParser.VALUE_TYPE_STRING: minSdkVersion = getMinSdkVersionForCodename( parser.getAttributeStringValue(i)); break; default: throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android" + ": unsupported value type in " + ANDROID_MANIFEST_ZIP_ENTRY_NAME + "'s" + " minSdkVersion" + ". Only integer values supported."); } break; } } result = Math.max(result, minSdkVersion); } eventType = parser.next(); } return result; } catch (AndroidBinXmlParser.XmlParserException e) { throw new MinSdkVersionException( "Unable to determine APK's minimum supported Android platform version" + ": malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } private  ApkUtils(); static ZipSections findZipSections(DataSource apk); static void setZipEocdCentralDirectoryOffset(
            ByteBuffer zipEndOfCentralDirectory, long offset); static ApkSigningBlock findApkSigningBlock(DataSource apk, ZipSections zipSections); static ByteBuffer getAndroidManifest(DataSource apk); static int getMinSdkVersionFromBinaryAndroidManifest(
            ByteBuffer androidManifestContents); static boolean getDebuggableFromBinaryAndroidManifest(
            ByteBuffer androidManifestContents); static String getPackageNameFromBinaryAndroidManifest(
            ByteBuffer androidManifestContents); static final String ANDROID_MANIFEST_ZIP_ENTRY_NAME; }