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

前端 未结 6 1925
既然无缘
既然无缘 2021-02-19 15:49

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 String

相关标签:
6条回答
  • 2021-02-19 16:04

    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.

    0 讨论(0)
  • 2021-02-19 16:10

    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.

    0 讨论(0)
  • 2021-02-19 16:16

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

    0 讨论(0)
  • 2021-02-19 16:18

    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)

    3. under 'Inspection profile', select a new custom profile (...)

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

    1. Run this inspection, and all your hardcoded strings will conveniently be collected into a single list.
    0 讨论(0)
  • 2021-02-19 16:26

    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.

    0 讨论(0)
  • 2021-02-19 16:28

    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

    0 讨论(0)
提交回复
热议问题