如何在 Android Studio 的项目中找到所有硬编码字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25043809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find all hard coded strings in my project in Android Studio
提问by Farrukh Najmi
I need to find and extract all hard coded strings in my project in 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. Even better would be if the entire process is automated.
我需要在 Android Studio (beta) 0.84 的项目中查找并提取所有硬编码字符串。我需要一个像 Find Bugs 这样的静态分析工具,一次性为我完成这项工作,并允许我单步执行每条消息并将字符串提取到资源文件中。如果整个过程是自动化的,那就更好了。
In Android Studio (beta) 0.84 the File / Setting /FindBugs-IDEA shows I18N as an option under Reporting tab. But I just cannot figure out how to make it work.
在 Android Studio (beta) 0.84 中,文件 / 设置 /FindBugs-IDEA 将 I18N 显示为报告选项卡下的一个选项。但我就是不知道如何使它工作。
Thanks for any suggestions.
感谢您的任何建议。
回答by Kevin Coppock
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 并准确指定它的运行方式,以及从检查中排除哪些(如果有)特殊情况。
回答by PAD
As @Maor Hadadand 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".
"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 文件)更可靠。
Seen here link
在这里看到链接
回答by TapanHP
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.
您可以选择要在其中搜索硬编码字符串的适当模块,它将为您提供所有字符串的列表。
回答by Vinayak Garg
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
注释 -
1.grep
不能在 windows 上使用
2. 首先cd
到项目的layout
目录
3. kcoppock 的回答很好,但需要太多时间
回答by Jitesh Prajapati
Use Ctrl+ Shift+ Fcombination for Windows to search everywhere, it shows preview also.
使用Ctrl+ Shift+F组合为Windows到处搜索,它也显示了预览。
Use Ctrl+ Fcombination for Windows to search in current file.
Windows使用Ctrl+F组合在当前文件中搜索。
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.
旧版本可能不可用。