@Test public void testGetPackageNameFromBinaryAndroidManifest() throws Exception { ByteBuffer manifest = getAndroidManifest("original.apk"); assertEquals( "android.appsecurity.cts.tinyapp", ApkUtils.getPackageNameFromBinaryAndroidManifest(manifest)); }
public static String getPackageNameFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws ApkFormatException { try { AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 1) && ("manifest".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { for (int i = 0; i < parser.getAttributeCount(); i++) { if ("package".equals(parser.getAttributeName(i)) && (parser.getNamespace().isEmpty())) { return parser.getAttributeStringValue(i); } } return null; } eventType = parser.next(); } return null; } catch (AndroidBinXmlParser.XmlParserException e) { throw new ApkFormatException( "Unable to determine APK package name: malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } }
ApkUtils { public static String getPackageNameFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws ApkFormatException { try { AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 1) && ("manifest".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { for (int i = 0; i < parser.getAttributeCount(); i++) { if ("package".equals(parser.getAttributeName(i)) && (parser.getNamespace().isEmpty())) { return parser.getAttributeStringValue(i); } } return null; } eventType = parser.next(); } return null; } catch (AndroidBinXmlParser.XmlParserException e) { throw new ApkFormatException( "Unable to determine APK package name: malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } }
ApkUtils { public static String getPackageNameFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws ApkFormatException { try { AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 1) && ("manifest".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { for (int i = 0; i < parser.getAttributeCount(); i++) { if ("package".equals(parser.getAttributeName(i)) && (parser.getNamespace().isEmpty())) { return parser.getAttributeStringValue(i); } } return null; } eventType = parser.next(); } return null; } catch (AndroidBinXmlParser.XmlParserException e) { throw new ApkFormatException( "Unable to determine APK package name: malformed binary resource: " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } private  ApkUtils(); }
ApkUtils { public static String getPackageNameFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws ApkFormatException { try { AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 1) && ("manifest".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { for (int i = 0; i < parser.getAttributeCount(); i++) { if ("package".equals(parser.getAttributeName(i)) && (parser.getNamespace().isEmpty())) { return parser.getAttributeStringValue(i); } } return null; } eventType = parser.next(); } return null; } catch (AndroidBinXmlParser.XmlParserException e) { throw new ApkFormatException( "Unable to determine APK package name: 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 String getPackageNameFromBinaryAndroidManifest( ByteBuffer androidManifestContents) throws ApkFormatException { try { AndroidBinXmlParser parser = new AndroidBinXmlParser(androidManifestContents); int eventType = parser.getEventType(); while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) { if ((eventType == AndroidBinXmlParser.EVENT_START_ELEMENT) && (parser.getDepth() == 1) && ("manifest".equals(parser.getName())) && (parser.getNamespace().isEmpty())) { for (int i = 0; i < parser.getAttributeCount(); i++) { if ("package".equals(parser.getAttributeName(i)) && (parser.getNamespace().isEmpty())) { return parser.getAttributeStringValue(i); } } return null; } eventType = parser.next(); } return null; } catch (AndroidBinXmlParser.XmlParserException e) { throw new ApkFormatException( "Unable to determine APK package name: 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; }