Fix image_space_test on Q+ target.

This is a follow-up after
    https://android-review.googlesource.com/958079
to make image_space_test pass when DEX2OATBOOTCLASSPATH is
set, i.e. on a Q+ device.

Test: image_space_test
Bug: 122937705

(cherry picked from commit 3f795d2ab38ede7821db73904ddd1ddadb6bac4e)

Change-Id: I237ffd348a4466981b969ff1c7fc93a9b4a1dc44
Merged-In: If84cde5e68ae42aed7d2a402817436c19ee6fc21
diff --git a/runtime/gc/space/image_space_test.cc b/runtime/gc/space/image_space_test.cc
index 0a35bce..34df447 100644
--- a/runtime/gc/space/image_space_test.cc
+++ b/runtime/gc/space/image_space_test.cc
@@ -18,6 +18,7 @@
 
 #include "android-base/stringprintf.h"
 
+#include "base/stl_util.h"
 #include "dexopt_test.h"
 #include "noop_compiler_callbacks.h"
 
@@ -123,7 +124,28 @@
     // We want to test the relocation behavior of ImageSpace. As such, don't pretend we're a
     // compiler.
     callbacks_.reset();
+
+    // Clear DEX2OATBOOTCLASSPATH environment variable used for boot image compilation.
+    // We don't want that environment variable to affect the behavior of this test.
+    CHECK(old_dex2oat_bcp_ == nullptr);
+    const char* old_dex2oat_bcp = getenv("DEX2OATBOOTCLASSPATH");
+    if (old_dex2oat_bcp != nullptr) {
+      old_dex2oat_bcp_.reset(strdup(old_dex2oat_bcp));
+      CHECK(old_dex2oat_bcp_ != nullptr);
+      unsetenv("DEX2OATBOOTCLASSPATH");
+    }
   }
+
+  void TearDown() override {
+    if (old_dex2oat_bcp_ != nullptr) {
+      int result = setenv("DEX2OATBOOTCLASSPATH", old_dex2oat_bcp_.get(), /* replace */ 0);
+      CHECK_EQ(result, 0);
+      old_dex2oat_bcp_.reset();
+    }
+  }
+
+ private:
+  UniqueCPtr<const char[]> old_dex2oat_bcp_;
 };
 
 using ImageSpaceDex2oatTest = ImageSpaceLoadingTest<false, true, true>;