CheckManifestInInstantRunMode extends DefaultAndroidTask { @VisibleForTesting static void runManifestBinaryChangeVerifier( InstantRunBuildContext instantRunBuildContext, File instantRunSupportDir, @NonNull File resOutBaseNameFile) throws IOException { 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"); if (crcFile.exists() && currentIterationCRC != null) { String previousIterationCRC = Files.readFirstLine(crcFile, Charsets.UTF_8); if (!currentIterationCRC.equals(previousIterationCRC)) { instantRunBuildContext.setVerifierResult( InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE); } } else { instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus.INITIAL_BUILD); } if (currentIterationCRC != null) { Files.createParentDirs(crcFile); Files.write(currentIterationCRC, crcFile, Charsets.UTF_8); } }  File getInstantRunManifestFile(); @SuppressWarnings("unused") void setInstantRunManifestFile(File instantRunManifestFile); File getPackageOutputFile(); @SuppressWarnings("unused") void setPackageOutputFile(File packageOutputFile); @TaskAction void checkManifestChanges();  }
@Test public void runManifestBinaryChangeVerifier() throws IOException { File instantRunSupportDir = new File(mTemporaryFolder.newFolder(), "instant-run-support/debug"); File resOutBaseNameFile = mTemporaryFolder.newFile(); writeManifestFile(resOutBaseNameFile, "Initial binary manifest"); { InstantRunBuildContext context = mock(InstantRunBuildContext.class); CheckManifestInInstantRunMode.runManifestBinaryChangeVerifier( context, instantRunSupportDir, resOutBaseNameFile); verify(context).setVerifierResult(InstantRunVerifierStatus.INITIAL_BUILD); } { InstantRunBuildContext context = mock(InstantRunBuildContext.class); writeManifestFile(resOutBaseNameFile, "Initial binary manifest"); CheckManifestInInstantRunMode.runManifestBinaryChangeVerifier( context, instantRunSupportDir, resOutBaseNameFile); verifyNoMoreInteractions(context); } { InstantRunBuildContext context = mock(InstantRunBuildContext.class); writeManifestFile(resOutBaseNameFile, "Changed binary manifest"); CheckManifestInInstantRunMode.runManifestBinaryChangeVerifier( context, instantRunSupportDir, resOutBaseNameFile); verify(context).setVerifierResult( InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE); } { InstantRunBuildContext context = mock(InstantRunBuildContext.class); writeManifestFile(resOutBaseNameFile, "Changed binary manifest"); CheckManifestInInstantRunMode.runManifestBinaryChangeVerifier( context, instantRunSupportDir, resOutBaseNameFile); verifyNoMoreInteractions(context); } }