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