How can I find all the MATCHING hard coded strings in Android Studio?

Several variations of this question have been asked before, but I do not understand how the answers are satisfactory for large projects.

My goal here is to find any hardcoded strings in my application that need to be localized, so I do the following:

  • Select the directory where I want to perform hard-coded string validation from the project hierarchy.
  • Go to Analysis -> Run Check By Name ...
  • Select "Hard-coded strings"
  • If you select the Directory radio button, click OK to start the check.

At the moment I receive about 3500 hits, 2500 of which are registration reports. I ran sed command to remove all entries from my project and repeat the steps above. This leaves 1000 hits. Of these, about 700 hundred literal strings are assigned to constants, so I do the following:

  • Go to Android Studio -> Settings
  • Go to Inspections -> Internationalization Issues -> Hard-coded Strings
  • In the box on the right, I check "Ignore literals assigned to constants"

I ran a check and ended up with 300 hits. Of these, about 200 are some form of hardcoded tag for reading / writing JSON properties, and 99 are string strings for things like filenames and general settings.

This 2 hour process left me 1 actual string literal that was not localized.

Question: Is there a built-in way of saying "Run this localization check, but ignore lines that match this [list] of regular expressions [s]"?

+3


source to share


2 answers


With version 1.3 of Android Studio after checking for "Hardcoded text" by name, it really does grab a lot of non-issues like parameters for log methods.

However, the right pane of the scan results window contains information about the rule, as well as some "problem resolution" options. One of these parameters would be "Annotate parameter ..." or "Annotate class ...". These parameters create entries in the annotations.xml file that tell the validation rule to ignore these parameters or classes.

However, subsequent test runs will not flag these calls. So after you've done this for a few code files, you will probably remove most of the exceptions and reduce the number of results for the whole project.



Here is an example XML that is generated in the annotations.xml file:

<root>
  <item name='android.content.Intent Intent(java.lang.String) 0'>
    <annotation name='org.jetbrains.annotations.NonNls'/>
  </item>
  <item name='android.content.Intent android.content.Intent putExtra(java.lang.String, boolean) 0'>
    <annotation name='org.jetbrains.annotations.NonNls'/>
  </item>
  <item name='android.content.Intent android.content.Intent putExtra(java.lang.String, int) 0'>
    <annotation name='org.jetbrains.annotations.NonNls'/>
  </item>
  <item name='android.content.Intent android.content.Intent setAction(java.lang.String) 0'>
    <annotation name='org.jetbrains.annotations.NonNls'/>
  </item>
</root>

      

+1


source


Just add // NON-NLS after the log line to suppress the warning.



NGMCLogger.i(TAG, "This log does not need translation"); //NON-NLS

      

+1


source







All Articles