Class: Fastlane::Actions::BundletoolAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



253
254
255
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 253

def self.authors
  ['Martin Gonzalez']
end

.available_optionsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 172

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :ks_path,
                                 env_name: 'FL_BUNDLETOOL_KEYSTORE_FILE',
                                 description: 'Path to .jks file',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ks_password,
                                 env_name: 'FL_BUNDLETOOL_KEYSTORE_PASSWORD',
                                 description: '.jks password',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ks_key_alias,
                                 env_name: 'FL_BUNDLETOOL_KEY_ALIAS',
                                 description: 'Alias for jks',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ks_key_alias_password,
                                 env_name: 'FL_BUNDLETOOL_KEY_PASSWORD',
                                 description: 'Alias password for .jks',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :bundletool_version,
                                 env_name: 'FL_BUNDLETOOL_VERSION',
                                 description: 'Version of bundletool to use, by default 0.11.0 will be used',
                                 is_string: true,
                                 default_value: '0.11.0'),
    FastlaneCore::ConfigItem.new(key: :download_url,
                                 env_name: 'FL_BUNDLETOOL_DOWNLOAD_URL',
                                 description: 'Url to download bundletool from, should point to a jar file',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :aab_path,
                                 env_name: 'FL_BUNDLETOOL_AAB_PATH',
                                 description: 'Path where the aab file is',
                                 is_string: true,
                                 optional: false,
                                 verify_block: proc do |value|
                                   unless value && !value.empty?
                                     UI.user_error!('You must set aab_path.')
                                   end
                                 end),
    FastlaneCore::ConfigItem.new(key: :apk_output_path,
                                 env_name: 'FL_BUNDLETOOL_APK_OUTPUT_PATH',
                                 description: 'Path where the apk file is going to be placed',
                                 is_string: true,
                                 optional: true,
                                 default_value: '.'),
    FastlaneCore::ConfigItem.new(key: :verbose,
                                 env_name: 'FL_BUNDLETOOL_VERBOSE',
                                 description: 'Show every messages of the action',
                                 is_string: false,
                                 type: Boolean,
                                 optional: true,
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :cache_path,
                                 env_name: 'FL_BUNDLETOOL_CACHE_PATH',
                                 description: 'Cache downloaded bundletool binary into the cache path specified',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :universal_apk,
                                 env_name: 'FL_BUNDLETOOL_UNIVERSAL_APK',
                                 description: 'Create universal APK (true) or split APKs (false)',
                                 is_string: false,
                                 type: Boolean,
                                 optional: true,
                                 default_value: true)
  ]
end

.clean_temp!Object



143
144
145
146
147
148
149
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 143

def self.clean_temp!
  cmd = "rm -rf #{@project_root}/bundletool_temp"
  Open3.popen3(cmd) do |_, _, stderr, wait_thr|
    exit_status = wait_thr.value
    raise stderr.read unless exit_status.success?
  end
end

.descriptionObject



249
250
251
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 249

def self.description
  'Extracts an universal apk from an .aab file'
end

.detailsObject



257
258
259
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 257

def self.details
  'Using the google oficial bundletool to extract an universal apk from .aab file to distribute it'
end

.download_and_write_bundletool(download_url, bundletool_filename, installation_path) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 266

def self.download_and_write_bundletool(download_url, bundletool_filename, installation_path)
  if(File.exist?"#{installation_path}/#{bundletool_filename}")
    puts_message("Using binary cached at #{installation_path}/#{bundletool_filename}")
    return
  end

  puts_message("Downloading bundletool from #{download_url}")

  URI.open(download_url) do |bundletool|
    File.open("#{installation_path}/#{bundletool_filename}", 'wb') do |file|
      file.write(bundletool.read)
    end
  end

  puts_success('Downloaded bundletool')
end

