class Fastlane::Actions::AndroidEmulatorKamiAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 163
def self.authors
  ["Michael Ruhl"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 111
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :sdk_dir,
                                 env_name: "ANDROID_SDK_DIR",
                                 description: "Path to the Android SDK DIR",
                                 default_value: ENV['ANDROID_HOME'] || ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_SDK'],
                                 optional: false,
                                 verify_block: proc do |value|
                                   UI.user_error!("No ANDROID_SDK_DIR given, pass using `sdk_dir: 'sdk_dir'`") unless value and !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :package,
                                env_name: "AVD_PACKAGE",
                                description: "The selected system image of the emulator",
                                optional: false),
    FastlaneCore::ConfigItem.new(key: :name,
                                 env_name: "AVD_NAME",
                                 description: "Name of the AVD",
                                 default_value: "fastlane",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :port,
                                 env_name: "AVD_PORT",
                                 description: "Port of the emulator",
                                 default_value: "5554",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :location,
                                 env_name: "AVD_LOCATION",
                                 description: "Set location of the the emulator '<longitude> <latitude>'",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :demo_mode,
                                 env_name: "AVD_DEMO_MODE",
                                 description: "Set the emulator in demo mode",
                                 is_string: false,
                                 default_value: true),
    FastlaneCore::ConfigItem.new(key: :resolution_width,
                                 env_name: "AVD_RESOLUTION_WIDTH",
                                 description: "Resolution of avd width",
                                 default_value: "1080",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :resolution_height,
                                 env_name: "AVD_RESOLUTION_HEIGHT",
                                 description: "Resolution of avd height",
                                 default_value: "1920",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :resolution_dpi,
                                 env_name: "AVD_RESOLUTION_DPI",
                                 description: "Device DPI",
                                 default_value: "480",
                                 optional: true)
     
  ]
end
description() click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 92
def self.description
  "Creates and starts an Android Emulator (AVD)"
end
details() click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 96
def self.details
  "Great for Screengrab"
end
example_code() click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 100
def self.example_code
  [
    'android_emulator(
      location: "9.1808 48.7771",
      package: "system-images;android-24;google_apis;x86_64",
      demo_mode: true,
      sdk_dir: "PATH_TO_SDK"
    )'
  ]
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 167
def self.is_supported?(platform)
  platform == :android
end
run(params) click to toggle source
# File lib/fastlane/plugin/android_emulator_kami/actions/android_emulator_action.rb, line 9
def self.run(params)
  sdk_dir = params[:sdk_dir]
  port = params[:port]
  adb = "#{sdk_dir}/platform-tools/adb"
  
  resolution_width  = params[:resolution_width] 
  resolution_height = params[:resolution_height]
  resolution_dpi = params[:resolution_dpi] 


  UI.message("Stopping emulator")
  system("#{adb} emu kill > /dev/null 2>&1 &")
  sleep(2)

  UI.message("Creating new emulator")
  FastlaneCore::CommandExecutor.execute(
    command: "#{sdk_dir}/tools/bin/avdmanager create avd -n '#{params[:name]}' -f -k '#{params[:package]}' -d 'Nexus 5'",
    print_all: true,
    print_command: false
  )

  UI.message("Override configuration")
  ini_path = "#{Dir.home}/.android/avd/#{params[:name]}.avd/config.ini"
  inifile = IniFile.load(ini_path)
  settings = inifile["global"]
  new_settings = {
    "hw.gpu.mode" => "host",
    "hw.gpu.enabled" => "yes",
    "skin.dynamic" => "yes",
    "hw.ramSize" => "2048",
    "skin.name" => "#{resolution_width}x#{resolution_height}",
    "hw.lcd.height" => resolution_height,
    "hw.lcd.width" => resolution_width,
    "hw.lcd.density" => resolution_dpi
  }
  new_ini = IniFile.new
  new_settings.each do |key, value|
      new_ini["global"][key] = value
  end
  merged_ini = inifile.merge new_ini
 
  #remove [global] section (first line)
  merged_ini_string = merged_ini.to_s.split("\n")[1..-1].join("\n")
  puts "New emulator configuration:\n"
  puts(merged_ini_string)
  File.write(ini_path, merged_ini_string)
  
  # Verify HAXM installed on mac
  if FastlaneCore::Helper.mac?
    kextstat = Actions.sh("kextstat", log: false)

    UI.user_error! "Please install the HAXM-Extension" unless kextstat.include?("intel")
  end

  UI.message("Starting emulator")
  emulator_executable = if File.file? "#{sdk_dir}/emulator/emulator"
                           "#{sdk_dir}/emulator/emulator"
                        else 
                          "#{sdk_dir}/emulator/tools"
                        end
  system("LC_NUMERIC=C; #{emulator_executable} @#{params[:name]} -port #{port} > /dev/null 2>&1 &")
  sh("#{adb} -e wait-for-device")

  until Actions.sh("#{adb} -e shell getprop init.svc.bootanim", log: false).include? "stopped" do
    sleep(5)
  end
  
  sleep(2)

  if params[:location]
    UI.message("Set location")
    sh("LC_NUMERIC=C; #{adb} emu geo fix #{params[:location]}")
  end

  if params[:demo_mode]
    UI.message("Set in demo mode")
    sh("#{adb} -e shell settings put global sysui_demo_allowed 1")
    sh("#{adb} -e shell am broadcast -a com.android.systemui.demo -e command clock -e hhmm 0700")
  end

  ENV['SCREENGRAB_SPECIFIC_DEVICE'] = "emulator-#{port}"
end