CheckManifestInInstantRunMode extends DefaultAndroidTask { @VisibleForTesting static void runManifestChangeVerifier(InstantRunBuildContext instantRunBuildContext, File instantRunSupportDir, @NonNull File manifestFileToPackage) throws IOException { File previousManifestFile = new File(instantRunSupportDir, "manifest.xml"); if (previousManifestFile.exists()) { String currentManifest = Files.asCharSource(manifestFileToPackage, Charsets.UTF_8).read(); String previousManifest = Files.asCharSource(previousManifestFile, Charsets.UTF_8).read(); if (!currentManifest.equals(previousManifest)) { instantRunBuildContext.setVerifierResult( InstantRunVerifierStatus.MANIFEST_FILE_CHANGE); Files.copy(manifestFileToPackage, previousManifestFile); } } else { Files.createParentDirs(previousManifestFile); Files.copy(manifestFileToPackage, previousManifestFile); instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus.INITIAL_BUILD); } }  File getInstantRunManifestFile(); @SuppressWarnings("unused") void setInstantRunManifestFile(File instantRunManifestFile); File getPackageOutputFile(); @SuppressWarnings("unused") void setPackageOutputFile(File packageOutputFile); @TaskAction void checkManifestChanges();  }
@Test public void runManifestChangeVerifier() throws IOException { File instantRunSupportDir = new File(mTemporaryFolder.newFolder(), "instant-run-support/debug"); File manifestFileToPackage = mTemporaryFolder.newFile(); { InstantRunBuildContext context = mock(InstantRunBuildContext.class); Files.write("Original Manifest", manifestFileToPackage, Charsets.UTF_8); CheckManifestInInstantRunMode.runManifestChangeVerifier( context, instantRunSupportDir, manifestFileToPackage); verify(context).setVerifierResult(InstantRunVerifierStatus.INITIAL_BUILD); } { InstantRunBuildContext context = mock(InstantRunBuildContext.class); Files.write("Original Manifest", manifestFileToPackage, Charsets.UTF_8); CheckManifestInInstantRunMode.runManifestChangeVerifier( context, instantRunSupportDir, manifestFileToPackage); verifyNoMoreInteractions(context); } { InstantRunBuildContext context = mock(InstantRunBuildContext.class); Files.write("Changed Manifest", manifestFileToPackage, Charsets.UTF_8); CheckManifestInInstantRunMode.runManifestChangeVerifier( context, instantRunSupportDir, manifestFileToPackage); verify(context).setVerifierResult(InstantRunVerifierStatus.MANIFEST_FILE_CHANGE); } { InstantRunBuildContext context = mock(InstantRunBuildContext.class); Files.write("Changed Manifest", manifestFileToPackage, Charsets.UTF_8); CheckManifestInInstantRunMode.runManifestChangeVerifier( context, instantRunSupportDir, manifestFileToPackage); verifyNoMoreInteractions(context); } }