Android: is there an easy way to find all the Strings in my project?

Android: is there an easy way to find all the Strings in my project?

I want to scan my Android project for all hardcoded Strings so I can localize the project, putting the Strings in strings.xml. I see an option in Eclipse to 'Externalize Strings...' but it isn't specific to Android. 
I know you can refactor individual strings but I'd like to refactor all the Strings at once. I've got a ton of files, and I'm trying to avoid going through each one manually.

Solutions/Answers:

Answer 1:

I would just open up a terminal window(putty if you are on windows) at the root directory of your project. You can then run something like the following:

grep -r --include=*.java '="[^"\r\n]*"' ./*  --to look for something like this String test="text";, a string with =""
or
grep -r --include=*.java '"[^"\r\n]*"' ./*  -- to look for any string, anything enclosed in ""

Of course the next part is to actually substitute the Strings into the Strings.xml, that could be scripted out as well, but may take more tinkering.

Answer 2:

I was searching for this myself. There is a nice way of doing this:

Right click on “src” directory -> Source -> Externalize Strings

This will find all Non-Externalize Strings and let you Externalize them.

Hope this will help 🙂

Edit:
This is not same as the Android -> Extract Android String.
But it can help you find all the Non-Externalize strings. Then, on each string you will have to perform:
Ctrl + 1 -> Android -> Extract Android String.

Answer 3:


Field[] fields = R.string.class.getDeclaredFields(); // or Field[] fields = R.string.class.getFields();
String str = "";
for (int i =0; i < fields.length; i++) {
int resId = getResources().getIdentifier(fields[i].getName(), "string", getPackageName());
str += fields[i].getName() + " = ";
if (resId != 0) {
str += getResources().getString(resId);
}
str += "\n";
}

You will get all codes of strings with its values in “str” variable.

Answer 4:

Just configure eclipse to show all the string in code:
Configuring eclipse to complain for hard coded strings for android xml preferences

And replace them one by one with eclipse refactor

Answer 5:

NOTE: This solutions can help with HALF your problem- finding all hardcoded strings. You will still need to do the actual refactoring yourself because you still need to name all extracted strings.


EDIT:

This is the shorter and better answer:

  1. Menu -> Analyze -> Run Inspection by name… (even has a shortcut if you feel nimble)
  2. Type “Hardcoded Text”, select scope and run.

You can run a custom lint inspections – to find only hardcoded texts in layout files:

  1. Menu -> Analyze -> Inspect Code
  2. selects desired scope (layouts, for instance)

    enter image description here

  3. under ‘Inspection profile’, select a new custom profile (…)

  4. Duplicate an existing profile, and un-check all inspections EXCEPT ‘HardcodedText’

enter image description here

enter image description here

  1. Run this inspection, and all your hardcoded strings will conveniently be collected into a single list.

Answer 6:

I believe the Google CodePro addon for Eclipse has this feature in there somewhere

References