Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@toms972

This comment has been minimized.

@lambed

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Sep 7, 2014

Thanks for the code

@ilyasjan

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.