require 'ruby_apk' Zip.warn_invalid_date = false module Fastlane module Actions module SharedValues ICON_OUTPUT_PATH = :ICON_OUTPUT_PATH end class ExtractAppIconAction < Action def self.run(config) Fastlane::Polidea.session.action_launched("extract_app_icon", config) platform = Actions.lane_context[Actions::SharedValues::PLATFORM_NAME].to_sym icon_output_path = config[:icon_output_path] # Gets icon from ipa/apk icon_file = extract_icon(platform, config) if icon_file Actions.lane_context[SharedValues::ICON_OUTPUT_PATH] = icon_output_path ENV[SharedValues::ICON_OUTPUT_PATH.to_s] = icon_output_path UI.success("Successfully extracted app icon file to '#{Actions.lane_context[SharedValues::ICON_OUTPUT_PATH]}'") icon_file else UI.important("No icon found.") end Fastlane::Polidea.session.action_completed("extract_app_icon") end def self.extract_icon(platform, config) validate_common(config) case platform when :ios extract_icon_from_ipa(config[:ipa], config[:icon_output_path]) if validate_ios(config) when :android extract_icon_from_apk(config[:apk], config[:icon_output_path]) if validate_android(config) end end ######## ios specific def self.validate_ios(config) UI.user_error!("No IPA file path given, pass using `ipa: 'ipa path'`") unless config[:ipa].to_s.length > 0 true end def self.extract_icon_from_ipa(ipa_file, icon_output_path) info = FastlaneCore::IpaFileAnalyser.fetch_info_plist_file(ipa_file) icon_files = ipa_icon_files(info) return if icon_files.empty? icon_file_name = icon_files.first Zip::File.open(ipa_file) do |zipfile| icon_file = self.largest_ios_icon(zipfile.glob("**/Payload/**/#{icon_file_name}*.png")) return nil unless icon_file tmp_path = "/tmp/app_icon.png" File.write(tmp_path, zipfile.read(icon_file)) Actions.sh("xcrun -sdk iphoneos pngcrush -revert-iphone-optimizations #{tmp_path} #{icon_output_path}") File.delete(tmp_path) end end def self.ipa_icon_files(info) icons = info['CFBundleIcons'] unless icons.nil? primary_icon = icons['CFBundlePrimaryIcon'] unless primary_icon.nil? return primary_icon['CFBundleIconFiles'] end end [] end def self.largest_ios_icon(icons) icons.max_by do |file, _| case file.name when /@(\d+)x/ $1.to_i else 1 end end end ######## android specific def self.validate_android(config) UI.user_error!("No APK file path given, pass using `apk: 'apk path'`") unless config[:apk].to_s.length > 0 true end def self.extract_icon_from_apk(apk_file, icon_output_path) apk = Android::Apk.new(apk_file) icon = largest_android_icon(apk) return unless icon icon_file = File.open(icon_output_path, 'wb') icon_file.write icon[:data] icon_file end def self.largest_android_icon(apk) icons = apk.icon selected_icon = icons.max_by do |name, _| case name when /x*hdpi/ name.count "x" when /mdpi/ -1 when %r{\/drawable\/} -2 else -3 end end { name: selected_icon[0], data: selected_icon[1] } if selected_icon rescue nil end ######## common def self.validate_common(config) UI.user_error!("No icon path given, pass using `icon_output_path: 'icon path'`") unless config[:icon_output_path].to_s.length > 0 true end def self.description "Extracts largest icon from .ipa/.apk, use `extract_app_info` instead" end def self.available_options [ FastlaneCore::ConfigItem.new(key: :ipa, env_name: "", description: ".ipa file to extract icon", optional: true, default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH]), FastlaneCore::ConfigItem.new(key: :apk, env_name: "", description: ".apk file to extract icon", optional: true, default_value: Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]), FastlaneCore::ConfigItem.new(key: :icon_output_path, env_name: "", description: "icon output path", optional: true, default_value: "./app_icon.png") ] end def self.output [ ['ICON_OUTPUT_PATH', 'Path to app icon extracted from .ipa/.apk'] ] end def self.author "Piotrek Dubiel" end def self.is_supported?(platform) [:ios, :android].include? platform end def self.category :deprecated end end end end