Android lint 刪除無用圖片檔案和配置檔案
Android lint 刪除無用、冗餘的 配置檔案和 圖片資源
轉載請註明 http://blog.csdn.net/aaawqqq?viewmode=contents
Android專案經過長期的迭代開發 專案當中有大量無用的java類和冗餘圖片
如果不整理將會導致 apk 包比較大
審查 清理Java類 使用UCDetector 可以檢視我的上篇 博文
Android lint 是Android SDK 提供的程式碼檢查工具 主要檢查 配置檔案 資原始檔 發現程式碼問題
我的使用場景 :
減少Android apk 包大小
使用Android lint 發現無用圖片和xml 檔案 通過刪除冗餘資源
我在此給大家分享的是工具與技術
具體的知識 大家通過 其它博文去學習 可以檢視文章結尾的參考連結 我在這就不復述了1) 工具
1.1) Android lint 在 Android sdk tools 當中 如果可以希望大家能配好環境變數
本文主講 以命令列形式的刪除無效資源的批處理
1.2) 另在eclipse當中有lint外掛
此方法
優點:執行簡單 在eclipse的 直接顯示
缺點:需要手動刪除 當冗餘檔案數量多的適合 會很傷腦筋
2) 輸入
開啟命令列 使用lint命令
如圖:
lint --check "UnusedResources" /Users/baozi/Dev/android/android > result.txt
/Users/baozi/Dev/android/android > result.txt
/Users/baozi/Dev/android/android 是 工程的路徑 (工程名為 android )
生成的掃描結果將會存放在當前目錄下的 result.txt 當中
如我的目錄 /Users/baozi/result.txt
3) 輸出檔案result.txt
開啟檔案目錄 /Users/baozi/result.txt
4) 根據結果 批量刪除對應的檔案
本文重點 當你第一次執行時 發現需要數千資原始檔需要刪除的時候就會傷腦筋
手工逐條刪除 並不符合程式猿三大優秀品質 : 懶惰 沒有耐心 驕傲
嘗試過使用 vim 刪除 發現操作起來也相當麻煩
大家可以參考下面的程式碼 使用FIle 獲取 result.txt 中的檔案資訊 呼叫 File .delete(); 方法刪除
/**
* 刪除 未使用的冗餘資源(圖片 xml佈局)
*
* @param b
*
* false 顯示資源列表
*
* true 顯示資源列表 並刪除資源
*
* @throws Exception
*/
private static void init(boolean b) throws Exception {
String encoding = "UTF-8"; // 字元格式
String projectPath = "/Users/baozi/Dev/shihui/android/";//Android工程所在地址
String filePath1 = "/Users/baozi";//result的所在路徑
File file = new File(filePath1, "result.txt");//獲取result.txt 檔案 生成地址
if (file.isFile() && file.exists()) { // 判斷檔案是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("UnusedResources") && !line.contains("res/value") && !line.contains("appcompat")
&& !line.contains("res/xml")) {
// System.out.println(line);
int end = line.indexOf(":");
if (end != -1) {
String file_end = line.substring(0, end);
String f = projectPath + file_end;
System.out.println(f);
if (b) {
new File(f).delete();
System.out.println("刪除成功");
}
}
}
}
read.close();
}
}
projectPath : Android工程在硬碟中的位置
filePath1 : lint 執行結果 result.txt 所在的位置
方法 引數 傳入false 僅列印結果 傳入true 列印結果 並刪除檔案
填入正確的地址 就能批量執行刪除未使用的 佈局 & 圖片 資源 (UnusedResources)
如果想要刪除其它操作 請修改 篩選條件
if (line.contains("UnusedResources") && !line.contains("res/value") && !line.contains("appcompat")
&& !line.contains("res/xml")
使用心得: 迴圈使用3-6次 能完成 刪除全部未使用的資源 但是有些廢棄的模組 存在程式碼以來關係 需要手工判斷刪除
附1:
使用eclipse自帶的 Android lint 外掛 審查程式碼的方式
使用方式:
右擊工程 → Android Tools → Run Lint: Check for Common Error
結果會在 Lint Warrings 當中顯示 和 看logcat 的方式相同
執行結果:
附2: 參考博文
http://blog.csdn.net/hudashi/article/details/8333349
結尾附上神獸
//┏┓ ┏┓
//┏┛┻━━━┛┻┓
//┃ ┃
//┃ ━ ┃
//┃ ┳┛ ┗┳ ┃
//┃ ┃
//┃ ┻ ┃
//┃ ┃
//┗━┓ ┏━┛
// ┃ ┃ 神獸保佑
// ┃ ┃ 程式碼無BUG!
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
每日精進 希望能幫上你