Skip to content

Commit

Permalink
refactor: Create a wrapper class for AAPT2's results
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatsu committed Mar 11, 2024
1 parent 481b785 commit b686522
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
32 changes: 12 additions & 20 deletions lib/android_apk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,43 +142,35 @@ def initialize(
@filepath = filepath

aapt2 = Aapt2::DumpBadging.new(apk_filepath: filepath)
vars = aapt2.parse
aapt2_result = aapt2.parse

# application info
@label = vars["application-label"]
@label = aapt2_result.label

@default_icon_path = vars["application"]["icon"]
@test_only = vars.key?("testOnly='-1'")
@default_icon_path = aapt2_result.default_icon_path
@test_only = aapt2_result.test_only?

# package

@package_name = vars["package"]["name"]
@version_code = vars["package"]["versionCode"]
@version_name = vars["package"]["versionName"] || ""
@package_name = aapt2_result.package_name
@version_code = aapt2_result.version_code
@version_name = aapt2_result.version_name || ""

# platforms
@min_sdk_version = vars["sdkVersion"]
@target_sdk_version = vars["targetSdkVersion"]
@min_sdk_version = aapt2_result.min_sdk_version
@target_sdk_version = aapt2_result.target_sdk_version

# icons and labels
@icons = {} # old
@labels = {}

vars.each_key do |k|
if (m = k.match(/\Aapplication-icon-(\d+)\z/))
@icons[m[1].to_i] = vars[k]
elsif (m = k.match(/\Aapplication-label-(\S+)\z/))
@labels[m[1]] = vars[k]
end
end
@icons = aapt2_result.icons # old
@labels = aapt2_result.labels

# It seems the resources in the aapt's output doesn't mean that it's available in resource.arsc
icons_in_arsc = ::AndroidApk::ResourceFinder.decode_resource_table(
apk_filepath: filepath,
default_icon_path: default_icon_path
)

@icon_path_hash = icons.dup.transform_keys do |dpi|
@icon_path_hash = @icons.dup.transform_keys do |dpi|
DPI_TO_NAME_MAP[dpi] || DEFAULT_RESOURCE_CONFIG
end.merge(icons_in_arsc)

Expand Down
63 changes: 60 additions & 3 deletions lib/android_apk/aapt2/dump_badging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,67 @@
class AndroidApk
module Aapt2
class DumpBadging
# @!attribute [r] label
# @return [String, NilClass] Return a value which is defined in AndroidManifest.xml. Could be nil.
# @!attribute [r] default_icon_path
# @return [String] Return a relative path of this apk's icon. This is the real filepath in the apk but not resource-friendly path.
# @!attribute [r] test_only
# @return [Boolean] Check whether or not this apk is a test mode. Return true if an apk is a test apk
# @!attribute [r] package_name
# @return [String] an application's package name which is defined in AndroidManifest
# @!attribute [r] version_code
# @return [String] an application's version code which is defined in AndroidManifest
# @!attribute [r] version_name
# @return [String, NilClass] an application's version name which is defined in AndroidManifest
# @!attribute [r] min_sdk_version
# @return [String, NilClass] an application's min sdk version. The format is an integer string which is defined in AndroidManifest.xml. Legacy apk may return nil.
# @!attribute [r] target_sdk_version
# @return [String, NilClass] an application's target sdk version. The format is an integer string which is defined in AndroidManifest.xml. Legacy apk may return nil.
# @!attribute [r] labels
# @return [Hash] an application's labels a.k.a application name in available resources.
# @!attribute [r] icons
# @return [Hash] an application's relative icon paths grouped by densities
# @deprecated no longer used
class Result
attr_reader :label, :default_icon_path, :test_only, :package_name, :version_code, :version_name, :min_sdk_version, :target_sdk_version, :icons, :labels

def initialize(variables)
# application info
@label = variables["application-label"]

@default_icon_path = variables["application"]["icon"]
@test_only = variables.key?("testOnly='-1'")

# package

@package_name = variables["package"]["name"]
@version_code = variables["package"]["versionCode"]
@version_name = variables["package"]["versionName"] || ""

# platforms
@min_sdk_version = variables["sdkVersion"]
@target_sdk_version = variables["targetSdkVersion"]

# icons and labels
@icons = {} # old
@labels = {}

variables.each_key do |k|
if (m = k.match(/\Aapplication-icon-(\d+)\z/))
@icons[m[1].to_i] = variables[k]
elsif (m = k.match(/\Aapplication-label-(\S+)\z/))
@labels[m[1]] = variables[k]
end
end
end

alias test_only? test_only
end

def self.dump_badging(apk_filepath:)
stdout, stderr, status = Open3.capture3("aapt2", "dump", "badging", "--include-meta-data", apk_filepath)
stdout if status.success?


if status.success?
stdout
else
Expand All @@ -30,7 +86,7 @@ def initialize(apk_filepath:)

# Parse output of aapt2 command to Hash format
#
# @return [Hash, nil] return nil if (see str) is nil. Otherwise the parsed hash will be returned.
# @return [::AndroidApk::Aapt2::DumpBadging::Result]
def parse
vars = {}
results = @dump_results.dup
Expand Down Expand Up @@ -59,7 +115,8 @@ def parse
end
end
end
return vars

Result.new(vars)
end

# workaround for https://code.google.com/p/android/issues/detail?id=160847
Expand Down

0 comments on commit b686522

Please sign in to comment.