@Test public void testGetAndroidManifest() throws Exception { ByteBuffer manifest = getAndroidManifest("original.apk"); MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(manifest); byte[] actualDigest = md.digest(); assertEquals( "8b3de63a282652221162cdc327f424924ac3c7c24e642035975a1ee7a395c4dc", HexEncoding.encode(actualDigest)); }
public static ByteBuffer getAndroidManifest(DataSource apk) throws IOException, ApkFormatException { ZipSections zipSections; try { zipSections = findZipSections(apk); } catch (ZipFormatException e) { throw new ApkFormatException("Not a valid ZIP archive", e); } List<CentralDirectoryRecord> cdRecords = V1SchemeVerifier.parseZipCentralDirectory(apk, zipSections); CentralDirectoryRecord androidManifestCdRecord = null; for (CentralDirectoryRecord cdRecord : cdRecords) { if (ANDROID_MANIFEST_ZIP_ENTRY_NAME.equals(cdRecord.getName())) { androidManifestCdRecord = cdRecord; break; } } if (androidManifestCdRecord == null) { throw new ApkFormatException("Missing " + ANDROID_MANIFEST_ZIP_ENTRY_NAME); } DataSource lfhSection = apk.slice(0, zipSections.getZipCentralDirectoryOffset()); try { return ByteBuffer.wrap( LocalFileRecord.getUncompressedData( lfhSection, androidManifestCdRecord, lfhSection.size())); } catch (ZipFormatException e) { throw new ApkFormatException("Failed to read " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } }
ApkUtils { public static ByteBuffer getAndroidManifest(DataSource apk) throws IOException, ApkFormatException { ZipSections zipSections; try { zipSections = findZipSections(apk); } catch (ZipFormatException e) { throw new ApkFormatException("Not a valid ZIP archive", e); } List<CentralDirectoryRecord> cdRecords = V1SchemeVerifier.parseZipCentralDirectory(apk, zipSections); CentralDirectoryRecord androidManifestCdRecord = null; for (CentralDirectoryRecord cdRecord : cdRecords) { if (ANDROID_MANIFEST_ZIP_ENTRY_NAME.equals(cdRecord.getName())) { androidManifestCdRecord = cdRecord; break; } } if (androidManifestCdRecord == null) { throw new ApkFormatException("Missing " + ANDROID_MANIFEST_ZIP_ENTRY_NAME); } DataSource lfhSection = apk.slice(0, zipSections.getZipCentralDirectoryOffset()); try { return ByteBuffer.wrap( LocalFileRecord.getUncompressedData( lfhSection, androidManifestCdRecord, lfhSection.size())); } catch (ZipFormatException e) { throw new ApkFormatException("Failed to read " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } }
ApkUtils { public static ByteBuffer getAndroidManifest(DataSource apk) throws IOException, ApkFormatException { ZipSections zipSections; try { zipSections = findZipSections(apk); } catch (ZipFormatException e) { throw new ApkFormatException("Not a valid ZIP archive", e); } List<CentralDirectoryRecord> cdRecords = V1SchemeVerifier.parseZipCentralDirectory(apk, zipSections); CentralDirectoryRecord androidManifestCdRecord = null; for (CentralDirectoryRecord cdRecord : cdRecords) { if (ANDROID_MANIFEST_ZIP_ENTRY_NAME.equals(cdRecord.getName())) { androidManifestCdRecord = cdRecord; break; } } if (androidManifestCdRecord == null) { throw new ApkFormatException("Missing " + ANDROID_MANIFEST_ZIP_ENTRY_NAME); } DataSource lfhSection = apk.slice(0, zipSections.getZipCentralDirectoryOffset()); try { return ByteBuffer.wrap( LocalFileRecord.getUncompressedData( lfhSection, androidManifestCdRecord, lfhSection.size())); } catch (ZipFormatException e) { throw new ApkFormatException("Failed to read " + ANDROID_MANIFEST_ZIP_ENTRY_NAME, e); } } private  ApkUtils(); }
ApkUtils { public static ByteBuffer getAndroidManifest(DataSource apk) throws IOException, ApkFormatException { ZipSections zipSections; try { zipSections = findZipSections(apk); } catch (ZipFormatException e) { throw new ApkFormatException("Not a valid ZIP archive", e); } List<CentralDirectoryRecord> cdRecords = V1SchemeVerifier.parseZipCentralDirectory(apk, zipSections); CentralDirectoryRecord androidManifestCdRecord = null; for (CentralDirectoryRecord cdRecord : cdRecords) { if (ANDROID_MANIFEST_ZIP_ENTRY_NAME.equals(cdRecord.getName())) { androidManifestCdRecord = cdRecord; break; } } if (androidManifestCdRecord == null) { throw new ApkFormatException("Missing " + ANDROID_MANIFEST_ZIP_ENTRY_NAME); } DataSource lfhSection = apk.slice(0, zipSections.getZipCentralDirectoryOffset()); try { return ByteBuffer.wrap( LocalFileRecord.getUncompressedData( lfhSection, androidManifestCdRecord, lfhSection.size())); } catch (ZipFormatException e) { throw new ApkFormatException("Failed to read " + 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 ByteBuffer getAndroidManifest(DataSource apk) throws IOException, ApkFormatException { ZipSections zipSections; try { zipSections = findZipSections(apk); } catch (ZipFormatException e) { throw new ApkFormatException("Not a valid ZIP archive", e); } List<CentralDirectoryRecord> cdRecords = V1SchemeVerifier.parseZipCentralDirectory(apk, zipSections); CentralDirectoryRecord androidManifestCdRecord = null; for (CentralDirectoryRecord cdRecord : cdRecords) { if (ANDROID_MANIFEST_ZIP_ENTRY_NAME.equals(cdRecord.getName())) { androidManifestCdRecord = cdRecord; break; } } if (androidManifestCdRecord == null) { throw new ApkFormatException("Missing " + ANDROID_MANIFEST_ZIP_ENTRY_NAME); } DataSource lfhSection = apk.slice(0, zipSections.getZipCentralDirectoryOffset()); try { return ByteBuffer.wrap( LocalFileRecord.getUncompressedData( lfhSection, androidManifestCdRecord, lfhSection.size())); } catch (ZipFormatException e) { throw new ApkFormatException("Failed to read " + 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; }