Skip to content

Instantly share code, notes, and snippets.

@OSemenovBoyarka
Created March 28, 2017 09:05
Show Gist options
  • Save OSemenovBoyarka/c1e74f73402ab0ab511c57e404fe7cd0 to your computer and use it in GitHub Desktop.
Save OSemenovBoyarka/c1e74f73402ab0ab511c57e404fe7cd0 to your computer and use it in GitHub Desktop.
Couple methods to launch required android emulator using fastlane
# Set of lanes for control android emulators
require 'open3'
platform :android do
# time in seconds, to wait until emulator will finish launching
emulator_launch_timeout = 600 # 10 mins should be more, than enough for any relevant configuration
desc "Starts needed emulator, given though 'device_name' option, kills all others and deletes all redundant apks"
private_lane :prepare_emulator do |options|
device_name = options[:device_name]
# this action is info only, needed to distinguish, which emulator failed and not dig very deeply in logs
sh "echo 'Starting #{device_name}'"
adb_port = options[:adb_port]
device_serial = "emulator-#{adb_port}"
UI.message('Killing any existent emulators on same port')
# we don't care output of this command, emulator can be not started
kill_emulator(
device_serial: device_serial
)
# start emulator on separate process
# following http://stackoverflow.com/questions/21826527/installing-apk-via-adb-hangs-the-emulator
# TODO make ANDROID_HOME configurable from outside
start_avd = "#{ENV['ANDROID_HOME']}/emulator/emulator -port #{adb_port} -avd #{device_name} -wipe-data -no-boot-anim -partition-size 1536"
Process.fork do
sh(start_avd)
end
# waiting until emulator runs completely
# time in seconds between checks, if emulator came online
retry_interval = 10
tries_count = 0
loop do
# run with sys.boot_completed http://stackoverflow.com/questions/3634041/detect-when-android-emulator-is-fully-booted
boot_complete_cmd = "ANDROID_SERIAL=#{device_serial} adb shell getprop sys.boot_completed"
stdout, _stdeerr, _status = Open3.capture3(boot_complete_cmd)
output = stdout.to_s.strip
if output.eql?('1'.to_s)
UI.success('Emulator Booted!')
break
end
time_spent = tries_count * retry_interval
unless time_spent < emulator_launch_timeout
raise "Failed to start emulator #{device_name},
it didn't become online after #{time_spent} seconds,
last message was: #{output} "
end
UI.message('Waiting for emulator to complete boot')
tries_count += 1
sleep(retry_interval)
end
# disable animations
adb(command: 'shell settings put global window_animation_scale 0', serial: device_serial)
adb(command: 'shell settings put global transition_animation_scale 0', serial: device_serial)
adb(command: 'shell settings put global animator_duration_scale 0', serial: device_serial)
UI.success("Emulator #{device_name} boot up took #{tries_count * retry_interval} seconds")
end
desc 'Kills all emulators, started by this file'
private_lane :kill_emulator do |options|
device_serial = options[:device_serial]
Open3.capture3("ANDROID_SERIAL=#{device_serial} adb emu kill")
end
end
# More information about multiple platforms in fastlane:
# https://github.com/KrauseFx/fastlane/blob/master/docs/Platforms.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment