|
| 1 | +module Fastlane |
| 2 | + module Actions |
| 3 | + class AndroidGenerateApkFromAabAction < Action |
| 4 | + def self.generate_command(aab_file_path, apk_output_file_path, keystore_path, keystore_password, keystore_key_alias, signing_key_password) |
| 5 | + command = "bundletool build-apks --mode universal --bundle #{aab_file_path} --output-format DIRECTORY --output #{apk_output_file_path} " |
| 6 | + code_sign_arguments = "--ks #{keystore_path} --ks-pass #{keystore_password} --ks-key-alias #{keystore_key_alias} --key-pass #{signing_key_password} " |
| 7 | + move_and_cleanup_command = "&& mv #{apk_output_file_path}/universal.apk #{apk_output_file_path}_tmp && rm -rf #{apk_output_file_path} && mv #{apk_output_file_path}_tmp #{apk_output_file_path}" |
| 8 | + |
| 9 | + # Attempt to code sign the APK if a keystore_path parameter is specified |
| 10 | + command += code_sign_arguments unless keystore_path.nil? |
| 11 | + |
| 12 | + # Move the universal.apk file to the specified output path and cleanup the directory created by bundletool |
| 13 | + command += move_and_cleanup_command |
| 14 | + |
| 15 | + return command |
| 16 | + end |
| 17 | + |
| 18 | + def self.run(params) |
| 19 | + begin |
| 20 | + sh('command -v bundletool > /dev/null') |
| 21 | + rescue StandardError |
| 22 | + UI.user_error!('bundletool is not installed. Please install it using the instructions at https://developer.android.com/studio/command-line/bundletool.') |
| 23 | + raise |
| 24 | + end |
| 25 | + |
| 26 | + # If no AAB param was provided, attempt to get it from the lane context |
| 27 | + # First GRADLE_ALL_AAB_OUTPUT_PATHS if only one |
| 28 | + # Second GRADLE_AAB_OUTPUT_PATH if it is set |
| 29 | + # Else use the specified parameter value |
| 30 | + if params[:aab_file_path].nil? |
| 31 | + all_aab_paths = Actions.lane_context[SharedValues::GRADLE_ALL_AAB_OUTPUT_PATHS] || [] |
| 32 | + aab_file_path = if all_aab_paths.count == 1 |
| 33 | + all_aab_paths.first |
| 34 | + else |
| 35 | + Actions.lane_context[SharedValues::GRADLE_AAB_OUTPUT_PATH] |
| 36 | + end |
| 37 | + else |
| 38 | + aab_file_path = params[:aab_file_path] |
| 39 | + end |
| 40 | + |
| 41 | + # If no AAB file path was found, raise an error |
| 42 | + if aab_file_path.nil? |
| 43 | + UI.user_error!('No AAB file path was specified and none was found in the lane context. Please specify the `aab_file_path` parameter or ensure that the relevant build action has been run prior to this action.') |
| 44 | + raise |
| 45 | + end |
| 46 | + |
| 47 | + apk_output_file_path = params[:apk_output_file_path] |
| 48 | + keystore_path = params[:keystore_path] |
| 49 | + keystore_password = params[:keystore_password] |
| 50 | + keystore_key_alias = params[:keystore_key_alias] |
| 51 | + signing_key_password = params[:signing_key_password] |
| 52 | + |
| 53 | + sh(generate_command(aab_file_path, apk_output_file_path, keystore_path, keystore_password, keystore_key_alias, signing_key_password)) |
| 54 | + end |
| 55 | + |
| 56 | + ##################################################### |
| 57 | + # @!group Documentation |
| 58 | + ##################################################### |
| 59 | + |
| 60 | + def self.description |
| 61 | + 'Generates an APK from the specified AAB' |
| 62 | + end |
| 63 | + |
| 64 | + def self.details |
| 65 | + 'Generates an APK from the specified AAB' |
| 66 | + end |
| 67 | + |
| 68 | + def self.available_options |
| 69 | + [ |
| 70 | + FastlaneCore::ConfigItem.new( |
| 71 | + key: :aab_file_path, |
| 72 | + env_name: 'ANDROID_AAB_FILE_PATH', |
| 73 | + description: 'The path to the AAB file. If not speicified, the action will attempt to read from the lane context using the `SharedValues::GRADLE_ALL_AAB_OUTPUT_PATHS` and `SharedValues::GRADLE_AAB_OUTPUT_PATH` keys', |
| 74 | + type: String, |
| 75 | + optional: true, |
| 76 | + default_value: nil, |
| 77 | + verify_block: proc { |p| UI.user_error!("AAB path `#{p}` is not a valid file path.") unless File.file?(p) } |
| 78 | + ), |
| 79 | + FastlaneCore::ConfigItem.new( |
| 80 | + key: :apk_output_file_path, |
| 81 | + env_name: 'ANDROID_APK_OUTPUT_PATH', |
| 82 | + description: 'The output path where the APK file will be generated. The directory will be created if it does not yet exist', |
| 83 | + type: String, |
| 84 | + optional: false, |
| 85 | + default_value: nil |
| 86 | + ), |
| 87 | + FastlaneCore::ConfigItem.new( |
| 88 | + key: :keystore_path, |
| 89 | + env_name: 'ANDROID_KEYSTORE_PATH', |
| 90 | + description: 'The path to the keystore file', |
| 91 | + type: String, |
| 92 | + optional: true, |
| 93 | + default_value: nil, |
| 94 | + verify_block: proc { |p| UI.user_error!("Keystore file path `#{p}` is not a valid file path.") unless File.file?(p) || p.nil } |
| 95 | + ), |
| 96 | + FastlaneCore::ConfigItem.new( |
| 97 | + key: :keystore_password, |
| 98 | + env_name: 'ANDROID_KEYSTORE_PASSWORD', |
| 99 | + description: 'The password for the keystore', |
| 100 | + type: String, |
| 101 | + optional: true, |
| 102 | + default_value: nil |
| 103 | + ), |
| 104 | + FastlaneCore::ConfigItem.new( |
| 105 | + key: :keystore_key_alias, |
| 106 | + env_name: 'ANDROID_KEYSTORE_KEY_ALIAS', |
| 107 | + description: 'The alias of the key in the keystore', |
| 108 | + type: String, |
| 109 | + optional: true, |
| 110 | + default_value: nil |
| 111 | + ), |
| 112 | + FastlaneCore::ConfigItem.new( |
| 113 | + key: :signing_key_password, |
| 114 | + env_name: 'ANDROID_SIGNING_KEY_PASSWORD', |
| 115 | + description: 'The password for the signing key', |
| 116 | + type: String, |
| 117 | + optional: true, |
| 118 | + default_value: nil |
| 119 | + ), |
| 120 | + ] |
| 121 | + end |
| 122 | + |
| 123 | + def self.authors |
| 124 | + ['Automattic'] |
| 125 | + end |
| 126 | + |
| 127 | + def self.is_supported?(platform) |
| 128 | + true |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments