Skip to content

Instantly share code, notes, and snippets.

@jberkel
Created July 12, 2011 15:26
Show Gist options
  • Save jberkel/1078196 to your computer and use it in GitHub Desktop.
Save jberkel/1078196 to your computer and use it in GitHub Desktop.
find_unused_strings.rb
#!/usr/bin/env ruby
require 'rexml/document'
require 'rexml/xpath'
require 'yaml'
require 'pp'
strings = 'res/values/strings.xml'
unused_yaml = 'unused.yaml'
doc = REXML::Document.new(File.new(strings))
names, unused = [], []
if File.exists?(unused_yaml)
unused = YAML.load_file(unused_yaml)
else
REXML::XPath.match(doc, '//string | //plurals').each do |e|
names << e.attribute('name').to_s
end
names.each do |n|
ret = system "ack 'R\.(string|plurals).#{n}' --type=java -1 src &>/dev/null"
if !ret
ret = system "ack '@(string|plurals)/#{n}' --type=xml -1 AndroidManifest.xml res &>/dev/null"
if !ret
unused << n
end
end
end
File.open(unused_yaml, 'w') { |f| f<<YAML.dump(unused) }
end
REXML::XPath.match(doc, '//string | //plurals').each do |e|
name = e.attribute('name').to_s
if unused.include? name
e.parent.delete e
end
end
puts doc.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment