Merge "Ensure R8 is built when run_on_as_app.py is run"
diff --git a/src/main/java/com/android/tools/r8/GenerateMainDexList.java b/src/main/java/com/android/tools/r8/GenerateMainDexList.java
index 48c2cde..1be9b3a 100644
--- a/src/main/java/com/android/tools/r8/GenerateMainDexList.java
+++ b/src/main/java/com/android/tools/r8/GenerateMainDexList.java
@@ -10,6 +10,7 @@
 import com.android.tools.r8.graph.DexApplication;
 import com.android.tools.r8.graph.DexDefinition;
 import com.android.tools.r8.graph.GraphLense;
+import com.android.tools.r8.shaking.DiscardedChecker;
 import com.android.tools.r8.shaking.Enqueuer;
 import com.android.tools.r8.shaking.Enqueuer.AppInfoWithLiveness;
 import com.android.tools.r8.shaking.MainDexClasses;
@@ -72,6 +73,11 @@
         options.mainDexListConsumer.accept(String.join("\n", result), options.reporter);
       }
 
+      if (!mainDexRootSet.checkDiscarded.isEmpty()) {
+        new DiscardedChecker(
+            mainDexRootSet, mainDexClasses.getClasses(), appView.appInfo(), options)
+            .run();
+      }
       // Print -whyareyoukeeping results if any.
       if (whyAreYouKeepingConsumer != null) {
         for (DexDefinition definition : mainDexRootSet.reasonAsked) {
diff --git a/src/test/java/com/android/tools/r8/maindexlist/checkdiscard/MainDexListCheckDiscard.java b/src/test/java/com/android/tools/r8/maindexlist/checkdiscard/MainDexListCheckDiscard.java
index cd85cd4..16958a6 100644
--- a/src/test/java/com/android/tools/r8/maindexlist/checkdiscard/MainDexListCheckDiscard.java
+++ b/src/test/java/com/android/tools/r8/maindexlist/checkdiscard/MainDexListCheckDiscard.java
@@ -6,14 +6,22 @@
 
 import com.android.tools.r8.CompilationFailedException;
 import com.android.tools.r8.CompilationMode;
+import com.android.tools.r8.GenerateMainDexList;
+import com.android.tools.r8.GenerateMainDexListCommand;
 import com.android.tools.r8.OutputMode;
 import com.android.tools.r8.R8Command;
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.errors.Unreachable;
 import com.android.tools.r8.origin.Origin;
+import com.android.tools.r8.utils.ListUtils;
 import com.google.common.collect.ImmutableList;
+import java.util.List;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
 
 class HelloWorldMain {
   public static void main(String[] args) {
@@ -25,8 +33,29 @@
 
 class NonMainDexClass {}
 
+@RunWith(Parameterized.class)
 public class MainDexListCheckDiscard extends TestBase {
-  public void runTest(String checkDiscardRule, boolean shouldFail) throws Exception {
+  private enum Command {
+    R8,
+    Generator
+  }
+
+  private static final List<Class<?>> CLASSES =
+      ImmutableList.of(HelloWorldMain.class, MainDexClass.class, NonMainDexClass.class);
+
+  @Parameters(name = "{0}")
+  public static Object[] parameters() {
+    return Command.values();
+  }
+
+  private final Command command;
+
+  public MainDexListCheckDiscard(Command command) {
+    this.command = command;
+  }
+
+
+  public void runTestWithR8(String checkDiscardRule) throws Exception {
     R8Command command =
         ToolHelper.prepareR8CommandBuilder(
                 readClasses(HelloWorldMain.class, MainDexClass.class, NonMainDexClass.class))
@@ -39,8 +68,33 @@
             .setDisableTreeShaking(true)
             .setDisableMinification(true)
             .build();
+    ToolHelper.runR8(command);
+  }
+
+  public void runTestWithGenerator(String checkDiscardRule) throws Exception {
+    GenerateMainDexListCommand.Builder builder =
+        GenerateMainDexListCommand.builder()
+            .addProgramFiles(ListUtils.map(CLASSES, ToolHelper::getClassFileForTestClass))
+            .addLibraryFiles(ToolHelper.getDefaultAndroidJar())
+            .addMainDexRules(
+                ImmutableList.of(keepMainProguardConfiguration(HelloWorldMain.class)),
+                Origin.unknown())
+            .addMainDexRules(ImmutableList.of(checkDiscardRule), Origin.unknown());
+    GenerateMainDexList.run(builder.build());
+  }
+
+  public void runTest(String checkDiscardRule, boolean shouldFail) throws Exception {
     try {
-      ToolHelper.runR8(command);
+      switch (command) {
+        case R8:
+          runTestWithR8(checkDiscardRule);
+          break;
+        case Generator:
+          runTestWithGenerator(checkDiscardRule);
+          break;
+        default:
+          throw new Unreachable();
+      }
     } catch (CompilationFailedException e) {
       Assert.assertTrue(shouldFail);
       return;