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

会有一股神秘感。 提交于 2019-11-30 23:28:52

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>

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

NGMCLogger.i(TAG, "This log does not need translation"); //NON-NLS
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!