Use command-line parsing in apiUsageSample

Extend build_r8lib.py so that it also runs the R8 command-line through
the API usage sample after linking with the minified r8lib.jar. This
ensures that we keep a large enough portion of the R8 API so that a user
could reimplement the R8 command-line.

Regenerate d8_api_usage_sample.jar and r8_api_usage_sample.jar by
running tools/gradle.py buildApiSampleJars.

Change-Id: I2342b395e07f2f9b6245ae746f47973948c39d57
diff --git a/build.gradle b/build.gradle
index cd831cc..f379bbf 100644
--- a/build.gradle
+++ b/build.gradle
@@ -111,7 +111,12 @@
     }
     apiUsageSample {
         java {
-            srcDirs = ['src/test/apiUsageSample']
+            srcDirs = ['src/test/apiUsageSample', 'src/main/java']
+            include 'com/android/tools/apiusagesample/*.java'
+            include 'com/android/tools/r8/BaseCompilerCommandParser.java'
+            include 'com/android/tools/r8/D8CommandParser.java'
+            include 'com/android/tools/r8/R8CommandParser.java'
+            include 'com/android/tools/r8/utils/FlagFile.java'
         }
     }
     debugTestResources {
diff --git a/tests/d8_api_usage_sample.jar b/tests/d8_api_usage_sample.jar
index f901b9d..7d503a5 100644
--- a/tests/d8_api_usage_sample.jar
+++ b/tests/d8_api_usage_sample.jar
Binary files differ
diff --git a/tests/r8_api_usage_sample.jar b/tests/r8_api_usage_sample.jar
index f901b9d..7d503a5 100644
--- a/tests/r8_api_usage_sample.jar
+++ b/tests/r8_api_usage_sample.jar
Binary files differ
diff --git a/tools/build_r8lib.py b/tools/build_r8lib.py
index e08c141..abaa619 100755
--- a/tools/build_r8lib.py
+++ b/tools/build_r8lib.py
@@ -40,7 +40,7 @@
 
 def test_d8sample():
   with utils.TempDir() as path:
-    args = ['java', '-cp', '%s:%s' % (R8LIB_JAR, SAMPLE_JAR),
+    args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, R8LIB_JAR),
             'com.android.tools.apiusagesample.D8ApiUsageSample',
             '--output', path,
             '--min-api', str(API_LEVEL),
@@ -52,12 +52,47 @@
     subprocess.check_call(args)
 
 
+def test_r8command():
+  with utils.TempDir() as path:
+    # SAMPLE_JAR and R8LIB_JAR should not have any classes in common, since e.g.
+    # R8CommandParser should have been minified in R8LIB_JAR.
+    # Just in case R8CommandParser is also present in R8LIB_JAR, we put
+    # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
+    args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, R8LIB_JAR),
+            'com.android.tools.r8.R8CommandParser',
+            '--output', path + "/output.zip",
+            '--min-api', str(API_LEVEL),
+            '--lib', ANDROID_JAR,
+            '--main-dex-list', '/dev/null',
+            os.path.join(utils.BUILD, 'test/examples/hello.jar')]
+    utils.PrintCmd(args)
+    subprocess.check_call(args)
+
+
+def test_r8cfcommand():
+  with utils.TempDir() as path:
+    # SAMPLE_JAR and R8LIB_JAR should not have any classes in common, since e.g.
+    # R8CommandParser should have been minified in R8LIB_JAR.
+    # Just in case R8CommandParser is also present in R8LIB_JAR, we put
+    # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
+    args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, R8LIB_JAR),
+            'com.android.tools.r8.R8CommandParser',
+            '--classfile',
+            '--output', path + "/output.jar",
+            '--lib', utils.RT_JAR,
+            os.path.join(utils.BUILD, 'test/examples/hello.jar')]
+    utils.PrintCmd(args)
+    subprocess.check_call(args)
+
+
 def main():
   # Handle --help
   parser.parse_args()
 
   build_r8lib()
   test_d8sample()
+  test_r8command()
+  test_r8cfcommand()
 
 
 if __name__ == '__main__':