如何在 Android Studio 中找到项目中的所有硬编码字符串

我需要在 Android Studio (beta)0.84中找到并提取项目中的所有硬编码字符串。 我需要一个静态分析工具,如查找错误这样做,我在一个镜头,并允许我通过每一个消息,并提取字符串的资源文件。如果整个过程是自动化的,那就更好了。

在 Android Studio (beta)0.84中,File/Set/FindBugs-IDEA 在 Reportingtab 下将 I18N 显示为一个选项。 但是我就是不知道怎么才能让它运作起来。

谢谢你的建议。

40742 次浏览

Go to "Analyze > Run Inspection By Name...", and type "Hardcoded text". Run that one against your whole project, and you should get an inspection results panel that will show the hardcoded text instances.

You can also go to Android Studio > Preferences > Inspections > Hardcoded text and specify exactly how it runs, and what (if any) special cases are excluded from inspection.

If you just want to find all the hard coded strings in all the layout files only, you can do so very quickly by running -

vinayak@vinayak-osx:layout $ grep -n "android:text=\"" * | grep -v "@string"

Notes -
1. grep can't be used on windows
2. First cd to project's layout dir
3. kcoppock answer is great, but it takes too much time

As @Maor Hadad and other upvotes suggested me : I convert my comment in an answer, so :

Since Android Studio 1.2.2, there is a new option in

"Analyse > Run inspection By Name" => "Hardcoded strings".

I used it and it seems quite more reliable with current version than "hardcoded text" (that checks only xml files).

Seen here link

Use Ctrl + Shift + F combination for Windows to search everywhere, it shows preview also.

Use Ctrl + F combination for Windows to search in current file.

Use Shift + Shift (Double Shift) combination for Windows to search Project File of Project.

May be unavailable for older versions.

For Windows platform, The best way I found is this:

You can use this shortcut Ctrl Alt Shift I and search for,

Hardcoded Text

in the search bar.

You can select appropriate module in which you want to search Hardcoded strings and it will give you the list of all Strings together.

Options for selecting modules

What worked fine for me was searching on the whole project using regex:

android:text="[a-z]

enter image description here

To find inside those kotlin/java class:

[.]text = "[a-z]

enter image description here

[.]setText("

enter image description here

don't forget to tap on the .* blue button at the very end of this image above