Cleanup unexpected files even if compilation is not required.

aosp/1880668 introduced a problem that odrefresh no longer cleans up
artifacts in /data when com.android.art is rolled back to the factory
installation. This CL fixes it.

Bug: 203198541
Test: atest odsign_e2e_tests
Change-Id: I81990ec35bd16c409fd924b856e2a37e95a7ad11
diff --git a/odrefresh/odrefresh.cc b/odrefresh/odrefresh.cc
index 822e4f5..d9c5f6f 100644
--- a/odrefresh/odrefresh.cc
+++ b/odrefresh/odrefresh.cc
@@ -1166,7 +1166,6 @@
 
   InstructionSet system_server_isa = config_.GetSystemServerIsa();
   std::vector<std::string> checked_artifacts;
-  checked_artifacts.push_back(cache_info_filename_);
 
   for (const InstructionSet isa : config_.GetBootExtensionIsas()) {
     if (!CheckBootExtensionArtifactsAreUpToDate(
@@ -1190,15 +1189,21 @@
   bool compilation_required = (!compilation_options->compile_boot_extensions_for_isas.empty() ||
                                !compilation_options->system_server_jars_to_compile.empty());
 
-  if (compilation_required) {
-    if (!config_.GetPartialCompilation()) {
-      return cleanup_and_compile_all();
-    }
-    Result<void> result = CleanupArtifactDirectory(checked_artifacts);
-    if (!result.ok()) {
-      LOG(ERROR) << result.error();
-      return ExitCode::kCleanupFailed;
-    }
+  // If partial compilation is disabled, we should compile everything regardless of what's in
+  // `compilation_options`.
+  if (compilation_required && !config_.GetPartialCompilation()) {
+    return cleanup_and_compile_all();
+  }
+
+  // We should only keep the cache info if we have artifacts on /data.
+  if (!checked_artifacts.empty()) {
+    checked_artifacts.push_back(cache_info_filename_);
+  }
+
+  Result<void> result = CleanupArtifactDirectory(checked_artifacts);
+  if (!result.ok()) {
+    LOG(ERROR) << result.error();
+    return ExitCode::kCleanupFailed;
   }
 
   return compilation_required ? ExitCode::kCompilationRequired : ExitCode::kOkay;
diff --git a/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java b/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java
index 94d5a19..c176a0c 100644
--- a/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java
+++ b/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java
@@ -16,6 +16,7 @@
 
 package com.android.tests.odsign;
 
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import com.android.tradefed.invoker.TestInformation;
@@ -134,6 +135,15 @@
         assertArtifactsNotModifiedAfter(sSystemServerArtifacts, timeMs);
     }
 
+    @Test
+    public void verifyUnexpectedFilesAreCleanedUp() throws Exception {
+        String unexpected = OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME + "/unexpected";
+        getDevice().pushString(/*contents=*/"", unexpected);
+        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+
+        assertFalse(getDevice().doesFileExist(unexpected));
+    }
+
     /**
      * Checks the input line by line and replaces all lines that match the regex with the given
      * replacement.