.download_bundletool(version, download_url, bundletool_filename, cache_path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 67

def self.download_bundletool(version, download_url, bundletool_filename, cache_path)              
  unless download_url.nil?          
    download_and_write_bundletool(download_url, bundletool_filename, cache_path)
  else
    bundletool_url = "https://github.com/google/bundletool/releases/download/#{version}/bundletool-all-#{version}.jar"
    download_and_write_bundletool(bundletool_url, bundletool_filename, cache_path)
  end        
  return true
rescue OpenURI::HTTPError => e
  clean_temp!
  puts_error!("Something went wrong when downloading bundletool version #{version}" + ". \nError message\n #{e.message}")
  return false
end

.extract_apk_from(aab_path, apk_output_path, keystore_info, bundletool_filename, installation_path, universal_apk) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 81

def self.extract_apk_from(aab_path, apk_output_path, keystore_info, bundletool_filename, installation_path, universal_apk)
  aab_absolute_path = Pathname.new(File.expand_path(aab_path)).to_s
  apk_output_absolute_path = Pathname.new(File.expand_path(apk_output_path)).to_s
  output_path = run_bundletool!(aab_absolute_path, keystore_info, bundletool_filename, installation_path, universal_apk)
  prepare_apk!(output_path, apk_output_absolute_path, universal_apk)
rescue StandardError => e
  puts_error!("Bundletool could not extract universal apk from aab at #{aab_absolute_path}. \nError message\n #{e.message}")
ensure
  clean_temp!
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 261

def self.is_supported?(platform)
  [:android].include?(platform)
end

.prepare_apk!(output_path, target_path, universal_apk) ⇒ Object



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
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 116

def self.prepare_apk!(output_path, target_path, universal_apk)
  puts_message("Preparing apk to #{target_path}...")
  if File.file?(target_path)
    puts_important("Apk at path #{target_path} exists. Replacing it.")
  end
  target_dir_name = File.dirname(target_path)
  unless Dir.exist?(target_dir_name)
    puts_important("Creating path #{target_dir_name} since does not exist")
    FileUtils.mkdir_p target_dir_name
  end

  cmd = ''
  if universal_apk == true
    cmd = "mv \"#{output_path}\" \"#{@bundletool_temp_path}/output.zip\" &&"\
    "unzip \"#{@bundletool_temp_path}/output.zip\" -d \"#{@bundletool_temp_path}\" &&"\
    "mv \"#{@bundletool_temp_path}/universal.apk\" \"#{target_path}\""
  else
    cmd = "mv \"#{output_path}\" \"#{target_path}\""
  end

  Open3.popen3(cmd) do |_, _, stderr, wait_thr|
    exit_status = wait_thr.value
    raise stderr.read unless exit_status.success?
  end
  puts_success("Preparing apk to #{target_path}")
end


242
243
244
245
246
247
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 242

def self.print_params(options)
  table_title = "Params for bundletool #{Fastlane::Bundletool::VERSION}"
  FastlaneCore::PrintTable.print_values(config: options,
                                        mask_keys: i[ks_password ks_key_alias_password],
                                        title: table_title)
end

.puts_error!(message) ⇒ Object



167
168
169
170
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 167

def self.puts_error!(message)
  p message
  UI.user_error! message
end

.puts_important(message) ⇒ Object



161
162
163
164
165
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 161

def self.puts_important(message)
  if @verbose
    UI.important "#{message} #{Fastlane::Helper::Emojis.warning}"
  end
end

.puts_message(message) ⇒ Object



151
152
153
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 151

def self.puts_message(message)
  UI.message message if @verbose
end

.puts_success(message) ⇒ Object



155
156
157
158
159
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 155

def self.puts_success(message)
  if @verbose
    UI.success "#{message} #{Fastlane::Helper::Emojis.green_checkmark}"
  end
end

.run(params) ⇒ Object



10
11
12
13
14
15
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
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 10

def self.run(params)
  print_params(params)
  @project_root = Dir.pwd
  @bundletool_temp_path = "#{@project_root}/bundletool_temp"
  @verbose = params[:verbose]
  keystore_info = {}
  unless params[:ks_path].nil?
    keystore_info[:keystore_path] = params[:ks_path]
    keystore_info[:keystore_password] = params[:ks_password]
    keystore_info[:alias] = params[:ks_key_alias]
    keystore_info[:alias_password] = params[:ks_key_alias_password]
  end

  bundletool_version = params[:bundletool_version]
  download_url = params[:download_url]
  aab_path = params[:aab_path]
  output_path = params[:apk_output_path] || '.'
  cache_path = params[:cache_path]
  universal_apk = params[:universal_apk]

  return unless validate_aab!(aab_path)

  if(cache_path.nil?)
    installation_path = @bundletool_temp_path
  else
    installation_path = Pathname.new(File.expand_path(cache_path)).to_s          
  end

  unless(Dir.exist?(installation_path))
    Dir.mkdir(installation_path)
  end

  unless(Dir.exist?(@bundletool_temp_path))
    Dir.mkdir(@bundletool_temp_path)
  end

  unless download_url.nil?
    bundletool_filename = "bundletool_#{Digest::SHA256.hexdigest(download_url)}.jar"
  else
    bundletool_filename = "bundletool_#{bundletool_version}.jar"
  end

  return unless download_bundletool(bundletool_version, download_url, bundletool_filename, installation_path)

  extract_apk_from(aab_path, output_path, keystore_info, bundletool_filename, installation_path, universal_apk)
end

.run_bundletool!(aab_path, keystore_info, bundletool_filename, installation_path, universal_apk) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 92

def self.run_bundletool!(aab_path, keystore_info, bundletool_filename, installation_path, universal_apk)
  puts_message("Extracting apk from #{aab_path}...")
  output_path = "#{@bundletool_temp_path}/output.apks"
  keystore_params = ''

  unless keystore_info.empty?
    key_alias_password = Shellwords.shellescape("pass:#{keystore_info[:alias_password]}")
    key_store_password = Shellwords.shellescape("pass:#{keystore_info[:keystore_password]}")
    key_alias = Shellwords.shellescape(keystore_info[:alias])
    keystore_params = "--ks=\"#{keystore_info[:keystore_path]}\" --ks-pass=#{key_store_password} --ks-key-alias=#{key_alias} --key-pass=#{key_alias_password}"
  end

  universal_apk_param = universal_apk == true ? "--mode=universal" : ""

  cmd = "java -jar #{installation_path}/#{bundletool_filename} build-apks --bundle=\"#{aab_path}\" --output=\"#{output_path}\" #{universal_apk_param} #{keystore_params}"

  Open3.popen3(cmd) do |_, _, stderr, wait_thr|
    exit_status = wait_thr.value
    raise stderr.read unless exit_status.success?
  end
  puts_success("Extracting apk from #{aab_path}")
  output_path
end

.validate_aab!(aab_path) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 57

def self.validate_aab!(aab_path)
  puts_message('Checking if .aab file exists...')
  unless File.file?(aab_path)
    puts_error!(".aab file at #{aab_path} does not exist")
    return false
  end
  puts_success('Checking if .aab file exists')
  return true
end