Snap for 8701348 from 55a4a561f793c69f7acb3105b240b60b30490a9f to mainline-cellbroadcast-release

Change-Id: I0ed31808695e001b4a858803d5bc1a0e8a0180a8
diff --git a/src/vogar/ModeId.java b/src/vogar/ModeId.java
index 613722a..c918104 100644
--- a/src/vogar/ModeId.java
+++ b/src/vogar/ModeId.java
@@ -35,31 +35,35 @@
     /**
      * $BOOTCLASSPATH for art+libcore only.
      * (Intended for use with dalvikvm only.)
-     * See TARGET_TEST_CORE_JARS in android/art/build/Android.common_path.mk
      */
     private static final String[] DEVICE_JARS = new String[] {
+            // ART module BCP libraries. See CORE_IMG_JARS in art/build/Android.common_path.mk.
             "core-oj",
             "core-libart",
-            "core-icu4j",
-            "conscrypt",
             "okhttp",
             "bouncycastle",
             "apache-xml",
+            // Stubs for dependencies from other APEX modules. If tests require it, this could use
+            // platform (xxx.module.platform.api.stubs) or even intra-core
+            // (xxx.module.intra.core.api.stubs) API stubs. However it's currently not necessary, so
+            // let's stick to public APIs for hygiene.
+            "i18n.module.public.api.stubs",
+            "conscrypt.module.public.api.stubs",
     };
 
     /**
      * $BOOTCLASSPATH for art+libcore only (host version).
      * (Intended for use with dalvikvm only.)
-     * See HOST_TEST_CORE_JARS in android/art/build/Android.common_path.mk
+     * See HOST_TEST_CORE_JARS in art/build/Android.common_path.mk
      */
     private static final String[] HOST_JARS = new String[] {
             "core-oj-hostdex",
             "core-libart-hostdex",
-            "core-icu4j-hostdex",
-            "conscrypt-hostdex",
             "okhttp-hostdex",
             "bouncycastle-hostdex",
             "apache-xml-hostdex",
+            "core-icu4j-hostdex",
+            "conscrypt-hostdex",
     };
 
     /**
diff --git a/src/vogar/Vogar.java b/src/vogar/Vogar.java
index 5a51ff4..59a0616 100644
--- a/src/vogar/Vogar.java
+++ b/src/vogar/Vogar.java
@@ -665,7 +665,8 @@
 
         AndroidSdk androidSdk = null;
         if (modeId.requiresAndroidSdk()) {
-            androidSdk = AndroidSdk.createAndroidSdk(console, mkdir, modeId, language);
+            androidSdk = AndroidSdk.createAndroidSdk(console, mkdir, modeId, language,
+                    !actionFiles.isEmpty());
         }
 
         if (runnerType == null) {
diff --git a/src/vogar/android/AndroidSdk.java b/src/vogar/android/AndroidSdk.java
index c8873c8..15c936d 100644
--- a/src/vogar/android/AndroidSdk.java
+++ b/src/vogar/android/AndroidSdk.java
@@ -70,7 +70,8 @@
      * compilation class path and android jar path.
      */
     public static AndroidSdk createAndroidSdk(
-            Log log, Mkdir mkdir, ModeId modeId, Language language) {
+            Log log, Mkdir mkdir, ModeId modeId, Language language,
+            boolean supportBuildFromSource) {
         List<String> path = new Command.Builder(log).args("which", ARBITRARY_BUILD_TOOL_NAME)
                 .permitNonZeroExitStatus(true)
                 .execute();
@@ -175,39 +176,43 @@
 
             desugarJarPath = desugarJar.getPath();
 
-            String pattern = outDir +
-                    "target/common/obj/JAVA_LIBRARIES/%s_intermediates/classes";
-            if (modeId.isHost()) {
-                pattern = outDir + "host/common/obj/JAVA_LIBRARIES/%s_intermediates/classes";
-            }
-            pattern += ".jar";
-
-            String[] jarNames = modeId.getJarNames();
-            compilationClasspath = new File[jarNames.length];
-            List<String> missingJars = new ArrayList<>();
-            for (int i = 0; i < jarNames.length; i++) {
-                String jar = jarNames[i];
-                File file;
+            if (!supportBuildFromSource) {
+                compilationClasspath = new File[]{};
+            } else {
+                String pattern = outDir +
+                        "target/common/obj/JAVA_LIBRARIES/%s_intermediates/classes";
                 if (modeId.isHost()) {
-                    if  ("conscrypt-hostdex".equals(jar)) {
-                        jar = "conscrypt-host-hostdex";
-                    } else if ("core-icu4j-hostdex".equals(jar)) {
-                        jar = "core-icu4j-host-hostdex";
-                    }
-                    file = new File(String.format(pattern, jar));
-                } else {
-                    file = findApexJar(jar, pattern);
-                    if (file.exists()) {
-                        log.verbose("Using jar " + jar + " from " + file);
-                    } else {
-                        missingJars.add(jar);
-                    }
+                    pattern = outDir + "host/common/obj/JAVA_LIBRARIES/%s_intermediates/classes";
                 }
-                compilationClasspath[i] = file;
-            }
-            if (!missingJars.isEmpty()) {
-                logMissingJars(log, missingJars);
-                throw new RuntimeException("Unable to locate all jars needed for compilation");
+                pattern += ".jar";
+
+                String[] jarNames = modeId.getJarNames();
+                compilationClasspath = new File[jarNames.length];
+                List<String> missingJars = new ArrayList<>();
+                for (int i = 0; i < jarNames.length; i++) {
+                    String jar = jarNames[i];
+                    File file;
+                    if (modeId.isHost()) {
+                        if  ("conscrypt-hostdex".equals(jar)) {
+                            jar = "conscrypt-host-hostdex";
+                        } else if ("core-icu4j-hostdex".equals(jar)) {
+                            jar = "core-icu4j-host-hostdex";
+                        }
+                        file = new File(String.format(pattern, jar));
+                    } else {
+                        file = findApexJar(jar, pattern);
+                        if (file.exists()) {
+                            log.verbose("Using jar " + jar + " from " + file);
+                        } else {
+                            missingJars.add(jar);
+                        }
+                    }
+                    compilationClasspath[i] = file;
+                }
+                if (!missingJars.isEmpty()) {
+                    logMissingJars(log, missingJars);
+                    throw new RuntimeException("Unable to locate all jars needed for compilation");
+                }
             }
         } else {
             throw new RuntimeException("Couldn't derive Android home from "
@@ -222,7 +227,9 @@
     private static void logMissingJars(Log log, List<String> missingJars) {
         StringBuilder makeCommand = new StringBuilder().append("m ");
         for (String jarName : missingJars) {
-            log.warn("Missing compilation jar " + jarName + " from APEX " + apexForJar(jarName));
+            String apex = apexForJar(jarName);
+            log.warn("Missing compilation jar " + jarName +
+                    (apex != null ? " from APEX " + apex : ""));
             makeCommand.append(jarName).append(" ");
         }
         log.info("Suggested make command: " + makeCommand);
@@ -230,10 +237,8 @@
 
     /** Returns the name of the APEX a particular jar might be located in */
     private static String apexForJar(String jar) {
-        if ("conscrypt".equals(jar)) {
-            return "com.android.conscrypt";
-        } else if ("core-icu4j".equals(jar)) {
-            return "com.android.i18n";
+        if (jar.endsWith(".api.stubs")) {
+            return null;  // API stubs aren't in any APEX.
         }
         return "com.android.art.testing";
     }
@@ -244,9 +249,12 @@
      * always non-null but possibly non-existent and so the caller should check.
      */
     private static File findApexJar(String jar, String filePattern) {
-        File file = new File(String.format(filePattern, jar + "." + apexForJar(jar)));
-        if (file.exists()) {
-            return file;
+        String apex = apexForJar(jar);
+        if (apex != null) {
+            File file = new File(String.format(filePattern, jar + "." + apex));
+            if (file.exists()) {
+                return file;
+            }
         }
         return new File(String.format(filePattern, jar));
     }