@@ -695,12 +695,24 @@ def run_app_in_simulator(
695695 root_dir = os .path .dirname (application_output_path )
696696 register_dsyms (root_dir )
697697 with extracted_app (application_output_path , app_name ) as app_path :
698- logger .info ("Installing app %s to simulator %s" , app_path , simulator_udid )
698+ app_bundle_id = bundle_id (app_path )
699+ logger .info ("Will install app %s to simulator %s" , app_path , simulator_udid )
700+ # First, quietly kill any existing instances of the app to match Xcode's behavior.
701+ # Otherwise we've observed that the simulator gets confused when trying to re-install the app.
702+ logger .debug (
703+ "Terminating existing instances of %s in %s" , app_bundle_id , simulator_udid
704+ )
705+ subprocess .run (
706+ [simctl_path , "terminate" , simulator_udid , app_bundle_id ],
707+ stdout = subprocess .DEVNULL ,
708+ stderr = subprocess .DEVNULL ,
709+ )
710+ # We should now be able to install and run it.
711+ logger .debug ("Installing..." )
699712 subprocess .run (
700713 [simctl_path , "install" , simulator_udid , app_path ],
701714 check = True ,
702715 )
703- app_bundle_id = bundle_id (app_path )
704716 launch_args = shlex .split (
705717 os .environ .get (
706718 "BAZEL_SIMCTL_LAUNCH_FLAGS" ,
0 commit comments