Start loading libandroid_runtime.so

Enable android.util.Log's native code

Test: ./ravenwood/run-ravenwood-tests.sh
Bug: 318393625

Change-Id: I0ec607586d71b1cc34b71db67249e110edc304df
diff --git a/Ravenwood.bp b/Ravenwood.bp
index 6337022..c5c8d15 100644
--- a/Ravenwood.bp
+++ b/Ravenwood.bp
@@ -129,6 +129,9 @@
         "mockito-ravenwood-prebuilt",
         "inline-mockito-ravenwood-prebuilt",
     ],
+    jni_libs: [
+        "libandroid_runtime",
+    ],
 }
 
 android_ravenwood_libgroup {
diff --git a/core/java/android/util/Log.java b/core/java/android/util/Log.java
index b33214d..8358b9a 100644
--- a/core/java/android/util/Log.java
+++ b/core/java/android/util/Log.java
@@ -72,8 +72,11 @@
  * a positive value may be considered as a successful invocation.
  */
 @android.ravenwood.annotation.RavenwoodKeepWholeClass
-@android.ravenwood.annotation.RavenwoodNativeSubstitutionClass(
-        "com.android.platform.test.ravenwood.nativesubstitution.Log_host")
+@android.ravenwood.annotation.RavenwoodClassLoadHook(
+        "com.android.platform.test.ravenwood.runtimehelper.ClassLoadHook.onClassLoaded")
+// Uncomment the following annotation to switch to the Java substitution version.
+//@android.ravenwood.annotation.RavenwoodNativeSubstitutionClass(
+//        "com.android.platform.test.ravenwood.nativesubstitution.Log_host")
 public final class Log {
     /** @hide */
     @IntDef({ASSERT, ERROR, WARN, INFO, DEBUG, VERBOSE})
diff --git a/ravenwood/Android.bp b/ravenwood/Android.bp
index 132804f..674d746 100644
--- a/ravenwood/Android.bp
+++ b/ravenwood/Android.bp
@@ -47,6 +47,7 @@
     ],
     libs: [
         "framework-minus-apex.ravenwood",
+        "ravenwood-junit",
     ],
     visibility: ["//visibility:private"],
 }
diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodUtils.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodUtils.java
index b736a76..37ceac6 100644
--- a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodUtils.java
+++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodUtils.java
@@ -73,9 +73,9 @@
                     return;
                 }
             }
-            throw new UnsatisfiedLinkError("Library " + libname + " no found in "
+            throw new UnsatisfiedLinkError("Library " + libname + " not found in "
                     + "java.library.path: " + path);
-        } catch (Exception e) {
+        } catch (Throwable e) {
             dumpFiles(System.out);
             throw e;
         }
@@ -96,6 +96,10 @@
                     listFiles(out, gparent, "");
                 }
             }
+
+            var gparent = new File("../..").getCanonicalFile();
+            out.println("# ../..=" + gparent);
+            listFiles(out, gparent, "");
         } catch (Throwable th) {
             out.println("Error: " + th.toString());
             th.printStackTrace(out);
diff --git a/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/nativesubstitution/Log_host.java b/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/nativesubstitution/Log_host.java
index 5930a14..f301b9c 100644
--- a/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/nativesubstitution/Log_host.java
+++ b/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/nativesubstitution/Log_host.java
@@ -22,6 +22,13 @@
 
 import java.io.PrintStream;
 
+/**
+ * Ravenwood "native substitution" class for {@link android.util.Log}.
+ *
+ * {@link android.util.Log} already uses the actual native code and doesn't use this class.
+ * In order to switch to this Java implementation, uncomment the @RavenwoodNativeSubstitutionClass
+ * annotation on {@link android.util.Log}.
+ */
 public class Log_host {
 
     public static boolean isLoggable(String tag, @Level int level) {
diff --git a/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/runtimehelper/ClassLoadHook.java b/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/runtimehelper/ClassLoadHook.java
index 1e12030..cc94090 100644
--- a/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/runtimehelper/ClassLoadHook.java
+++ b/ravenwood/runtime-helper-src/framework/com/android/platform/test/ravenwood/runtimehelper/ClassLoadHook.java
@@ -15,8 +15,9 @@
  */
 package com.android.platform.test.ravenwood.runtimehelper;
 
+import android.platform.test.ravenwood.RavenwoodUtils;
+
 import java.io.File;
-import java.io.PrintStream;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 
@@ -27,8 +28,6 @@
  * load other JNI or do other set up here.
  */
 public class ClassLoadHook {
-    private static PrintStream sOut = System.out;
-
     /**
      * If true, we won't load `libandroid_runtime`
      *
@@ -36,7 +35,7 @@
      * so we need a way to remove the dependency.
      */
     private static final boolean SKIP_LOADING_LIBANDROID = "1".equals(System.getenv(
-            "HOSTTEST_SKIP_LOADING_LIBANDROID"));
+            "RAVENWOOD_SKIP_LOADING_LIBANDROID"));
 
     public static final String CORE_NATIVE_CLASSES = "core_native_classes";
     public static final String ICU_DATA_PATH = "icu.data.path";
@@ -44,7 +43,7 @@
     public static final String GRAPHICS_NATIVE_CLASSES = "graphics_native_classes";
 
     public static final String VALUE_N_A = "**n/a**";
-    public static final String LIBANDROID_RUNTIME_NAME = "libandroid_runtime";
+    public static final String LIBANDROID_RUNTIME_NAME = "android_runtime";
 
     private static String sInitialDir = new File("").getAbsolutePath();
 
@@ -68,7 +67,7 @@
     }
 
     private static void log(String message) {
-        sOut.println("ClassLoadHook: " + message);
+        System.out.println("ClassLoadHook: " + message);
     }
 
     private static void log(String fmt, Object... args) {
@@ -92,13 +91,6 @@
         }
     }
 
-    private static void loadJniLibrary(String name) {
-        final String path = sInitialDir + "/lib64/" + name + ".so";
-        System.out.println("Loading " + path + " ...");
-        System.load(path);
-        System.out.println("Done loading " + path);
-    }
-
     private static boolean sLoadFrameworkNativeCodeCalled = false;
 
     /**
@@ -115,7 +107,7 @@
         // libandroid_runtime uses Java's system properties to decide what JNI methods to set up.
         // Set up these properties for host-side tests.
 
-        if ("1".equals(System.getenv("HOSTTEST_DUMP_PROPERTIES"))) {
+        if ("1".equals(System.getenv("RAVENWOOD_DUMP_PROPERTIES"))) {
             log("Java system properties:");
             dumpSystemProperties();
         }
@@ -141,7 +133,7 @@
         setProperty(ICU_DATA_PATH, VALUE_N_A);
         setProperty(KEYBOARD_PATHS, VALUE_N_A);
 
-        loadJniLibrary(LIBANDROID_RUNTIME_NAME);
+        RavenwoodUtils.loadJniLibrary(LIBANDROID_RUNTIME_NAME);
     }
 
     /**
@@ -156,7 +148,7 @@
     };
 
     /**
-     * @return if a given method is a native method or not.
+     * @return if a given class has any native method or not.
      */
     private static boolean hasNativeMethod(Class<?> clazz) {
         for (var method : clazz.getDeclaredMethods()) {