Highlight Android NDK error output - android

Is there tool that adds highlighting to android ndk build output (may be on stderr).
e.g. If it will highlight word "error:" in red and "Warning:" in orange that would be what I look.
But if it will also give different colors to code and error messages then it would be awesome!
ANSWER
red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)
$ANDROID_NDK/ndk-build 2>&1 | sed -e "s/\(error:\)/${red}\1${norm}/i" | sed -e "s/\(warning:\)/${yellow}\1${norm}/i"

You could do this by piping the output of your build command (or any other terminal command) to sed:
red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)
make | sed -e "s/\b\(error:\)\b/${red}\1${norm}/i" | sed -e "s/\b\(warning:\)\b/${yellow}\1${norm}/i"

Related

Redirect output to /dev/null when two commands are piped?

I am trying to build a docker image for android CI systems but wanted to avoid a heavy log file. So to avoid output on stdout I wanted it to redirect to /dev/null. I am a novice in using linux commands.
My actual command is:
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager "platforms;android-27"
And also I am commenting multiple lines togeather with &&. So It results in something like this:
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager "platforms;android-27" && \
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager "tools" "platform-tools"
But, the command ($ANDROID_HOME/tools/bin/sdkmanager "platforms;android-27") displays a large license file on screen and it results in a large log file, which ultimately exceeds the log file limit where I am trying to run.
To avoid this, I wanted pass the output to /dev/null. So, I tried it with:
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager "platforms;android-27" && \
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager "tools" "platform-tools" > /dev/null 2>&1 &
It results in licences not being accepted which it should.
How can I do this?
I suggest:
{ command1 | command2 && command3 | command4; } >/dev/null 2>&1

Loop for replace string by other one in different file

I have two lists
list1:
A:1
B:3
C:1
D:5
list2:
1:blue
3:green
5:red
How can i do for have something like:
Desired output(file3):
A:blue
B:green
C:blue
D:red
And here is my unworking code ...
#!/system/bin/bash
list1=$(cat file1)
list2=$(cat file2)
for i in "$list1"; do
num_file1=$( echo $i | cut -d ":" -f 2)
string_file2=$(cat $list2 | grep "$num_file1" | cut -d ":" -f 2)
echo -e "$i" | sed "s/$num_file1/$string_file2/" > list3
done
I also tried
sed 's/"$num_file1"/"$string_file2"/' and many other but failed every times for what i want .. Where i am wrung with sed ??
Ps: its on android ... and few command are misted ...
Give this awk line a try:
awk -F':' 'NR==FNR{k[$1]=$2;next}{print $1 FS k[$1]}' f2 f1
I didn't test the code, but it should work.
Note this assumes that all idx in file1 we have corresponding entry in file2
The awk solution is perfectly fine. Using lesser commands, you could say:
sort -t \: -k 2 list1 | join -t \: -1 2 - list2 | cut -d \: -f 2,3 | sort -t \:
Where i am wrung with sed ??
You were quite close, wrong only in three places.
for i in "$list1"; do has to be for i in $list1; do without quotes, since you want to process the lines individually, not the whole $list1 at once.
string_file2=$(cat $list2 | …) has to be string_file2=$(echo "$list2" | …) with echo instead of cat (since you already read the file2 into the variable $list2) and with quotes to preserve the line separation.
The output redirection > list3 has to be moved away from the echo -e "$i" | sed … line to the end of the done line, otherwise only the last output line would remain in file3.
Another approach to the problem would be to use an array for the colors, indexed by their number:
#!/bin/bash
eval rgb=($(sed 's/\(.*\):/[\1]=/' <file2)) # change 1:blue to [1]=blue etc.
while IFS=: read letter number
do echo $letter:${rgb[$number]} # change A:1 to A:blue etc.
done <file1 >file3

Is this shell script correct?

