Skip to content

Commit 6dc8304

Browse files
committed
Support universal APK as input as well
1 parent ad5935c commit 6dc8304

File tree

2 files changed

+141
-94
lines changed

2 files changed

+141
-94
lines changed

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ def self.run(params)
1010
if (api_token.nil? || api_token.empty?) && !api_url.is_a?(URI::File)
1111
UI.user_error!('An API token is required when using an `api_url` with a scheme other than `file://`')
1212
end
13+
if params[:aab_path].nil? && params[:universal_apk_path].nil?
14+
UI.user_error!('You must provide at least an `aab_path` or an `universal_apk_path`, or both')
15+
end
1316

1417
# Build the payload base
1518
metrics_helper = Fastlane::Helper::AppSizeMetricsHelper.new(
@@ -21,28 +24,32 @@ def self.run(params)
2124
'Build Type': params[:build_type],
2225
Source: params[:source]
2326
)
24-
metrics_helper.add_metric(name: 'AAB File Size', value: File.size(params[:aab_path]))
27+
# Add AAB file size
28+
metrics_helper.add_metric(name: 'AAB File Size', value: File.size(params[:aab_path])) unless params[:aab_path].nil?
29+
# Add Universal APK file size
30+
metrics_helper.add_metric(name: 'Universal APK File Size', value: File.size(params[:universal_apk_path])) unless params[:universal_apk_path].nil?
2531

26-
# Add device-specific 'splits' metrics to the payload if a `:include_split_sizes` is enabled
32+
# Add optimized file and download sizes for each split `.apk` metrics to the payload if a `:include_split_sizes` is enabled
2733
if params[:include_split_sizes]
28-
check_bundletool_installed!
2934
apkanalyzer_bin = params[:apkanalyzer_binary] || find_apkanalyzer_binary!
30-
UI.message("[App Size Metrics] Generating the various APK splits from #{params[:aab_path]}...")
31-
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir|
32-
Action.sh('bundletool', 'build-apks', '--bundle', params[:aab_path], '--output-format', 'DIRECTORY', '--output', tmp_dir)
33-
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) }
34-
UI.message("[App Size Metrics] Generated #{apks.length} APKs.")
35-
36-
apks.each do |apk|
37-
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...")
38-
split_name = File.basename(apk, '.apk')
39-
file_size = Action.sh(apkanalyzer_bin, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i
40-
download_size = Action.sh(apkanalyzer_bin, 'apk', 'download-size', apk, print_command: false, print_command_output: false).chomp.to_i
41-
metrics_helper.add_metric(name: 'APK File Size', value: file_size, metadata: { split: split_name })
42-
metrics_helper.add_metric(name: 'Download Size', value: download_size, metadata: { split: split_name })
35+
unless params[:aab_path].nil?
36+
check_bundletool_installed!
37+
UI.message("[App Size Metrics] Generating the various APK splits from #{params[:aab_path]}...")
38+
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir|
39+
Action.sh('bundletool', 'build-apks', '--bundle', params[:aab_path], '--output-format', 'DIRECTORY', '--output', tmp_dir)
40+
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) }
41+
UI.message("[App Size Metrics] Generated #{apks.length} APKs.")
42+
43+
apks.each do |apk|
44+
split_name = File.basename(apk, '.apk')
45+
add_apk_size_metrics(helper: metrics_helper, apkanalyzer_bin: apkanalyzer_bin, apk: apk, split_name: split_name)
46+
end
47+
48+
UI.message('[App Size Metrics] Done computing splits sizes.')
4349
end
44-
45-
UI.message('[App Size Metrics] Done computing splits sizes.')
50+
end
51+
unless params[:universal_apk_path].nil?
52+
add_apk_size_metrics(helper: metrics_helper, apkanalyzer_bin: apkanalyzer_bin, apk: params[:universal_apk_path], split_name: 'Universal')
4653
end
4754
end
4855

@@ -76,6 +83,14 @@ def self.find_apkanalyzer_binary!
7683
apkanalyzer_bin
7784
end
7885

