[英]How can I find all hard coded strings in my project in Android Studio
I need to find and extract all hard coded strings in my project in Android Studio (beta) 0.84.我需要在 Android Studio (beta) 0.84 的项目中查找并提取所有硬编码字符串。 I need a static analysis tool like Find Bugs do this for me in one shot and allow me to step through each message and extract the string to resource files.
我需要一个像 Find Bugs 这样的静态分析工具,一次性为我完成这项工作,并允许我单步执行每条消息并将字符串提取到资源文件中。 Even better would be if the entire process is automated.
如果整个过程是自动化的,那就更好了。
In Android Studio (beta) 0.84 the File / Setting /FindBugs-IDEA shows I18N as an option under Reporting tab.在 Android Studio (beta) 0.84 中,文件 / 设置 /FindBugs-IDEA 将 I18N 显示为报告选项卡下的选项。 But I just cannot figure out how to make it work.
但我就是不知道如何使它工作。
Thanks for any suggestions.感谢您的任何建议。
As @Maor Hadad and other upvotes suggested me : I convert my comment in an answer, so :正如@Maor Hadad和其他赞成票建议我的那样:我将我的评论转换为答案,因此:
Since Android Studio 1.2.2, there is a new option in从 Android Studio 1.2.2 开始,在
"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).我使用了它,当前版本似乎比“硬编码文本”(仅检查 xml 文件)更可靠。
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.您还可以转到 Android Studio > Preferences > Inspections > Hardcoded text 并准确指定它的运行方式,以及从检查中排除哪些(如果有)特殊情况。
For Windows platform, The best way I found is this:对于 Windows 平台,我发现的最好方法是这样的:
You can use this shortcut Ctrl
Alt
Shift
I
and search for,您可以使用此快捷方式
Ctrl
Alt
Shift
I
并搜索,
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.您可以选择要在其中搜索硬编码字符串的适当模块,它将为您提供所有字符串的列表。
What worked fine for me was searching on the whole project using regex:对我有用的是使用正则表达式搜索整个项目:
android:text="[az]机器人:文本=“[az]
To find inside those kotlin/java class:要在这些 kotlin/java 类中找到:
[.]text = "[az] [.]text = "[az]
[.]setText(" [.]setText("
don't forget to tap on the .* blue button at the very end of this image above不要忘记点击上图最末尾的 .* 蓝色按钮
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 grep
下不能使用grep
2. First cd
to project's layout
dir 2.首先
cd
到项目的layout
目录
3. kcoppock answer is great, but it takes too much time 3. kcoppock 的回答很棒,但需要太多时间
Use Ctrl + Shift + F combination for Windows to search everywhere, it shows preview also.使用 Windows 的Ctrl + Shift + F组合可以到处搜索,它也显示预览。
Use Ctrl + F combination for Windows to search in current file.使用Ctrl + F组合在 Windows 中搜索当前文件。
Use Shift + Shift (Double Shift) combination for Windows to search Project File of Project.使用 Windows 的Shift + Shift (Double Shift)组合来搜索项目的项目文件。
May be unavailable for older versions.旧版本可能不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.