Class: Fastlane::Actions::AndroidEmulatorAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



164
165
166
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 164

def self.authors
  ["Michael Ruhl"]
end

.available_optionsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fastlane/plugin/android_emulator/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: :device,
                                  env_name: "AVD_DEVICE",
                                  description: "Device",
                                  default_value: "Nexus 5",
                                  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 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: :cold_boot,
                                 env_name: "AVD_COLD_BOOT",
                                 description: "Create a new AVD every run",
                                 is_string: false,
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :additional_options,
                                 env_name: "AVD_ADDITIONAL_OPTIONS",
                                 description: "Set additional options of the emulation",
                                 type: Array,
                                 is_string: false,
                                 optional: true)

  ]
end

.avd_active(params, config_file) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 7

def self.avd_active(params, config_file)
  image = params[:package].gsub(";", "/")
  if File.exist?(config_file)
    return File.readlines(config_file).grep(/#{image}/).size > 0
  else
    return false
  end
end

.descriptionObject



91
92
93
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 91

def self.description
  "Creates and starts an Android Emulator (AVD)"
end

.detailsObject



95
96
97
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 95

def self.details
  "Great for Screengrab"
end

.example_codeObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 99

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",
      additional_options: []
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 168

def self.is_supported?(platform)
  platform == :android
end

.run(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb', line 16

def self.run(params)
  sdk_dir = params[:sdk_dir]
  port = params[:port]
  adb = "#{sdk_dir}/platform-tools/adb"

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

  config_file = "#{Dir.home}/.android/avd/#{params[:name]}.avd/config.ini"

  if !avd_active(params, config_file) || params[:cold_boot]
    UI.message("Creating new emulator")
    FastlaneCore::CommandExecutor.execute(
      command: "#{sdk_dir}/cmdline-tools/latest/bin/avdmanager create avd -n '#{params[:name]}' -f -k '#{params[:package]}' -d '#{params[:device]}'",
      print_all: true,
      print_command: true
    )

    configuration = {}
    File.readlines(config_file).each do |definition|
      key, value = definition.split("=")
      configuration[key.strip] = value.strip
    end

    UI.message("Override configuration")
    additional = {
      "hw.gpu.mode" => "auto",
      "hw.gpu.enabled" => "yes",
      "skin.dynamic" => "yes",
      "skin.name" => "1080x1920"
    }

    if params[:additional_options]
      params[:additional_options].each do |option|
        key, value = option.split("=")
        additional[key.strip] = value.strip
      end
    end

    configuration = configuration.merge(additional)
    open(config_file, 'w') do |f|
      configuration.each { |key, value| f << "#{key.strip}=#{value.strip}\n" }
    end
  end

  # Verify HAXM installed on mac
  if FastlaneCore::Helper.mac?
    kextstat = Actions.sh("kextstat", log: false)

    UI.important("Please install the HAXM-Extension for better performance") unless kextstat.include?("intel")
  end

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

  until Actions.sh("#{adb} -e shell getprop dev.bootcomplete", log: false).strip == "1"
    sleep(5)
  end

  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