86+
def self.add_apk_size_metrics(helper:, apkanalyzer_bin:, apk:, split_name:)
87+
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...")
88+
file_size = Action.sh(apkanalyzer_bin, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i
89+
download_size = Action.sh(apkanalyzer_bin, 'apk', 'download-size', apk, print_command: false, print_command_output: false).chomp.to_i
90+
helper.add_metric(name: 'APK File Size', value: file_size, metadata: { split: split_name })
91+
helper.add_metric(name: 'Download Size', value: download_size, metadata: { split: split_name })
92+
end
93+
7994
#####################################################
8095
# @!group Documentation
8196
#####################################################
@@ -95,6 +110,7 @@ def self.details
95110
DETAILS
96111
end
97112

113+
# rubocop:disable Metrics/MethodLength
98114
def self.available_options
99115
[
100116
FastlaneCore::ConfigItem.new(
@@ -165,7 +181,7 @@ def self.available_options
165181
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_AAB_PATH',
166182
description: 'The path to the .aab to extract size information from',
167183
type: String,
168-
optional: false,
184+
optional: true, # We can have `aab_path` only, or `universal_apk_path` only, or both (but not none)
169185
verify_block: proc do |value|
170186
UI.user_error!('You must provide an path to an existing `.aab` file') unless File.exist?(value)
171187
end
@@ -178,6 +194,16 @@ def self.available_options
178194
type: FastlaneCore::Boolean,
179195
default_value: true
180196
),
197+
FastlaneCore::ConfigItem.new(
198+
key: :universal_apk_path,
199+
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_UNIVERSAL_APK_PATH',
200+
description: 'The path to the Universal .apk to extract size information from',
201+
type: String,
202+
optional: true, # We can have `aab_path` only, or `universal_apk_path` only, or both (but not none)
203+
verify_block: proc do |value|
204+
UI.user_error!('You must provide an path to an existing `.apk` file') unless File.exist?(value)
205+
end
206+
),
181207
FastlaneCore::ConfigItem.new(
182208
key: :apkanalyzer_binary,
183209
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_APKANALYZER_BINARY',
@@ -191,6 +217,7 @@ def self.available_options
191217
),
192218
]
193219
end
220+
# rubocop:enable Metrics/MethodLength
194221

195222
def self.return_type
196223
:integer

spec/android_send_app_size_metrics_spec.rb

Lines changed: 95 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -51,87 +51,107 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
5151
end
5252

5353
context 'when `include_split_sizes` is turned off' do
54-
it 'generates the expected payload compressed by default' do
55-
expected = {
56-
meta: [
57-
{ name: 'Platform', value: 'Android' },
58-
{ name: 'App Name', value: 'my-app' },
59-
{ name: 'App Version', value: '10.2-rc-3' },
60-
{ name: 'Product Flavor', value: 'Vanilla' },
61-
{ name: 'Build Type', value: 'Release' },
62-
{ name: 'Source', value: 'unit-test' },
63-
],
64-
metrics: [
65-
{ name: 'AAB File Size', value: 123_456 },
66-
]
67-
}
68-
69-
test_app_size_action(
70-
fake_aab_size: 123_456,
71-
fake_apks: {},
72-
expected_payload: expected,
73-
app_name: 'my-app',
74-
app_version_name: '10.2-rc-3',
75-
product_flavor: 'Vanilla',
76-
build_type: 'Release',
77-
source: 'unit-test',
78-
include_split_sizes: false
79-
)
54+
context 'when only providing an `aab_path`' do
55+
it 'generates the expected payload compressed by default' do
56+
expected = {
57+
meta: [
58+
{ name: 'Platform', value: 'Android' },
59+
{ name: 'App Name', value: 'my-app' },
60+
{ name: 'App Version', value: '10.2-rc-3' },
61+
{ name: 'Product Flavor', value: 'Vanilla' },
62+
{ name: 'Build Type', value: 'Release' },
63+
{ name: 'Source', value: 'unit-test' },
64+
],
65+
metrics: [
66+
{ name: 'AAB File Size', value: 123_456 },
67+
]
68+
}
69+
70+
test_app_size_action(
71+
fake_aab_size: 123_456,
72+
fake_apks: {},
73+
expected_payload: expected,
74+
app_name: 'my-app',
75+
app_version_name: '10.2-rc-3',
76+
product_flavor: 'Vanilla',
77+
build_type: 'Release',
78+
source: 'unit-test',
79+
include_split_sizes: false
80+
)
81+
end
82+
83+
it 'generates the expected payload uncompressed when disabling gzip' do
84+
expected = {
85+
meta: [
86+
{ name: 'Platform', value: 'Android' },
87+
{ name: 'App Name', value: 'my-app' },
88+
{ name: 'App Version', value: '10.2-rc-3' },
89+
{ name: 'Product Flavor', value: 'Vanilla' },
90+
{ name: 'Build Type', value: 'Release' },
91+
{ name: 'Source', value: 'unit-test' },
92+
],
93+
metrics: [
94+
{ name: 'AAB File Size', value: 123_456 },
95+
]
96+
}
97+
98+
test_app_size_action(
99+
fake_aab_size: 123_456,
100+
fake_apks: {},
101+
expected_payload: expected,
102+
app_name: 'my-app',
103+
app_version_name: '10.2-rc-3',
104+
product_flavor: 'Vanilla',
105+
build_type: 'Release',
106+
source: 'unit-test',
107+
include_split_sizes: false,
108+
use_gzip_content_encoding: false
109+
)
110+
end
80111
end
81112

82-
it 'generates the expected payload uncompressed when disabling gzip' do
83-
expected = {
84-
meta: [
85-
{ name: 'Platform', value: 'Android' },
86-
{ name: 'App Name', value: 'my-app' },
87-
{ name: 'App Version', value: '10.2-rc-3' },
88-
{ name: 'Product Flavor', value: 'Vanilla' },
89-
{ name: 'Build Type', value: 'Release' },
90-
{ name: 'Source', value: 'unit-test' },
91-
],
92-
metrics: [
93-
{ name: 'AAB File Size', value: 123_456 },
94-
]
95-
}
96-
97-
test_app_size_action(
98-
fake_aab_size: 123_456,
99-
fake_apks: {},
100-
expected_payload: expected,
101-
app_name: 'my-app',
102-
app_version_name: '10.2-rc-3',
103-
product_flavor: 'Vanilla',
104-
build_type: 'Release',
105-
source: 'unit-test',
106-
include_split_sizes: false,
107-
use_gzip_content_encoding: false
108-
)
113+
context 'when only providing an `universal_apk_path`' do
114+
it 'generates the expected payload containing the apk file size'
115+
end
116+
117+
context 'when providing both an `aab_path` and an `universal_apk_path`' do
118+
it 'generates the expected payload containing the aab and universal apk file size'
109119
end
110120
end
111121

112122
context 'when keeping the default value of `include_split_sizes` turned on' do
113-
it 'generates the expected payload containing the aab file size and optimized split sizes' do
114-
expected_fixture = File.join(test_data_dir, 'android-metrics-payload.json')
115-
expected = JSON.parse(File.read(expected_fixture))
116-
117-
test_app_size_action(
118-
fake_aab_size: 987_654_321,
119-
fake_apks: {
120-
'base-arm64_v8a.apk': [164_080, 64_080],
121-
'base-arm64_v8a_2.apk': [164_082, 64_082],
122-
'base-armeabi.apk': [150_000, 50_000],
123-
'base-armeabi_2.apk': [150_002, 50_002],
124-
'base-armeabi_v7a.apk': [150_070, 50_070],
125-
'base-armeabi_v7a_2.apk': [150_072, 50_072]
126-
},
127-
expected_payload: expected,
128-
app_name: 'wordpress',
129-
app_version_name: '19.8-rc-3',
130-
app_version_code: 1214,
131-
product_flavor: 'Vanilla',
132-
build_type: 'Release',
133-
source: 'unit-test'
134-
)
123+
context 'when only providing an `aab_path`' do
124+
it 'generates the expected payload containing the aab file size and optimized split sizes' do
125+
expected_fixture = File.join(test_data_dir, 'android-metrics-payload.json')
126+
expected = JSON.parse(File.read(expected_fixture))
127+
128+
test_app_size_action(
129+
fake_aab_size: 987_654_321,
130+
fake_apks: {
131+
'base-arm64_v8a.apk': [164_080, 64_080],
132+
'base-arm64_v8a_2.apk': [164_082, 64_082],
133+
'base-armeabi.apk': [150_000, 50_000],
134+
'base-armeabi_2.apk': [150_002, 50_002],
135+
'base-armeabi_v7a.apk': [150_070, 50_070],
136+
'base-armeabi_v7a_2.apk': [150_072, 50_072]
137+
},
138+
expected_payload: expected,
139+
app_name: 'wordpress',
140+
app_version_name: '19.8-rc-3',
141+
app_version_code: 1214,
142+
product_flavor: 'Vanilla',
143+
build_type: 'Release',
144+
source: 'unit-test'
145+
)
146+
end
147+
end
148+
149+
context 'when only providing an `universal_apk_path`' do
150+
it 'generates the expected payload containing the apk file size and optimized file and download sizes'
151+
end
152+
153+
context 'when providing both an `aab_path` and an `universal_apk_path`' do
154+
it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits'
135155
end
136156
end
137157
end

0 commit comments

Comments
 (0)