#!/usr/bin/env ruby require 'emu_ctl' require 'rubygems' require 'open3' # # starts up android emulator on headless unix machine # can only handle 1 emulator at a time # needs adb, android and emulator on the path (from Android SDK) # usage = " Usage: #{File.basename($0)} list prints all available emulators #{File.basename($0)} running shows all running emulators #{File.basename($0)} kill (ID|all) kills the emulator with ID (see `running`) or all runnig emulators #{File.basename($0)} launch ID runs the emulator with ID, ID is an integer form the output of `list` #{File.basename($0)} targets shows all possible targets and skins, will only display targets with abi #{File.basename($0)} new ID SKIN creates a new avd with API level according to ID and skin SKIN check `targets` for possible values (depends on installed sdk packages) Only works for targets with default abi #{File.basename($0)} rm ID deletes avd with ID returned in list #{File.basename($0)} force-kill sends `kill -9` to all running emulator processes, BE CAREFUL" # command line args if ARGV.empty? puts "You need to specify an action" puts "#{usage}" exit 2 end action = ARGV.shift case action when 'kill' arg = ARGV.shift raise 'you need to specify which emulator to kill' if arg.nil? running = emus = EmuCtl::ADB.devices targets = arg == 'all' ? running : [ running[arg.to_i] ] puts "trying to kill emulators: \n\t#{targets.join('\n\t')}" targets.each{ |t| EmuCtl::ADB.kill_emu(t.qualifier)} puts "done killing" when 'force-kill' warn "dont' use `force-kill` if you don't have to, it might coase android to leave some trash in /tmp/android-[user]/" pids = EmuCtl::Ctl.running_pids puts "found running emulators with pid: #{pids}" EmuCtl::Ctl.kill_pids(pids) puts "killed all emulators" when 'list' avds = EmuCtl::Ctl.list puts avds.map{ |o| "#{avds.index(o)} - #{o.to_s}" }.join("\n") when 'launch' raise "you need to specify the emulator id\n#{usage}" if ARGV.empty? index = ARGV.shift.to_i avd = EmuCtl::Ctl.list[index] EmuCtl::Ctl.start(avd) when 'targets' targets = EmuCtl::Ctl.list_targets puts targets.map{ |o| "#{targets.index(o)} - #{o.to_s}" }.join("\n") when 'new' target_index = ARGV.shift.to_i skin = ARGV.shift raise "you need to specify target and skin" if target_index.nil? || skin.nil? target = EmuCtl::Ctl.list_targets[target_index] puts "creating new avd from #{target.id} with skin #{skin}" EmuCtl::Ctl.create(target, skin) when 'running' emus = EmuCtl::ADB.devices puts emus.map{ |o| "#{emus.index(o)} - #{o.to_s}" }.join("\n") when 'rm' emu_index = ARGV.shift raise 'you need to specify the emulator id' if emu_index.nil? avd = EmuCtl::Ctl.list[Integer(emu_index)] puts "deleting: #{avd}" EmuCtl::Ctl.delete(avd) else puts "unknown action #{action}\n#{usage}" end exit 0