I have a shell script like this :
sed -i '/^###########/,/^#End of Build.Prop/d' /system/build.prop;
#
sed -i '/^#Start Build.Prop Tweak/,/^#End of Build.Prop Tweak/d' /system/build.prop;
#
sed -i '/^#Start Build.Prop Tweak/,/^ro\.config\.hwfeature_wakeupkey=0/d' /system/build.prop;
Of the three Shell Commands stated above none of them works when put in a sh file. But, if I use a TerminalEmulator, the three scripts can be executed
I want to use the scripts in an Android Device
No, it is too dangerous.
When the end-search tag is missing, you will delete a large part of your file.
When you want to delete the first and second line in a file, it seems working ok:
$ cat test.txt
first line
second line
third line
$ cat test.txt | sed '/first/,/second/ d'
third line
EDIT: One command less with sed '/first/,/second/ d' test.txt
But what happens when the second line can not be found?
Your sed command should skip removing lines, but it will:
$ cat test.txt | sed '/first/,/mistake/ d'
$
EDIT: One command less with sed '/first/,/mistake/ d' test.txt
All lines from the first match have been deleted !

Tool to check Android strings.xml variables?

A simple typo in an Android localization variable (for instance %1d instead of %1$d in strings.xml) can lead to a crash.
We can't afford to test exhaustively (many screens, some very rarely shown, tens of languages, very frequent releases, no revenue) so we must find a smarter way. Those errors are not shown in Eclipse, and actually I am looking for a non-visual tool so that it can be called by our automatic release tool.
I wrote the following script to check the localisation files:
#! /bin/sh
# Spot malformed string replacement patterns in Android localization files.
grep -R "%1$ s" values*
grep -R "%1$ d" values*
grep -R '%' values* |
sed -e 's/%/\n%/g' | # Split lines that contain several expressions
grep '%' | # Filter out lines that do not contain expressions
grep -v ' % ' | # Lone % character, not a variable
grep -v '%<' | # Same, at the end of the string
grep -v '% ' | # Same, at the beginning of the string
grep -v '%で' | # Same, no spaces in Japanese
grep -v '%s' | # Single string variable
grep -v '%d' | # Single decimal variable
grep -v '%[0-9][0-9]\?$s' | # Multiple string variable
grep -v '%[0-9][0-9]\?$d' | # Multiple decimal variable
grep -v '%1$.1f' | # ?
grep -v '%.1f'
grep -R '%' values*
PROBLEM: It fails to catch problems in Arabic localization files.
QUESTION: rather than re-inventing the wheel, is there already such a validation tool? (not Eclipse)
If not: what checks did I forget?
Note: this script is open source
This sounds like something that should be incorporated into the Lint tool in ADT.

Is there any simple way to find out unused strings in Android project?

