Skip to content

Commit 06a77d8

Browse files
authored
Merge pull request #409 from wordpress-mobile/screenshots/emulator-helper
Add Android AVD / emulator actions
2 parents 86a0511 + 0a94acf commit 06a77d8

File tree

7 files changed

+491
-12
lines changed

7 files changed

+491
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ _None_
1010

1111
### New Features
1212

13-
_None_
13+
- Add `android_create_avd`, `android_launch_emulator` and `android_shutdown_emulator` actions. [#409]
1414

1515
### Bug Fixes
1616

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module Fastlane
2+
module Actions
3+
class AndroidCreateAvdAction < Action
4+
def self.run(params)
5+
device_model = params[:device_model]
6+
api_level = params[:api_level]
7+
avd_name = params[:avd_name]
8+
sdcard = params[:sdcard]
9+
10+
helper = Fastlane::Helper::Android::EmulatorHelper.new
11+
12+
# Ensure we have the system image needed for creating the AVD with this API level
13+
system_image = params[:system_image] || helper.install_system_image(api: api_level)
14+
15+
# Create the AVD for device, API and system image we need
16+
helper.create_avd(
17+
api: api_level,
18+
device: device_model,
19+
system_image: system_image,
20+
name: avd_name,
21+
sdcard: sdcard
22+
)
23+
end
24+
25+
#####################################################
26+
# @!group Documentation
27+
#####################################################
28+
29+
def self.description
30+
'Creates a new Android Virtual Device (AVD) for a specific device model and API level'
31+
end
32+
33+
def self.details
34+
<<~DESC
35+
Creates a new Android Virtual Device (AVD) for a specific device model and API level.
36+
By default, it also installs the necessary system image (using `sdkmanager`) if needed before creating the AVD
37+
DESC
38+
end
39+
40+
def self.available_options
41+
[
42+
FastlaneCore::ConfigItem.new(key: :device_model,
43+
env_name: 'FL_ANDROID_CREATE_AVD_DEVICE_MODEL',
44+
description: 'The device model code to use to create the AVD. Valid values can be found using `avdmanager list devices`',
45+
type: String,
46+
optional: false),
47+
FastlaneCore::ConfigItem.new(key: :api_level,
48+
env_name: 'FL_ANDROID_CREATE_AVD_API_LEVEL',
49+
description: 'The API level to use to install the necessary system-image and create the AVD',
50+
type: Integer,
51+
optional: false),
52+
FastlaneCore::ConfigItem.new(key: :avd_name,
53+
env_name: 'FL_ANDROID_CREATE_AVD_AVD_NAME',
54+
description: 'The name to give to the created AVD. If not provided, will be derived from device model and API level',
55+
type: String,
56+
optional: true,
57+
default_value: nil),
58+
FastlaneCore::ConfigItem.new(key: :sdcard,
59+
env_name: 'FL_ANDROID_CREATE_AVD_SDCARD',
60+
description: 'The size of the SD card to use for the AVD',
61+
type: String,
62+
optional: true,
63+
default_value: '512M'),
64+
FastlaneCore::ConfigItem.new(key: :system_image,
65+
env_name: 'FL_ANDROID_CREATE_AVD_SYSTEM_IMAGE',
66+
description: 'The system image to use (as used/listed by `sdkmanager`). Defaults to the appropriate system image given the API level requested and the current machine\'s architecture',
67+
type: String,
68+
optional: true,
69+
default_value_dynamic: true,
70+
default_value: nil),
71+
]
72+
end
73+
74+
def self.output
75+
end
76+
77+
def self.return_value
78+
'Returns the name of the created AVD'
79+
end
80+
81+
def self.authors
82+
['Automattic']
83+
end
84+
85+
def self.is_supported?(platform)
86+
platform == :android
87+
end
88+
end
89+
end
90+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Fastlane
2+
module Actions
3+
class AndroidLaunchEmulatorAction < Action
4+
def self.run(params)
5+
helper = Fastlane::Helper::Android::EmulatorHelper.new
6+
helper.launch_avd(
7+
name: params[:avd_name],
8+
cold_boot: params[:cold_boot],
9+
wipe_data: params[:wipe_data]
10+
)
11+
end
12+
13+
#####################################################
14+
# @!group Documentation
15+
#####################################################
16+
17+
def self.description
18+
'Boots an Android emulator using the given AVD name'
19+
end
20+
21+
def self.details
22+
description
23+
end
24+
25+
def self.available_options
26+
[
27+
FastlaneCore::ConfigItem.new(key: :avd_name,
28+
env_name: 'FL_ANDROID_LAUNCH_EMULATOR_AVD_NAME',
29+
description: 'The name of the AVD to boot',
30+
type: String,
31+
optional: false),
32+
FastlaneCore::ConfigItem.new(key: :cold_boot,
33+
env_name: 'FL_ANDROID_LAUNCH_EMULATOR_COLD_BOOT',
34+
description: 'Indicate if we want a cold boot (true) of if we prefer booting from a snapshot (false)',
35+
type: Fastlane::Boolean,
36+
default_value: true),
37+
FastlaneCore::ConfigItem.new(key: :wipe_data,
38+
env_name: 'FL_ANDROID_LAUNCH_EMULATOR_WIPE_DATA',
39+
description: 'Indicate if we want to wipe the device data before booting the AVD, so it is like it were a brand new device',
40+
type: Fastlane::Boolean,
41+
default_value: true),
42+
]
43+
end
44+
45+
def self.output
46+
end
47+
48+
def self.return_value
49+
'The serial of the emulator that was created after booting the AVD (e.g. `emulator-5554`)'
50+
end
51+
52+
def self.authors
53+
['Automattic']
54+
end
55+
56+
def self.is_supported?(platform)
57+
platform == :android
58+
end
59+
end
60+
end
61+
end

lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_send_app_size_metrics.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,26 @@ def check_bundletool_installed!
7171
end
7272

7373
# The path where the `apkanalyzer` binary was found, after searching it:
74-
# - in priority in `$ANDROID_SDK_ROOT` (or `$ANDROID_HOME` for legacy setups), under `cmdline-tools/latest/bin/` or `cmdline-tools/tools/bin`
74+
# - in priority in `$ANDROID_HOME` (or `$ANDROID_SDK_ROOT` for legacy setups), under `cmdline-tools/latest/bin/` or `cmdline-tools/tools/bin`
7575
# - and falling back by trying to find it in `$PATH`
7676
#
7777
# @return [String,Nil] The path to `apkanalyzer`, or `nil` if it wasn't found in any of the above tested paths.
7878
#
7979
def find_apkanalyzer_binary
80-
sdk_root = ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_HOME']
81-
if sdk_root
82-
pattern = File.join(sdk_root, 'cmdline-tools', '{latest,tools}', 'bin', 'apkanalyzer')
83-
apkanalyzer_bin = Dir.glob(pattern).find { |path| File.executable?(path) }
84-
end
85-
apkanalyzer_bin || Action.sh('command', '-v', 'apkanalyzer', print_command_output: false) { |_| nil }
80+
@tools ||= Fastlane::Helper::Android::ToolsPathHelper.new
81+
@tools.find_tool_path(binary: 'apkanalyzer', search_paths: @tools.cmdline_tools_search_paths)
8682
end
8783

8884
# The path where the `apkanalyzer` binary was found, after searching it:
89-
# - in priority in `$ANDROID_SDK_ROOT` (or `$ANDROID_HOME` for legacy setups), under `cmdline-tools/latest/bin/` or `cmdline-tools/tools/bin`
85+
# - in priority in `$ANDROID_HOME` (or `$ANDROID_SDK_ROOT` for legacy setups), under `cmdline-tools/latest/bin/` or `cmdline-tools/tools/bin`
9086
# - and falling back by trying to find it in `$PATH`
9187
#
9288
# @return [String] The path to `apkanalyzer`
9389
# @raise [FastlaneCore::Interface::FastlaneError] if it wasn't found in any of the above tested paths.
9490
#
9591
def find_apkanalyzer_binary!
96-
apkanalyzer_bin = find_apkanalyzer_binary
97-
UI.user_error!('Unable to find `apkanalyzer` executable in either `$PATH` or `$ANDROID_SDK_ROOT`. Make sure you installed the Android SDK Command-line Tools') if apkanalyzer_bin.nil?
98-
apkanalyzer_bin
92+
@tools ||= Fastlane::Helper::Android::ToolsPathHelper.new
93+
@tools.find_tool_path!(binary: 'apkanalyzer', search_paths: @tools.cmdline_tools_search_paths)
9994
end
10095

10196
# Add the `file-size` and `download-size` values of an APK to the helper, as reported by the corresponding `apkanalyzer apk …` commands
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module Fastlane
2+
module Actions
3+
class AndroidShutdownEmulatorAction < Action
4+
def self.run(params)
5+
helper = Fastlane::Helper::Android::EmulatorHelper.new
6+
helper.shut_down_emulators!(serials: params[:serials])
7+
end
8+
9+
#####################################################
10+
# @!group Documentation
11+
#####################################################
12+
13+
def self.description
14+
'Shuts down Android emulators'
15+
end
16+
17+
def self.details
18+
description
19+
end
20+
21+
def self.available_options
22+
[
23+
FastlaneCore::ConfigItem.new(key: :serials,
24+
env_name: 'FL_ANDROID_SHUTDOWN_EMULATOR_SERIALS',
25+
description: 'The serial(s) of the emulators to shut down. If not provided (nil), will shut them all down',
26+
type: Array,
27+
optional: true,
28+
default_value: nil),
29+
]
30+
end
31+
32+
def self.output
33+
end
34+
35+
def self.return_value
36+
# If you method provides a return value, you can describe here what it does
37+
end
38+
39+
def self.authors
40+
['Automattic']
41+
end
42+
43+
def self.is_supported?(platform)
44+
platform == :android
45+
end
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)