103

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.

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.

Thanks for any suggestions.

Farrukh Najmi
  • 5,055
  • 3
  • 35
  • 54

5 Answers5

158

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

Iman Marashi
  • 5,593
  • 38
  • 51
PAD
  • 2,240
  • 1
  • 22
  • 28
  • 48
    Mind you, the `Hardcoded Text` inspection returns the hardcoded texts in the `.XML` files, whereas the `Hardcoded Strings` inspection returns the hardcoded texts in `.java` files. – Loyalar Jun 29 '16 at 12:42
  • Please see my answer to a similar problem here https://stackoverflow.com/a/56121083/3904109 – DragonFire May 13 '19 at 23:31
  • I had a similar task but for my Flutter Project. The Inspection mentioned above did not bring the expected results, so I tried a Regex. `".+\Q\E` if you substitute the double quote with a single you get all the Strings inside single quotes. – pbertsch Jun 08 '20 at 14:55
  • for now as of 18-oct-2022: "CODE -> Analyze code -> Run inspection By Name" => "Hardcoded strings – Ammar Oct 18 '22 at 09:38
123

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.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 2
    This worked awesomely! Thanks. BTW, are there any tools that would automatically extract strings to values.xml file using a generated resource id based on the text? That would be a great help. Then one can just go and refcator the resource id names to something shorter. – Farrukh Najmi Jul 30 '14 at 19:27
  • I don't know of automatic, but I believe if you highlight one of the strings and hit Option-Enter, you'll get an option to automatically extract that string, but you'll have to type the name manually. – Kevin Coppock Jul 30 '14 at 19:28
  • 3
    +1 a very cool feature of Android Studio. Has anyone found a way to exclude android.util.Log messages from the results? (about 95% of my results!) – Ben Clayton Oct 13 '14 at 13:26
  • 8
    For information : Android Studio 1.2.2 added a new option in "Run inspection By Name" => "Hardcoded strings". It seems quite more reliable with current version than "hardcoded text". Seen here [link](http://stackoverflow.com/a/30799619/3649279) – PAD Jun 12 '15 at 11:28
  • @PAD you should write it as an answer. Hardcoded text return only xml files. ty :) – Maor Hadad Oct 15 '15 at 00:08
  • Works! Pure as asked by the OP. That should be the Accepted answer. – sud007 May 27 '16 at 12:55
15

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

TapanHP
  • 5,969
  • 6
  • 37
  • 66
4

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

Igor Romcy
  • 336
  • 4
  • 7
1

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

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80