I have a huge Android project with many strings declared in strings.xml. I wanted to remove unused strings in strings.xml.
Is there any easy way to do so?
On Android Studio:
Menu -> Analyze -> Run Inspection by Name -> Unused resources
Check File mask(s) checkbox and put strings.xml in the text field.
Here is another solution that is fairly easy. In the Android Studio menu go to
Refactor > Remove Unused Resources....
Click Preview to see what the unused resources are and selectively remove them.
In my case "Run Inspection by Name" didnt work, despite the fact I was using "Remove unused resources".
Solution:
Open strings.xml
Secondary click
Refactor --> Remove Unused Resources
I have no clue why "Remove Unused Resources" works one way but not the other.
With ADT 16 you can do it as simple as possible. Update to ADT 16 and use Android Lint. It is really amazing tool. It can find all unused resources (not only strings) and many more. From its official site:
Here are some examples of the types of errors that it looks for:
- Missing translations (and unused translations)
- Layout performance problems (all the issues the old layoutopt tool used to find, and more)
- Unused resources
- Inconsistent array sizes (when arrays are defined in multiple configurations)
- Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
- Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
- Usability problems (like not specifying an input type on a text field)
- Manifest errors
and many more.
This is how I did it with Android 3.3.
Check in any unsaved changes to your repository.
Right click your App's module -> Refactor -> Remove Unused Resources -> Preview
In the Refactoring Preview, collapse both the views ('Items to be deleted' & 'Unused Resource Declarations')
Right click 'Items to be deleted' -> Exclude
Right click 'Unused Resource Declarations' -> Exclude
Now expand 'Unused Resource Declarations' and under that locate your app specific strings.xml (there would be multiple strings.xmls)
Right click that strings.xml -> Include
Do Refactor! All unused strings from the xml file are deleted!
Note: Try building the project. If compilation fails, its most likely that these strings.xml is being referred from some layout/menu xmls, which themselves are unused.
So those layout xmls can also be deleted manually!
Build and run. Test!
To check string.xml.
It's easy (at least in my version of Eclipse)
In Eclipse for Android (I have version v22.6.2-1085508)
Right click on the project name in "Package explorer"
Select "Android Tools".
Select "Run Lint: Check for common Errors".
Now when you open strings.xml, you will see that unused string are highlighted.
You can fix other potential issues.
In Android Studio Press
Ctlr+Alt+Shift+i
Select -> Unused resources
It shows you unused unused strings and icons.
Thanks Happy coding :)
Run this script from root of your project.
for resourcefile in `find res/values/*.xml`; do
for stringname in `grep '.*/\1/g'`; do
count1=`grep -rc "R.string.${stringname}" src | egrep -v ':0$' | wc -l`
count2=`grep -rc "#string/${stringname}" res/layout | egrep -v ':0$' | wc -l`
count3=`grep -rc "#string/${stringname}" res/menu | egrep -v ':0$' | wc -l`
count4=`grep -rc "#string/${stringname}" AndroidManifest.xml | egrep -v '^0$' | wc -l`
count5=`grep -rc "#string/${stringname}" res/xml | egrep -v ':0$' | wc -l`
if [ $count1 -eq 0 -a $count2 -eq 0 -a $count3 -eq 0 -a $count4 -eq 0 -a $count5 -eq 0 ]; then
echo $resourcefile : $stringname
fi
done
done
for resourcename in `find res/drawable* -type f -name '*.???'`; do
resource=`echo $resourcename | xargs basename | sed "s/^\(.*\)\....$/\1/g"`
count1=`grep -rc "R\.drawable\.${resource}" src | egrep -v ':0$' | wc -l`
count2=`grep -rc "#drawable/${resource}" res/layout | egrep -v ':0$' | wc -l`
count3=`grep -rc "#drawable/${resource}" res/drawable*/*.xml | egrep -v ':0$' | wc -l`
count4=`grep -rc "#drawable/${resource}" res/menu | egrep -v ':0$' | wc -l`
count5=`grep -rc "#drawable/${resource}" AndroidManifest.xml | egrep -v '^0$' | wc -l`
if [ $count1 -eq 0 -a $count2 -eq 0 -a $count3 -eq 0 -a $count4 -eq 0 -a $count5 -eq 0 ]; then
echo $resourcename
fi
done
for resourcename in `find res/layout/*.xml`; do
resource=`echo $resourcename | xargs basename | sed "s/^\(.*\)\....$/\1/g"`
count1=`grep -rc "R\.layout\.${resource}" src | egrep -v ':0$' | wc -l`
if [ $count1 -eq 0 ]; then
echo $resourcename
fi
done
It gives me this kind of output:
res/values/activity_strings.xml : activity_more
res/values/activity_strings.xml : activity_as_reply_to
res/values/db_strings.xml : sql_backlog_count
res/values/db_strings.xml : sql_backlog_update_last_resend
...
For missing translation only:
Using InteliJ, click on the panel bar of your InteliJ: "Analyze" > "Run Inspection by Name" > Type in: Incomplete translation

Categories

Resources