Class: Danger::DangerApkstats

Inherits:
Plugin
  • Object
show all
Defined in:
lib/apkstats/plugin.rb

Overview

Show stats of your apk file. By default, it’s done using apkanalyzer in android sdk.

All command need your apk filepath like below

apkstats.apk_filepath=<your new apk filepath>

Examples:

Compare two apk files and print it.


apkstats.compare_with(<your old apk filepath>, do_report: true) # report it in markdown table
apkstats.compare_with(<your old apk filepath>, do_report: false) # just return results

Show the file size of your apk file.


apkstats.file_size

Show the download size of your apk file.


apkstats.download_size

Show all required features of your apk file.


apkstats.required_features

Show all non-required features of your apk file.


apkstats.non_required_features

Show all requested permissions of your apk file.


apkstats.permissions

Show the min sdk version of your apk file.


apkstats.min_sdk

Show the target sdk version of your apk file.


apkstats.target_sdk

Show the methods reference count of your apk file.


apkstats.method_reference_count

Show the number of dex of your apk file.


apkstats.dex_count

See Also:

  • Matsuda/danger-apkstats

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#apk_filepathString

Required Your target apk filepath.



90
91
92
# File 'lib/apkstats/plugin.rb', line 90

def apk_filepath
  @apk_filepath
end

#command_pathSymbol, Nil

Optional A custom command path



84
85
86
# File 'lib/apkstats/plugin.rb', line 84

def command_path
  @command_path
end

#command_typeSymbol, Nil

Optional A command type to be run. One of keys of COMMAND_TYPE_MAP



78
79
80
# File 'lib/apkstats/plugin.rb', line 78

def command_type
  @command_type
end

Instance Method Details

#compare_with(other_apk_filepath, do_report: true) ⇒ Hash

Get stats of two apk files and calculate diffs between them.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/apkstats/plugin.rb', line 99

def compare_with(other_apk_filepath, do_report: true)
  raise "apk filepaths must be specified" if apk_filepath.nil? || apk_filepath.empty?

  base_apk = Apkstats::Entity::ApkInfo.new(command, apk_filepath)
  other_apk = Apkstats::Entity::ApkInfo.new(command, other_apk_filepath)

  return {
      base: base_apk.to_h,
      other: base_apk.to_h,
      diff: Apkstats::Entity::ApkInfoDiff.new(base_apk, other_apk).to_h,
  }.tap do |result|
    break unless do_report

    diff = result[:diff]

    md = +"### Apk comparision results" << "\n\n"
    md << "Property | Summary" << "\n"
    md << ":--- | :---" << "\n"

    diff[:min_sdk].tap do |min_sdk|
      break if min_sdk.size == 1

      md << "Min SDK Change | Before #{min_sdk[1]} / After #{min_sdk[0]}" << "\n"
    end

    diff[:target_sdk].tap do |target_sdk|
      break if target_sdk.size == 1

      md << "Target SDK Change | Before #{target_sdk[1]} / After #{target_sdk[0]}" << "\n"
    end

    result[:base][:file_size].tap do |file_size|
      size = Apkstats::Helper::Bytes.from_b(file_size)

      md << "New File Size | #{size.to_b} Bytes. (#{size.to_mb} MB) " << "\n"
    end

    diff[:file_size].tap do |file_size|
      size = Apkstats::Helper::Bytes.from_b(file_size)

      md << "File Size Change | #{size.to_s_b} Bytes. (#{size.to_s_kb} KB) " << "\n"
    end

    diff[:download_size].tap do |download_size|
      size = Apkstats::Helper::Bytes.from_b(download_size)

      md << "Download Size Change | #{size.to_s_b} Bytes. (#{size.to_s_kb} KB) " << "\n"
    end

    result[:base][:method_reference_count].tap do |method_reference_count|
      md << "New Method Reference Count | #{method_reference_count}" << "\n"
    end

    diff[:method_reference_count].tap do |method_reference_count|
      md << "Method Reference Count Change | #{method_reference_count}" << "\n"
    end

    result[:base][:dex_count].tap do |dex_count|
      md << "New Number of dex file(s) | #{dex_count}" << "\n"
    end

    diff[:dex_count].tap do |dex_count|
      md << "Number of dex file(s) Change | #{dex_count}" << "\n"
    end

    report_hash_and_arrays = lambda { |key, name|
      list_up_entities = lambda { |type_key, label|
        diff[key][type_key].tap do |features|
          break if features.empty?

          md << "#{label} | " << features.map { |f| "- #{f}" }.join("<br>").to_s << "\n"
        end
      }

      list_up_entities.call(:new, "New #{name}")
      list_up_entities.call(:removed, "Removed #{name}")
    }

    report_hash_and_arrays.call(:required_features, "Required Features")
    report_hash_and_arrays.call(:non_required_features, "Non-required Features")
    report_hash_and_arrays.call(:permissions, "Permissions")

    markdown(md)
  end
rescue StandardError => e
  warn("apkstats failed to execute the command due to #{e.message}")

  e.backtrace&.each { |line| STDOUT.puts line }
end

#dex_count(_opts = {}) ⇒ Fixnum

Show the number of dex of your apk file.



258
259
260
261
# File 'lib/apkstats/plugin.rb', line 258

def dex_count(_opts = {})
  result = run_command(__method__)
  result || -1
end

#download_size(_opts = {}) ⇒ Fixnum

Show the download size of your apk file.



202
203
204
205
# File 'lib/apkstats/plugin.rb', line 202

def download_size(_opts = {})
  result = run_command(__method__)
  result ? result.to_i : -1
end

#file_size(_opts = {}) ⇒ Fixnum

Show the file size of your apk file.



194
195
196
197
# File 'lib/apkstats/plugin.rb', line 194

def file_size(_opts = {})
  result = run_command(__method__)
  result ? result.to_i : -1
end

#method_reference_count(_opts = {}) ⇒ Fixnum

Show the methods reference count of your apk file.



250
251
252
253
# File 'lib/apkstats/plugin.rb', line 250

def method_reference_count(_opts = {})
  result = run_command(__method__)
  result || -1
end

#min_sdk(_opts = {}) ⇒ String, Nil

Show the min sdk version of your apk file.



236
237
238
# File 'lib/apkstats/plugin.rb', line 236

def min_sdk(_opts = {})
  run_command(__method__)
end

#non_required_features(_opts = {}) ⇒ Array<String>, Nil

Show all non-required features of your apk file. The result doesn’t contain required features.



220
221
222
223
# File 'lib/apkstats/plugin.rb', line 220

def non_required_features(_opts = {})
  result = run_command(__method__)
  result ? result.to_a : nil
end

#permissions(_opts = {}) ⇒ Array<String>, Nil

Show all permissions of your apk file.



228
229
230
231
# File 'lib/apkstats/plugin.rb', line 228

def permissions(_opts = {})
  result = run_command(__method__)
  result ? result.to_a : nil
end

#required_features(_opts = {}) ⇒ Array<String>, Nil

Show all required features of your apk file. The result doesn’t contain non-required features.



211
212
213
214
# File 'lib/apkstats/plugin.rb', line 211

def required_features(_opts = {})
  result = run_command(__method__)
  result ? result.to_a : nil
end

#target_sdk(_opts = {}) ⇒ String, Nil

Show the target sdk version of your apk file.



243
244
245
# File 'lib/apkstats/plugin.rb', line 243

def target_sdk(_opts = {})
  run_command(__method__)
end