anchor.diff:      
       # We have to look for a free TCP port
       
  -    echo "Looking for a free TCP port in range 5554-5584"
  +    echo "Looking for a free TCP port in range 5554-5584" >&2
       
       for i in $(seq 5554 2 5584)
       do
  
positive.code:     
      if [ -z "$port" ]
      then
          echo "Unfortunately, the emulator port space is exhausted!"
          exit 1
      else
          echo "We have a free TCP port: $port"
      fi
      
      export ANDROID_SERIAL="emulator-$port"
  
negative.code:     ''}
      
      # Launch the emulator
      ${androidsdkComposition}/libexec/android-sdk-*/tools/emulator -avd device -no-boot-anim -port $port $NIX_ANDROID_EMULATOR_FLAGS &
  
      # Wait until the device has completely booted
      
      echo "Waiting until the emulator has booted the device and the package manager is ready..." >&2
      
      ${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port wait-for-device
      
      echo "Device state has been reached" >&2
      
      while [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell getprop dev.bootcomplete | grep 1)" ]
      do
          sleep 5
      done
      
      echo "dev.bootcomplete property is 1" >&2
      
      #while [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell getprop sys.boot_completed | grep 1)" ]
      #do
          #sleep 5
      #done
      
      #echo "sys.boot_completed property is 1" >&2
      
      echo "ready" >&2
      
      ${stdenv.lib.optionalString (app != null) ''
        # Install the App through the debugger
        ${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port install ${app}/*.apk
      
        # Start the application
        ${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell am start -a android.intent.action.MAIN -n ${package}/.${activity}
      ''}
      EOF
      chmod +x $out/bin/run-test-emulator
    '';
  }
  