編輯:關於 Android 程式設計
需求一改再改、UI 一調再調,專案裡往往留下一堆再也沒人參照的圖片、版面配置與字串。它們不只是讓 APK 變胖,對接手程式碼的人來說更是無形的負擔。問題是:一個稍具規模的工程,靠人力逐一比對根本不現實,因此我們需要一套自動化的清理流程。
整個腳本分成三個階段:
--check UnusedResources,把未使用資源的報告輸出成 XML;drawable、layout、anim、menu 等檔案型資源,直接刪除檔案;res/values 底下未被使用的 color、string、string-array 節點,透過 DOM 節點比對移除,再寫回原檔。第三步之所以要用 DOM 而不是字串取代,是因為 values 檔案是結構化的 XML,逐行正則替換很容易把格式弄壞;用 DocumentBuilder 解析後再序列化,才不會留下破碎的標籤。
public static void doDocCheck(String projectPath, String resultPath)
throws IOException, InterruptedException {
String cmd = "cmd /c lint --check UnusedResources " + projectPath
+ " > " + resultPath;
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
}
逐行掃描報告,只挑出帶有 UnusedResources 標記、且排除 appcompat 與 res/values 的行,取冒號前的路徑拼上專案根目錄後刪除。
public static void clearResources(String projectPath, String resultPath)
throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(resultPath));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("UnusedResources")
&& !line.contains("appcompat")
&& !line.contains("res\\values")) {
int end = line.indexOf(':');
if (end != -1) {
String f = projectPath + line.substring(0, end);
new File(f).delete();
}
}
}
}
做法是先把報告中涉及的 color、string、string-array 收集成一份「待刪清單」XML,接著解析 res/values 下的每個檔案,逐一比對 name 屬性;一旦命中清單就把該節點從 DOM 樹中移除,最後再序列化寫回檔案。
public static void clear(File clearFile, File checkFile)
throws ParserConfigurationException, SAXException, IOException {
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(clearFile);
Element rootElement = document.getDocumentElement();
NodeList childNodes = rootElement.getChildNodes();
Document document2 = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(checkFile);
NodeList childNodes2 = document2.getDocumentElement().getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if (childNode.getNodeType() != Node.ELEMENT_NODE) continue;
for (int j = 0; j < childNodes2.getLength(); j++) {
Node childNode2 = childNodes2.item(j);
if (childNode2.getNodeType() != Node.ELEMENT_NODE) continue;
if (childNode.getAttributes().getNamedItem("name").getNodeValue()
.equals(childNode2.getAttributes()
.getNamedItem("name").getNodeValue())) {
rootElement.removeChild(childNode);
}
}
}
writeFile(clearFile.getAbsolutePath(), serialize(rootElement));
}
序列化時需要自己走訪節點樹(遞迴輸出標籤、屬性與文字節點),把結果組回一份格式正確的 XML 字串後覆寫原檔。
用 Android Studio 與 Gradle 一次搞定多渠道打包
用 Gradle 做多渠道打包,體驗確實如傳聞中順手。以下把整個流程整理成一份可照著做的操作筆記。 第一步:在 AndroidManifest.xml 中埋入渠道佔位符
Android 多點觸控圖片縮放、平移與慣性滑動實作
在圖片瀏覽、相簿與編輯類應用中,多點觸控是非常常見的需求。使用者通常希望能縮放圖片、拖動畫面、雙擊放大,並在放開手指後依速度自然滑動。Android 提供了 ScaleG
Android 圖片選擇器:配置豐富、易於擴充的開源庫
最近專案剛好需要,我便動手做了一個 Android 圖片選擇器的庫。它支援圖庫多選/單選、圖片裁剪、拍照,以及自訂圖片載入庫,大幅簡化了使用流程。 截圖 主要優點 1、
Android 網路請求實戰:POST 與 GET 的實作與差異
Android 應用幾乎都離不開與伺服器互動,而互動的本質就是手機客戶端送出網路請求。下面先釐清最常見的兩種請求方式——POST 與 GET——的差別,再分別用它們實作一