简体   繁体   English

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

[英]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 文件)更可靠。

Seen here link在这里看到链接

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.

相关问题 如何在Android Studio中找到所有RELEVANT硬编码字符串? - How can I find all RELEVANT hard coded strings in Android Studio? 如何忽略登录Android Studio中的所有硬编码字符串? - How can I ignore all hard coded strings inside Log in Android Studio? 如何在 Android Studio IDE 中找到我项目中所有未使用的方法? - How can I find all unused methods of my project in the Android Studio IDE? 如何将最初用IDEA或Eclipse编码的android项目转换为Android Studio并添加Gradle? - How can I convert an android project originally coded in IDEA or Eclipse to Android Studio and add Gradle? 我在 Android Studio 中找不到我的项目的构建文件夹 - I can't find the build folder of my project in Android studio 我如何构建搜索系统以查找项目中需要android权限的所有引用? - How can I build a search system to find all references that require an android permission in my project? Android:有没有一种简单的方法可以找到我项目中的所有字符串? - Android: is there an easy way to find all the Strings in my project? 如何还原我的Android Studio项目? - How can I restore my Android Studio project? 如何在Android Studio中正确重命名项目? - How can I rename my project properly in Android Studio? 如何将此模块添加到我的Android Studio项目中? - How can I add this module to my Android Studio project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM