原理基於上篇的多渠道打包工具,我們使用apktool解壓需要驗證的apk檔案後,去讀AndroidManifest.xml,當讀到渠道號哪一行的時候輸出即可。
源碼如下:
Main.java
package com.Market5577.channelVerifyTool;public class Main { public static void main(String[] args) { System.out .println("=====**====Code by H3c=====**======"); System.out.println("==**==渠道驗證工具==**=="); if (args.length != 1) { System.out .println("==ERROR==usage:java -jar channelV.jar apkDirectory======"); System.out .println("==INFO==Example: java -jar channelV.jar /apps======"); return; } String apk = args[0]; SplitApk sp = new SplitApk(apk); sp.mySplit(); }}
SplitApk.java
package com.Market5577.channelVerifyTool;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;public class SplitApk { HashMap<String, String> qudao = new HashMap<String, String>();// 渠道號,渠道名 String curPath;// 當前檔案夾路徑 String apkDirectory; public SplitApk(String directory) { this.curPath = new File("").getAbsolutePath(); this.apkDirectory = directory; } public void mySplit() { File dire = new File(apkDirectory); if (!dire.exists()) { System.out.println("沒有該檔案"); return; } if (dire.isDirectory()) { File[] sonFile = dire.listFiles(); for (File file : sonFile) { modifyXudao(file.getAbsolutePath()); } } else { modifyXudao(apkDirectory); } System.out.println("====Over===="); } /** * apktool解壓apk,替換渠道值 * * @throws Exception */ private void modifyXudao(String apkName) { // 解壓 /C 執行字串指定的命令然後終斷 String cmdUnpack = "cmd.exe /C java -jar apktool.jar d -f -s " + apkName; runCmd(cmdUnpack); String[] apkFilePath = apkName.split("\\\\"); String shortApkName = apkFilePath[apkFilePath.length - 1]; String dir = shortApkName.split(".apk")[0]; File packDir = new File(dir);// 獲得解壓的apk目錄 String f_mani = packDir.getAbsolutePath() + "\\AndroidManifest.xml"; File manifest = new File(f_mani); for (int i = 0; i < 10; i++) { if (manifest.exists()) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } if (!manifest.exists()) { System.out.println("====驗證失敗======"); } /* * 遍曆map,複製manifese進來,修改後打包,簽名,儲存在對應檔案夾中 */ BufferedReader br = null; FileReader fr = null; String keyLine = null; try { fr = new FileReader(manifest); br = new BufferedReader(fr); String line = null; while ((line = br.readLine()) != null) { if (line.contains("\"BaiduMobAd_CHANNEL\"")) { // 關鍵代碼===我這裡是用的百度統計工具 keyLine = line; } } } catch (Exception e) { e.printStackTrace(); System.out.println("====驗證失敗======"); } finally { try { if (fr != null) { fr.close(); } if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } if (keyLine != null) { String tmps[] = keyLine.split("\\\""); System.out.println("讀到的渠道是:" + tmps[3]); } else { System.out.println("====驗證失敗,請關閉======"); } // 刪除中途檔案 String cmdKey = String.format("cmd.exe /C rd /s/q %s", dir); runCmd(cmdKey); } /** * 執行指令 * * @param cmd */ public void runCmd(String cmd) { Runtime rt = Runtime.getRuntime(); BufferedReader br = null; InputStreamReader isr = null; try { Process p = rt.exec(cmd); // p.waitFor(); isr = new InputStreamReader(p.getInputStream()); br = new BufferedReader(isr); String msg = null; while ((msg = br.readLine()) != null) { System.out.println(msg); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (isr != null) { isr.close(); } if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
產生jar後寫一個批處理即可:
@echo off::set /p var=請拖入apk:::java -jar cVerify.jar %var%java -jar cVerify.jar C:\Users\Harris\Desktop\rePackTool\apkecho.&echo 請按任意鍵退出...&pause>nulexit
該工具支援檔案和檔案夾的拖入~
全文完