Skip to content

Instantly share code, notes, and snippets.

@toms972
Created July 28, 2014 09:05
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save toms972/c83504df2da1176a248a to your computer and use it in GitHub Desktop.
Save toms972/c83504df2da1176a248a to your computer and use it in GitHub Desktop.
Android environment has a limitation of maximum 65536 methods definition Here is a ruby script that helps to count the number of methods per each jar.
#!/usr/bin/env ruby
class CountAndroidMethods
def count(path)
full_folder_path = File.expand_path(path)
total_count = 0
# Traverse the folder
Dir.entries(full_folder_path).each {|file|
# Attempt to read only jars
if file.end_with?('jar')
# Count methods in dex
count = count_methods_in_jar(full_folder_path, file)
# Accumulate count
total_count = total_count + count
# Print out count
puts "File: [#{file}] Methods: [#{count}]"
end
}
# Print out total count
puts "Total of #{total_count} methods"
end
def count_methods_in_jar(full_folder_path, jar_file_name)
temp_dex_path = full_folder_path + '/temp.dex'
jar_path = full_folder_path + '/' + jar_file_name
# Create a temp dex file
`dx --dex --output=#{temp_dex_path} #{jar_path}`
# Count methods in dex
count = `cat #{temp_dex_path} | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d"'`
# Delete temp dex file
File.delete(temp_dex_path)
# Return int
count.to_i
end
end
# Input check
if ARGV.empty? || ARGV.size > 1
puts 'usage: ./count_android_methods.rb [jar folder path]'
else
counter = CountAndroidMethods.new
counter.count(ARGV.first)
end
@lambed
Copy link

lambed commented Aug 31, 2014

This is nice work however, the real question is if you have too many methods in a jar in a project especially in a library you don't control what can you do about it.

@yshinkarev
Copy link

Thanks for the code

@ilyasjan
Copy link

ilyasjan commented Sep 1, 2015

how to use this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment