Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Created December 15, 2011 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spudtrooper/1481157 to your computer and use it in GitHub Desktop.
Save spudtrooper/1481157 to your computer and use it in GitHub Desktop.
Prints out the duplicated classes found in the passed in jars...helpful finding library include problems in Android projects
#!/usr/bin/env ruby
#
# Prints out the duplicated classes in jars
#
# Example:
#
# find_duplicated_jars *.jar
#
class FindDuplicatedJars
def initialize
@verbose = false
@print_classes = false
end
def post_process(classes2jars)
#
# Map group of jars to list of classes
#
jars2classes = {}
classes2jars.each do |cls,jars|
lst = jars2classes[jars] || []
lst << cls
jars2classes[jars] = lst
end
#
# Print out the number of classes duplicated in each possible
# combination of jar
#
jars2classes.each do |jars,classes|
next if jars.size < 2
STDOUT.printf "[%d] %-50s : %d\n",jars.size,jars.join(','),classes.size
if @print_classes
classes.sort.each do |cls|
puts " #{cls}"
end
end
end
end
def note(str)
return if not @verbose
STDERR.puts str
STDERR.flush
end
def process(classes2jars,jar)
#
# Map classes to list of jars in which they're found
#
note "Reading #{jar}..."
IO.popen "jar tvf #{jar}" do |input|
input.each do |line|
line.scan /(\S+)\.class\s*/ do |res|
cls = res[0]
lst = classes2jars[cls] || []
lst << jar
classes2jars[cls] = lst
end
end
end
end
def main(argv)
@print_classes = false
@verbose = false
files = []
argv.each do |arg|
if arg =~ /-p/
@print_classes = true
elsif arg =~ /-v/
@verbose = true
else
files << arg
end
end
classes2jars = {}
files.each do |f|
process classes2jars,f
end
post_process classes2jars
end
end
FindDuplicatedJars.new.main ARGV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment