15

Android crashes are often caused by a resource containing %1$ s when %1$s was intended.
To my surprise, Android Studio does not show these syntax errors:

enter image description here

↑ The real syntax error above is the %1$ s (makes the app crash), not the ellipsis that Android Studio highlights.

How to check for resource variable typos in Android Studio?
I am looking for the equivalent of lint --check StringFormatInvalid.

Context: Many semi-automatic language translation tools break variables, making apps crash.

What I have tried, does not work:

Android Studio Lint Invalid format string Highlighting level Inspections

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • I am aware that my suggestion is slightly off topic, but nonethless: Why don't you write (unit) tests therefore? I am not fluent with the Android SDK but I would be surprised if there wasn't a way to read all `resource`s and have them evaluated (and fail when wrong). – Rüdiger Herrmann Feb 04 '16 at 12:19
  • @RüdigerHerrmann: If such a tool existed it would be on-topic. The syntax of variables is not trivial though, not something that can be casually implemented in each app's unit tests. – Nicolas Raoul Feb 04 '16 at 13:59
  • I didn't mean to implement a syntax checker but rather use the Android API to execute what causes the _crash_ at runtime. – Rüdiger Herrmann Feb 04 '16 at 15:32

2 Answers2

3

in Intellij it is possible to create custom inspections. I am not sure if it is possible in Android Studio.

Follow the link: https://www.jetbrains.com/idea/help/creating-custom-inspections.html

edit:

after follow the steps in the link you see in the next image my created inspection: enter image description here

In the next image you see the basic configuration of the inspection: enter image description here

In the next two images you see the setting of the variables. Only the used regex is not the ready. You must write it for general variable using (between $ and . is a space): enter image description here enter image description here

schlagi123
  • 715
  • 4
  • 11
  • I tried Inspections but it does not highlight variable syntax errors, I updated my question to show that better. – Nicolas Raoul Feb 04 '16 at 12:12
  • This could be a first start. Unfortunately, checking variables is rather complex so we will need a lot of regexps... I actually tried a few years ago and could only get something half-satisfying with 10 combined regexps. – Nicolas Raoul Feb 04 '16 at 13:40
  • When regexps to hard, it is also possible to write a Script in Groovy (https://www.jetbrains.com/idea/help/structural-search-and-replace-examples.html#script_constraints) – schlagi123 Feb 04 '16 at 13:54
2

Every thing in strings.xml between <string> and </string> is merely a string for Android studio and lint. It will not identify syntax typos. Syntax errors/typos means error with code and not strings. You'll need to make sure that these are written right by yourself or create your own script to do these checks on the string.xml file. There is no inbuilt mechanism to identify code in strings and then check for syntax errors in them.

The highlighted thing with ellipses is there because it is a grammatical (optimization) error. If you make a spelling/grammatical mistake in strings.xml it will be highlighted. Example below:

enter image description here

But, if you intend to put in formats/code as strings it needs to be handled on your own or using some custom script that you'll need to write by yourself. Or as mentioned by @Rüdiger in his comment on the question, you can write unit test cases to check the sanity and integrity of your strings in strings.xml.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • Are you sure there is nothing available except grammar check and source code? Eclipse was very good at checking the syntax of variables embedded in resource strings. – Nicolas Raoul Feb 04 '16 at 13:41
  • See this for all checks that lint does: http://tools.android.com/tips/lint-checks ... I could relate only translations and spellings with during resources. See if you find anything else relevant in the list. – Viral Patel Feb 04 '16 at 13:55
  • That link contains only 1 relevant paragraph (StringFormatInvalid), which is already shown by the screenshot in my question. – Nicolas Raoul Feb 04 '16 at 14:03
  • Yes, I thinks these are all the checks they got in android studio as of today. May be in future we can hope to see other checks that eclipse send to be doing and are still missing in android studio. – Viral Patel Feb 04 '16 at 14:17