Skip to content

Commit ea55986

Browse files
committed
Support removal of arrays
Match patterns such as "string-array" that would previously only return "array", and subsequently fail in `remove_resource_value` Add string-array to test app
1 parent 73eef7f commit ea55986

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

android_clean_app.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Issue:
2323
Stores a single issue reported by Android Lint
2424
"""
2525
pattern = re.compile('The resource `?([^`]+)`? appears to be unused')
26+
pattern_array = re.compile('<`?([^`]+)`? name="`?([^`]+)`?">')
2627

2728
def __init__(self, filepath, remove_file):
2829
self.filepath = filepath
@@ -35,12 +36,18 @@ def __str__(self):
3536
def __repr__(self):
3637
return '{0} {1}'.format(self.filepath)
3738

38-
def add_element(self, message):
39+
def add_element(self, message, errorLine1):
3940
res_all = re.findall(Issue.pattern, message)
4041
if res_all:
4142
res = res_all[0]
4243
bits = res.split('.')[-2:]
43-
self.elements.append((bits[0], bits[1]))
44+
if bits[0] != 'array':
45+
type = bits[0]
46+
else:
47+
# array is a generic type, which may take form string-array, integer-array, etc.
48+
# The only way to get type is to parse from errorLine1 which is less reliable than message.
49+
type = re.findall(Issue.pattern_array, errorLine1)[0][0]
50+
self.elements.append((type, bits[1]))
4451
else:
4552
print("The pattern '%s' seems to find nothing in the error message '%s'. We can't find the resource and can't remove it. The pattern might have changed, please check and report this in github issues." % (
4653
Issue.pattern, message))
@@ -95,7 +102,7 @@ def parse_lint_result(lint_result_path):
95102
# TODO stop guessing
96103
remove_entire_file = (location.get('line') or location.get('column')) is None
97104
issue = Issue(filepath, remove_entire_file)
98-
issue.add_element(issue_xml.get('message'))
105+
issue.add_element(issue_xml.get('message'), issue_xml.get('errorLine1'))
99106
issues.append(issue)
100107
return issues
101108

test/android_app/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
<resources>
33
<string name="app_name">android_app</string>
44
<string name="missing">missing</string>
5+
<string-array name="missing_array">
6+
<item>Test1</item>
7+
<item>Test2</item>
8+
</string-array>
59
</resources>

0 commit comments

Comments
 (0)