多くの文字列がで宣言されている巨大なAndroidプロジェクトがありますstrings.xml。で未使用の文字列を削除したかったstrings.xml。
そうする簡単な方法はありますか?
多くの文字列がで宣言されている巨大なAndroidプロジェクトがありますstrings.xml。で未使用の文字列を削除したかったstrings.xml。
そうする簡単な方法はありますか?
回答:
上のAndroidメーカー:
メニュー -> 分析 -> 名前による検査の実行 ->未使用のリソース
[ファイルマスク]チェックボックスをオンにstrings.xmlして、テキストフィールドに入力します。
tools:ignore="UnusedResources"
これはかなり簡単な別のソリューションです。Android Studioメニューで、
リファクタリング>未使用のリソースを削除...。
[ プレビュー]をクリックして、未使用のリソースを確認し、選択的に削除します。
ADT 16を使用すると、できる限り簡単に実行できます。ADT 16に更新し、Android Lintを使用します。それは本当に素晴らしいツールです。それはすべての未使用のリソース(文字列だけでなく)を見つけることができます。その公式サイトから:
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.
Warning: The resource R.string.... appears to be unused [UnusedResources] [lint] <string name="...">some text</string>
これは、Android 3.3で行った方法です。
保存されていない変更をリポジトリにチェックインします。
注:プロジェクトをビルドしてみてください。コンパイルが失敗した場合、これらのstrings.xmlは、それら自体が使用されていないレイアウト/メニューxmlから参照されている可能性が高いです。したがって、これらのレイアウトxmlは手動で削除することもできます!
ビルドして実行します。テスト!
プロジェクトのルートからこのスクリプトを実行します。
`find res / values / *。xml`のリソースファイル用; 行う
`grep '。* / \ 1 / g'`の文字列名の場合; 行う
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]; その後
echo $ resourcefile:$ stringname
fi
できた
できた
`find res / drawable * -type f -name '*。???'`のリソース名の場合 行う
resource = `echo $ resourcename | xargsベース名| 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]; その後
echo $ resourcename
fi
できた
`find res / layout / *。xml`のリソース名用; 行う
resource = `echo $ resourcename | xargsベース名| sed "s / ^ \(。* \)\ .... $ / \ 1 / g" `
count1 = `grep -rc" R \ .layout \。$ {resource} "src | egrep -v ':0 $' | wc -l`
if [$ count1 -eq 0]; その後
echo $ resourcename
fi
できた
それは私にこの種の出力を与えます:
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 ...
Custom scope、選択Production classes- >app- >values未使用の文字列、色を見つけるために...