Merge "Do tearDown iff we did setUp"
diff --git a/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java b/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
index 4dbf4ba..9c8714f 100644
--- a/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
+++ b/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
@@ -83,9 +83,12 @@
     private MetricsProcessor mMetricsProcessor;
     @Rule public TestMetrics mMetrics = new TestMetrics();
 
+    private boolean mNeedTearDown = false;
+
     @Before
     public void setUp() throws Exception {
         testIfDeviceIsCapable(getDevice());
+        mNeedTearDown = true;
 
         getDevice().installPackage(findTestFile(APK_NAME), /* reinstall */ false);
 
@@ -94,12 +97,10 @@
 
     @After
     public void tearDown() throws Exception {
-        try {
-            testIfDeviceIsCapable(getDevice());
-        } catch (Exception e) {
-            // Suppress execption here.
-            // If we throw exceptions in both setUp() and tearDown(),
-            // then test is reported as fail with org.junit.TestCouldNotBeSkippedException.
+        if (!mNeedTearDown) {
+            // If we skipped setUp, we don't need to undo it, and that avoids potential exceptions
+            // incompatible hardware. (Note that tests can change what testIfDeviceIsCapable()
+            // sees, so we can't rely on that - b/268688303.)
             return;
         }
         // Set PKVM enable and reboot to prevent previous staged session.