Shabbir Dhangot
Shabbir Dhangot

Reputation: 9121

Extract all hardcoded strings to string resource

I know there is Alt + Enter combination to extract single String to string resource. But I am wondering is there anything that I can extract all string of my project into string resource?

Also Android studio not making same String to string resource if I make one of them.

String s = "Hello World";
String s2 = "Hello World";

For example. I make "Hello World" to string resource still another remain Hardcoded in same file as well in the project too.

String s = getString(R.string.helloworld);
String s2 = "Hello World";

If anyone know something like that.

Upvotes: 24

Views: 23564

Answers (5)

JB0x2D1
JB0x2D1

Reputation: 920

I wrote the following script to extract all hardcoded strings from an activity.xml file and add them to strings.xml. (Bash/Linux):

Usage: extractAll.sh activity_main.xml strings.xml
Original files are backed up before changes are made.

#!/bin/bash
################################################################################
#  Extract All Hardcoded Strings From an Activity XML File and Save New 
#  Versions of the activity.xml and strings.xml
################################################################################

#check the number of arguments supplied, print usage if not 2
if [ "$#" -ne 2 ]; then
    echo "extract all hardcoded strings from activity.xml and update .xml files"
    echo "original files are saved with .bak extension"
    echo "usage: $0 activity.xml strings.xml"
    exit
fi

#backup input files
#TODO: save these backups in another folder so they don't cause build error
cp $1 $1.bak
cp $2 $2.bak

#grep for hardcoded strings, for each one sed out special characters, change 
#all to lower case, replace space characters with underscores, truncate string
#variable names to 30 characters. The result will be the name of the new string
#variable entered in strings.xml
grep -Po "(?<=android:text=\")[^\"]+(?=\")" $1 | while read -r HARDSTRING ; do
    STRINGVARNAME=`echo $HARDSTRING | sed 's/\([\d0-\d31]\|[\d33-\d35]\|\$\|[\d37-\d47]\|[\d58-\d64]\|[\d91-\d96]\|[\d123-\d126]\)//g'|sed -e 's/\(.*\)/\L\1/'|sed 's/ /_/g'|head -c 30`
    #substitute each hardcoded string with the string variable name
    sed -i "s/$HARDSTRING/@string\/$STRINGVARNAME/" $1
    #get the number of lines in strings.xml file
    NUMLINES=`wc -l < $2`
    #insert string definition at second-to-last line of strings.xml
    let "NUMLINES++" #I had to increment mine to get the desired result
    #add an entry to the strings.xml defining the newly extracted string
    sed -i "$NUMLINES""i\\    <string name=\""$STRINGVARNAME"\">$HARDSTRING</string>\\" $2
done

Final note: wc -l returned one less than expected in my test case using a strings.xml file created by Android Studio (I suspect because the last line had no newline). This caused sed -i to insert the new line at the third-to-last position instead of the desired second-to-last. My fix was to increment the result of wc -l using let.

References:
What are invalid characters in XML
sed one-liner to convert all uppercase to lowercase?
How to process each output line in a loop?
How can I truncate a line of text longer than a given length?
Linux Shell Script - String Comparison with wildcards
How to insert a string into second to last line of a file
Check number of arguments passed to a Bash script
https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash
https://docs.oracle.com/cd/E82085_01/150/funtional_artifacts_guide/or-fasg-standards.htm
https://tldp.org/LDP/abs/html/refcards.html#AEN22828

Upvotes: 2

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

For hardcoded strings in XML layout files:

Click on Analyze -> Run inspection by name -> enter Hardcoded strings -> select the whole project -> OK. Here, you will get all the hardcoded strings from your xml layout files only that u can extract to strings.xml by:

Click on the hardcoded text -> Alt + Enter -> extract from resource -> enter your corresponding resource name for that string -> OK

For hardcoded strings in Java class code:

Click on Analyze -> Run inspection by name -> enter Hardcoded texts -> select the whole project -> OK. Here, you will get all the hardcoded strings from your Java class files only that u can extract to strings.xml by:

Click on the hardcoded text -> Alt + Enter -> extract from resource -> enter your corresponding resource name for that string -> OK -> add getString for each string resource created in your java code.

Hence, Hardcoded strings -> Java class code whereas Hardcoded texts -> Xml layout files in Android Studio.

Upvotes: 4

DragonFire
DragonFire

Reputation: 4082

In Android 3.2.1 you can go to Edit->Find->Find In Path... and then search for android:text=" . This will give you a list of all the hardcoded strings in xml files.

Search for Toast.makeText to find all the toast items from the java files.

Search for setText(" to search for text sets in java files and so forth.

You can do searches like this for items you would like to find throughout the project and replace.

Upvotes: 3

AllDayAmazing
AllDayAmazing

Reputation: 2383

I believe the shortcut you are looking for is (on a Mac) Alt + Command + C

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

As your requirements and as I know there is no such feature in android studio you were really searching for, But here are some alternative ways that can help you.

  • Go to "Analyze > Run Inspection ..", and type "Hardcoded strings". Run that in your whole project, and you will get an inspection results panel that will show all the hardcoded text of projects. Then hit Alt + Enter and you'll get an option to automatically extract that Strings.

  • Another approach is to Find and Replace But It's not better because of time consumption. To simplify the approach you can have a look at here for flexibility.

Upvotes: 29

Related Questions