Make 674-hiddenapi more deterministic

This does not seem to make the order of output line completely
deterministic, but with the new patch the test should simply
print nothing.

Bug: 64382372
Test: art/test.py --host -b -r -t 674-hiddenapi
Change-Id: I18bb76a0247b5b274e783d293aeb01e3b665a78d
diff --git a/test/674-hiddenapi/check b/test/674-hiddenapi/check
index 660643f..c319a0a 100644
--- a/test/674-hiddenapi/check
+++ b/test/674-hiddenapi/check
@@ -15,6 +15,9 @@
 # limitations under the License.
 
 # Remove pid and date from the log messages.
-grep -vE '^dalvikvm(32|64) E [^]]+]' "$2" | grep -v JNI_OnUnload > "$2.tmp"
+grep -vE '^dalvikvm(32|64) E [^]]+]' "$2" \
+    | grep -v JNI_OnLoad \
+    | grep -v JNI_OnUnload \
+    > "$2.tmp"
 
 ./default-check "$1" "$2.tmp"
diff --git a/test/674-hiddenapi/expected.txt b/test/674-hiddenapi/expected.txt
index 727a061..e69de29 100644
--- a/test/674-hiddenapi/expected.txt
+++ b/test/674-hiddenapi/expected.txt
@@ -1,5 +0,0 @@
-JNI_OnLoad called
-JNI_OnLoad called
-JNI_OnLoad called
-JNI_OnLoad called
-Done
diff --git a/test/674-hiddenapi/src-art/Main.java b/test/674-hiddenapi/src-art/Main.java
index 9773e0a..56a644f 100644
--- a/test/674-hiddenapi/src-art/Main.java
+++ b/test/674-hiddenapi/src-art/Main.java
@@ -35,18 +35,19 @@
     // The expectation is that hidden members in parent should be visible to
     // the child.
     doTest(false, false);
+    doUnloading();
 
     // Now append parent dex file to boot class path and run again. This time
     // the child dex file should not be able to access private APIs of the parent.
     appendToBootClassLoader(DEX_PARENT_BOOT);
     doTest(true, false);
+    doUnloading();
 
     // And finally append to child to boot class path as well. With both in the
     // boot class path, access should be granted.
     appendToBootClassLoader(DEX_CHILD);
     doTest(true, true);
-
-    System.out.println("Done");
+    doUnloading();
   }
 
   private static void doTest(boolean parentInBoot, boolean childInBoot) throws Exception {
@@ -129,6 +130,14 @@
     return tempFile.getAbsolutePath();
   }
 
+  private static void doUnloading() {
+    // Do multiple GCs to prevent rare flakiness if some other thread is keeping the
+    // classloader live.
+    for (int i = 0; i < 5; ++i) {
+       Runtime.getRuntime().gc();
+    }
+  }
+
   private static String nativeLibFileName;
 
   private static final String DEX_PARENT =