Improve error message for INSTALL_FAILED_OLDER_SDK from adb.
Clean up some tests.

--
MOS_MIGRATED_REVID=103600539
diff --git a/tools/android/incremental_install.py b/tools/android/incremental_install.py
index 04da8cd..c49f2ec 100644
--- a/tools/android/incremental_install.py
+++ b/tools/android/incremental_install.py
@@ -106,6 +106,10 @@
   """Raised when there is a problem with timestamp reading/writing."""
 
 
+class OldSdkException(Exception):
+  """Raised when the SDK on the target device is older than the app allows."""
+
+
 class Adb(object):
   """A class to handle interaction with adb."""
 
@@ -156,6 +160,8 @@
       # "error: " to the beginning, so take it off so that we don't end up
       # printing "Error: error: ..."
       raise MultipleDevicesError(re.sub("^error: ", "", stderr))
+    elif "INSTALL_FAILED_OLDER_SDK" in stdout:
+      raise OldSdkException()
 
     if adb.returncode != 0:
       raise AdbError(args, adb.returncode, stdout, stderr)
@@ -729,9 +735,12 @@
     sys.exit("Error: Device unauthorized. Please check the confirmation "
              "dialog on your device.")
   except MultipleDevicesError as e:
-    sys.exit(
-        "Error: " + e.message + "\nTry specifying a device serial with " +
-        "\"blaze mobile-install --adb_arg=-s --adb_arg=$ANDROID_SERIAL\"")
+    sys.exit("Error: " + e.message + "\nTry specifying a device serial with "
+             "\"blaze mobile-install --adb_arg=-s --adb_arg=$ANDROID_SERIAL\"")
+  except OldSdkException as e:
+    sys.exit("Error: The device does not support the API level specified in "
+             "the application's manifest. Check minSdkVersion in "
+             "AndroidManifest.xml")
   except TimestampException as e:
     sys.exit("Error:\n%s" % e.message)
   except AdbError as e:
diff --git a/tools/android/incremental_install_test.py b/tools/android/incremental_install_test.py
index e16a00b..773b348 100644
--- a/tools/android/incremental_install_test.py
+++ b/tools/android/incremental_install_test.py
@@ -489,8 +489,9 @@
     try:
       self._CallIncrementalInstall(incremental=True)
       self.fail("Should have quit if there is no device")
-    except SystemExit:
-      pass
+    except SystemExit as e:
+      # make sure it's the right SystemExit reason
+      self.assertTrue("Device not found" in str(e))
 
   def testUnauthorizedDevice(self):
     self._mock_adb.SetError(1, "", "device unauthorized. Please check the "
@@ -498,8 +499,9 @@
     try:
       self._CallIncrementalInstall(incremental=True)
       self.fail("Should have quit if the device is unauthorized.")
-    except SystemExit:
-      pass
+    except SystemExit as e:
+      # make sure it's the right SystemExit reason
+      self.assertTrue("Device unauthorized." in str(e))
 
   def testInstallFailure(self):
     self._mock_adb.SetError(0, "Failure", "", for_arg="install")
@@ -508,8 +510,9 @@
     try:
       self._CallIncrementalInstall(incremental=False)
       self.fail("Should have quit if the install failed.")
-    except SystemExit:
-      pass
+    except SystemExit as e:
+      # make sure it's the right SystemExit reason
+      self.assertTrue("Failure" in str(e))
 
   def testStartCold(self):
     # Based on testUploadToPristineDevice
@@ -579,12 +582,13 @@
         "more than one emulator",
     ]
     for error in errors:
-      self._mock_adb.SetError(255, "", error)
+      self._mock_adb.SetError(1, "", error)
       try:
         self._CallIncrementalInstall(incremental=True)
         self.fail("Should have quit if there were multiple devices.")
-      except SystemExit:
-        pass
+      except SystemExit as e:
+        # make sure it's the right SystemExit reason
+        self.assertTrue("Try specifying a device serial" in str(e))
 
   def testIncrementalInstallOnPristineDevice(self):
     self._CreateZip()
@@ -621,5 +625,18 @@
     except SystemExit:
       pass
 
+  def testSdkTooOld(self):
+    self._mock_adb.SetError(
+        0, "INSTALL_FAILED_OLDER_SDK", "", for_arg="install")
+    self._CreateZip()
+    self._CreateLocalManifest("zip1 zp1 ip1 0")
+    try:
+      self._CallIncrementalInstall(incremental=False)
+      self.fail("Should have quit if the SDK is too old.")
+    except SystemExit as e:
+      # make sure it's the right SystemExit reason
+      self.assertTrue("minSdkVersion" in str(e))
+
+
 if __name__ == "__main__":
   unittest.main()