Class: AndroidApk

Inherits:
Object
  • Object
show all
Defined in:
lib/android_apk.rb

Defined Under Namespace

Classes: AndroidManifestValidateError

Constant Summary collapse

NOT_ALLOW_DUPLICATE_TAG_NAMES =
%w(application)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



6
7
8
# File 'lib/android_apk.rb', line 6

def filepath
  @filepath
end

#iconObject

Returns the value of attribute icon.



6
7
8
# File 'lib/android_apk.rb', line 6

def icon
  @icon
end

#iconsObject

Returns the value of attribute icons.



6
7
8
# File 'lib/android_apk.rb', line 6

def icons
  @icons
end

#labelObject

Returns the value of attribute label.



6
7
8
# File 'lib/android_apk.rb', line 6

def label
  @label
end

#labelsObject

Returns the value of attribute labels.



6
7
8
# File 'lib/android_apk.rb', line 6

def labels
  @labels
end

#package_nameObject

Returns the value of attribute package_name.



6
7
8
# File 'lib/android_apk.rb', line 6

def package_name
  @package_name
end

#resultsObject

Returns the value of attribute results.



6
7
8
# File 'lib/android_apk.rb', line 6

def results
  @results
end

#sdk_versionObject

Returns the value of attribute sdk_version.



6
7
8
# File 'lib/android_apk.rb', line 6

def sdk_version
  @sdk_version
end

#target_sdk_versionObject

Returns the value of attribute target_sdk_version.



6
7
8
# File 'lib/android_apk.rb', line 6

def target_sdk_version
  @target_sdk_version
end

#version_codeObject

Returns the value of attribute version_code.



6
7
8
# File 'lib/android_apk.rb', line 6

def version_code
  @version_code
end

#version_nameObject

Returns the value of attribute version_name.



6
7
8
# File 'lib/android_apk.rb', line 6

def version_name
  @version_name
end

Class Method Details

._parse_aapt(results) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/android_apk.rb', line 124

def self._parse_aapt(results)
  vars = Hash.new
  results.split("\n").each do |line|
    key, value = _parse_line(line)
    next if key.nil?
    if vars.key?(key) && allow_duplicate?(key)
      if (vars[key].is_a?(Hash) and value.is_a?(Hash))
        vars[key].merge(value)
      else
        vars[key] = [vars[key]] unless (vars[key].is_a?(Array))
        if (value.is_a?(Array))
          vars[key].concat(value)
        else
          vars[key].push(value)
        end
      end
    else
       vars[key] = value.nil? ? nil :
         (value.is_a?(Hash) ? value :
           (value.length > 1 ? value : value[0]))
    end
  end
  return vars
end

._parse_line(line) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/android_apk.rb', line 112

def self._parse_line(line)
  return nil if line.nil?
  info = line.split(":", 2)
  values =
      if info[0].start_with?('application-label')
        _parse_values_workaround info[1]
      else
        _parse_values info[1]
      end
  return info[0], values
end

._parse_values(str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/android_apk.rb', line 99

def self._parse_values(str)
  return nil if str.nil?
  if str.index("='")
    # key-value hash
    vars = Hash[str.scan(/(\S+)='((?:\\'|[^'])*)'/)]
    vars.each_value {|v| v.gsub(/\\'/, "'")}
  else
    # values array
    vars = str.scan(/'((?:\\'|[^'])*)'/).map{|v| v[0].gsub(/\\'/, "'")}
  end
  return vars
end

._parse_values_workaround(str) ⇒ Object



94
95
96
97
# File 'lib/android_apk.rb', line 94

def self._parse_values_workaround(str)
  return nil if str.nil?
  str.scan(/^'(.+)'$/).map{|v| v[0].gsub(/\\'/, "'")}
end

.allow_duplicate?(key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



149
150
151
152
# File 'lib/android_apk.rb', line 149

def self.allow_duplicate?(key)
  raise AndroidManifestValidateError, "Duplication of #{key} tag is not allowed" if NOT_ALLOW_DUPLICATE_TAG_NAMES.include?(key)
  true
end

.analyze(filepath) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/android_apk.rb', line 12

def self.analyze(filepath)
  return nil unless File.exist?(filepath)
  apk = AndroidApk.new
  command = "aapt dump badging #{filepath.shellescape} 2>&1"
  results = `#{command}`
  if $?.exitstatus != 0 or results.index("ERROR: dump failed")
    return nil
  end
  apk.filepath = filepath
  apk.results = results
  vars = _parse_aapt(results)

  # application info
  apk.label = vars['application-label']
  apk.icon = vars['application']['icon']

  # package 
  apk.package_name, apk.version_code, apk.version_name =
    vars['package'].values_at('name', 'versionCode', 'versionName')

  # platforms
  apk.sdk_version = vars['sdkVersion']
  apk.target_sdk_version = vars['targetSdkVersion']

  # icons and labels
  apk.icons = Hash.new
  apk.labels = Hash.new
  vars.each_key do |k|
    k =~ /^application-icon-(\d+)$/ && apk.icons[$1.to_i] = vars[k]
    k =~ /^application-label-(\S+)$/ && apk.labels[$1] = vars[k]
  end

  return apk
end

Instance Method Details

#dpi_str(dpi) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/android_apk.rb', line 65

def dpi_str(dpi)
  case dpi.to_i
    when 120
      'ldpi'
    when 160
      'mdpi'
    when 240
      'hdpi'
    when 320
      'xhdpi'
    when 480
      'xxhdpi'
    when 640
      'xxxhdpi'
    else
      'xxxhdpi'
  end
end

#icon_file(dpi = nil, want_png = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/android_apk.rb', line 47

def icon_file(dpi = nil, want_png = false)
  icon = dpi ? self.icons[dpi.to_i] : self.icon
  return nil if icon.empty?

  if want_png && icon.end_with?('.xml')
    dpis = dpi_str(dpi)
    icon.gsub! %r{res/(drawable|mipmap)-anydpi-(?:v\d+)/([^/]+)\.xml}, "res/\\1-#{dpis}-v4/\\2.png"
  end

  Dir.mktmpdir do |dir|
    command = "unzip #{self.filepath.shellescape} #{icon.shellescape} -d #{dir.shellescape} 2>&1"
    results = `#{command}`
    path =  dir + "/" + icon 
    return nil unless File.exist?(path)
    return File.new(path,'r')
  end
end

#signatureObject



84
85
86
87
88
89
90
91
# File 'lib/android_apk.rb', line 84

def signature
  command = "unzip -p #{self.filepath.shellescape} META-INF/*.RSA META-INF/*.DSA | keytool -printcert | grep SHA1:"
  output, _, status = Open3.capture3(command)
  return if status != 0 || output.nil? || !output.index('SHA1:')
  val = output.scan(/(?:[0-9A-Z]{2}:?){20}/)
  return nil if val.nil? || val.length != 1
  return val[0].gsub(/:/, "").downcase
end