class CheckManifestInInstantRunMode extends AndroidVariantTask {

    @VisibleForTesting
    static void runManifestBinaryChangeVerifier(
            InstantRunBuildContext buildContext,
            File instantRunSupportDir,
            @NonNull File resOutBaseNameFile)
            throws IOException {
        // get the new manifest file CRC
        String currentIterationCRC = null;
        try (JarFile jarFile = new JarFile(resOutBaseNameFile)) {
            ZipEntry entry = jarFile.getEntry(SdkConstants.ANDROID_MANIFEST_XML);
            if (entry != null) {
                currentIterationCRC = String.valueOf(entry.getCrc());
            }
        }

        File crcFile = new File(instantRunSupportDir, "manifest.crc");
        // check the manifest file binary format.
        if (crcFile.exists() && currentIterationCRC != null) {
            // compare its content with the new binary file crc.
            String previousIterationCRC =
                    Files.asCharSource(crcFile, Charsets.UTF_8).readFirstLine();
            if (!currentIterationCRC.equals(previousIterationCRC)) {
                buildContext.setVerifierStatus(
                        InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE);
            }
        } else {
            // we don't have a back up of the crc file, better be safe and force the APK build.
            buildContext.setVerifierStatus(InstantRunVerifierStatus.INITIAL_BUILD);
        }

        if (currentIterationCRC != null) {
            // write the new manifest file CRC.
            Files.createParentDirs(crcFile);
            Files.asCharSink(crcFile, Charsets.UTF_8).write(currentIterationCRC);
        }
    }

}