bstar55
bstar55

Reputation: 3634

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

A few flavors of this question have been asked before, but I don't understand how the answers to them 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:

  1. Select the directory I want to run the hardcoded strings check from in the project hierarchy
  2. Navigate to Analyze-->Run inspection by name...
  3. Select "Hard coded strings"
  4. With the "Directory" radio button selected, click OK to run the check

At this point, I get about 3500 hits, 2500 of which are logging statements. I run a sed command to remove all logging from my project and repeat the steps above. This leaves 1000 hits. Of those about 700 hundred are strings literals assigned to constants, so I do the following:

  1. Navigate to Android Studio-->Preferences
  2. Navigate to Inspections-->Internationalization issues-->Hard coded strings
  3. In the box to the right, I check "Ignore literals assigned to constants"

I rerun the check and end up with 300 hits. Of those about 200 are some form of hardcoded tag for reading/writing JSON properties, and 99 are built strings for things like file names and shared preferences.

This 2 hour process left me with 1 actual string literal that wasn't being localized.

The question: Is there a built-in way to say, "run this localization check, but ignore lines that match this [list of] regular expression[s]"?

Upvotes: 4

Views: 2412

Answers (2)

Madhu S. Kapoor
Madhu S. Kapoor

Reputation: 345

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

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

Upvotes: 2

Rich Champeaux
Rich Champeaux

Reputation: 201

With version 1.3 of Android Studio, after running "Hardcoded text" inspection by name, it does indeed catch lots of non-issues, such as parameters to Log methods.

However, the right panel of the inspection result window contains information about the rule, along with some "problem resolution" options. One of those options will be "Annotate parameter..." or "Annotate class...". These options create entries in an annotations.xml file that tells the inspection rule to ignore those parameters or classes.

With this, subsequent runs of the inspection won't flag those calls. So after you do it for a couple of code files, you will probably hit most of the exceptions and it will cut down the number of results for the whole project.

Here is an example of the 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>

Upvotes: 3

Related Questions