class Fastlane::Actions::PlateAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 139
def self.authors
  ["Jonathan Nogueira"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 79
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :apk_path,
                                 env_name: "SOUS_APK_PATH",
                                 description: "Path to the APK File",
                                 is_string: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("No APK Path given, pass using `apk_path: 'path to apk'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :keystore_path,
                                 env_name: "SOUS_KEYSTORE_PATH",
                                 description: "Path to the Keystore file to sign APKs",
                                 is_string: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Keystore path is not present, pass using `keystore_path: 'path to keystore'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :key_password,
                                 env_name: "SOUS_KEY_PASSWORD",
                                 description: "Signing Keystore password",
                                 is_string: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("No Keystore password given, pass using `key_password: 'password'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :alias_name,
                                 env_name: "SOUS_ALIAS_NAME",
                                 description: "Keystore Alias name",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :alias_password,
                                 env_name: "SOUS_ALIAS_PASSWORD",
                                 description: "URL to the git repo containing the android secrets",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :zip_align,
                                 env_name: "SOUS_ZIP_ALIGN",
                                 description: "Whether to specifically zip align the apk before signing",
                                 is_string: false,
                                 optional: true,
                                 default_value: false)
  ]
end
description() click to toggle source

@!group Documentation

# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 125
def self.description
  "This action signs apks using an existing keystore"
end
details() click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 129
def self.details
  "You can use this action to sign APKs"
end
get_build_tools() click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 66
def self.get_build_tools
  android_home = Helper::SousHelper.get_android_home
  build_tools_root = File.join(android_home, '/build-tools')

  sub_dirs = Dir.glob(File.join(build_tools_root, '*', ''))
  build_tools_last_version = ''
  sub_dirs.each do |sub_dir|
    build_tools_last_version = sub_dir
  end

  build_tools_last_version
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 143
def self.is_supported?(platform)
  platform == :android
end
output() click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 133
def self.output
  [
    ['SIGNED_APK_PATH', 'The path to the signed apk file']
  ]
end
run(params) click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 9
def self.run(params)
  apk_path = params[:apk_path]
  keystore_path = params[:keystore_path]
  key_password = params[:key_password]
  alias_name = params[:alias_name]
  alias_password = params[:alias_password]
  zip_align = params[:zip_align]

  UI.message("Signing APK #{apk_path}...")
  apk_path = self.sign_apk(
    apk_path: apk_path,
    keystore_path: keystore_path,
    key_password: key_password,
    alias_name: alias_name,
    alias_password: alias_password,
    zip_align: zip_align
  )

  Actions.lane_context[SharedValues::SIGNED_APK_PATH] = apk_path

  apk_path
end
sign_apk(apk_path:, keystore_path:, key_password:, alias_name:, alias_password:, zip_align:) click to toggle source
# File lib/fastlane/plugin/sous/actions/plate_action.rb, line 32
def self.sign_apk(apk_path:, keystore_path:, key_password:, alias_name:, alias_password:, zip_align:)
  build_tools_path = self.get_build_tools

  # https://developer.android.com/studio/command-line/zipalign
  if zip_align == true
    apk_path_aligned = apk_path.gsub(".apk", "-aligned.apk")
    if File.exist?(apk_path_aligned)
      File.delete(apk_path_aligned)
    end
    sh("#{build_tools_path}zipalign 4 \"#{apk_path}\" \"#{apk_path_aligned}\"")
  else
    apk_path_aligned = apk_path
  end

  apk_path_signed = apk_path.gsub(".apk", "-signed.apk")
  apk_path_signed = apk_path_signed.gsub("unsigned", "")
  apk_path_signed = apk_path_signed.gsub("--", "-")

  # https://developer.android.com/studio/command-line/apksigner
  if File.exist?(apk_path_signed)
    File.delete(apk_path_signed)
  end

  sh("#{build_tools_path}apksigner sign --ks \"#{keystore_path}\" --ks-pass pass:\"#{key_password}\" --v1-signing-enabled true --v2-signing-enabled true --out \"#{apk_path_signed}\" \"#{apk_path_aligned}\"")

  sh("#{build_tools_path}apksigner verify \"#{apk_path_signed}\"")

  if File.exist?(apk_path_aligned)
    File.delete(apk_path_aligned)
  end

  apk_path_signed
end