Android apk多渠道验证工具 - 不提供工具,只提供源码


             
           

原理基于上篇的多渠道打包工具,我们使用apktool解压需要验证的apk文件后,去读AndroidManifest.xml,当读到渠道号哪一行的时候输出即可。

源码如下:

Main.java

  1. package com.Market5577.channelVerifyTool;  
  2.   
  3. public class Main {  
  4.     public static void main(String[] args) {  
  5.         System.out  
  6.                 .println("=====**====Code by H3c=====**======");  
  7.         System.out.println("==**==渠道验证工具==**==");  
  8.   
  9.         if (args.length != 1) {  
  10.             System.out  
  11.                     .println("==ERROR==usage:java -jar channelV.jar apkDirectory======");  
  12.             System.out  
  13.                     .println("==INFO==Example: java -jar channelV.jar /apps======");  
  14.             return;  
  15.         }  
  16.   
  17.         String apk = args[0];  
  18.   
  19.         SplitApk sp = new SplitApk(apk);  
  20.         sp.mySplit();  
  21.     }  
  22. }  
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

  1. package com.Market5577.channelVerifyTool;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.util.HashMap;  
  9.   
  10. public class SplitApk {  
  11.     HashMap<String, String> qudao = new HashMap<String, String>();// 渠道号,渠道名  
  12.     String curPath;// 当前文件夹路径  
  13.     String apkDirectory;  
  14.   
  15.     public SplitApk(String directory) {  
  16.         this.curPath = new File("").getAbsolutePath();  
  17.         this.apkDirectory = directory;  
  18.     }  
  19.   
  20.     public void mySplit() {  
  21.         File dire = new File(apkDirectory);  
  22.         if (!dire.exists()) {  
  23.             System.out.println("没有该文件");  
  24.             return;  
  25.         }  
  26.   
  27.         if (dire.isDirectory()) {  
  28.             File[] sonFile = dire.listFiles();  
  29.             for (File file : sonFile) {  
  30.                 modifyXudao(file.getAbsolutePath());  
  31.             }  
  32.         } else {  
  33.             modifyXudao(apkDirectory);  
  34.         }  
  35.           
  36.         System.out.println("====Over====");  
  37.     }  
  38.   
  39.     /** 
  40.      * apktool解压apk,替换渠道值 
  41.      *  
  42.      * @throws Exception 
  43.      */  
  44.     private void modifyXudao(String apkName) {  
  45.         // 解压 /C 执行字符串指定的命令然后终断  
  46.         String cmdUnpack = "cmd.exe /C java -jar apktool.jar d -f -s "  
  47.                 + apkName;  
  48.         runCmd(cmdUnpack);  
  49.   
  50.         String[] apkFilePath = apkName.split("\\\\");  
  51.         String shortApkName = apkFilePath[apkFilePath.length - 1];  
  52.         String dir = shortApkName.split(".apk")[0];  
  53.         File packDir = new File(dir);// 获得解压的apk目录  
  54.   
  55.         String f_mani = packDir.getAbsolutePath() + "\\AndroidManifest.xml";  
  56.         File manifest = new File(f_mani);  
  57.   
  58.         for (int i = 0; i < 10; i++) {  
  59.             if (manifest.exists()) {  
  60.                 break;  
  61.             }  
  62.             try {  
  63.                 Thread.sleep(1000);  
  64.             } catch (InterruptedException e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.   
  69.         if (!manifest.exists()) {  
  70.             System.out.println("====验证失败======");  
  71.         }  
  72.   
  73.         /* 
  74.          * 遍历map,复制manifese进来,修改后打包,签名,存储在对应文件夹中 
  75.          */  
  76.         BufferedReader br = null;  
  77.         FileReader fr = null;  
  78.         String keyLine = null;  
  79.         try {  
  80.             fr = new FileReader(manifest);  
  81.             br = new BufferedReader(fr);  
  82.             String line = null;  
  83.             while ((line = br.readLine()) != null) {  
  84.                 if (line.contains("\"BaiduMobAd_CHANNEL\"")) { // 关键代码===我这里是用的百度统计工具  
  85.                     keyLine = line;  
  86.                 }  
  87.             }  
  88.         } catch (Exception e) {  
  89.             e.printStackTrace();  
  90.             System.out.println("====验证失败======");  
  91.         } finally {  
  92.             try {  
  93.                 if (fr != null) {  
  94.                     fr.close();  
  95.                 }  
  96.                 if (br != null) {  
  97.                     br.close();  
  98.                 }  
  99.             } catch (IOException e) {  
  100.                 e.printStackTrace();  
  101.             }  
  102.         }  
  103.   
  104.         if (keyLine != null) {  
  105.             String tmps[] = keyLine.split("\\\"");  
  106.             System.out.println("读到的渠道是:" + tmps[3]);  
  107.         } else {  
  108.             System.out.println("====验证失败,请关闭======");  
  109.         }  
  110.   
  111.         // 删除中途文件  
  112.         String cmdKey = String.format("cmd.exe /C rd /s/q %s", dir);  
  113.         runCmd(cmdKey);  
  114.     }  
  115.   
  116.     /** 
  117.      * 执行指令 
  118.      *  
  119.      * @param cmd 
  120.      */  
  121.     public void runCmd(String cmd) {  
  122.         Runtime rt = Runtime.getRuntime();  
  123.         BufferedReader br = null;  
  124.         InputStreamReader isr = null;  
  125.         try {  
  126.             Process p = rt.exec(cmd);  
  127.             // p.waitFor();  
  128.             isr = new InputStreamReader(p.getInputStream());  
  129.             br = new BufferedReader(isr);  
  130.             String msg = null;  
  131.             while ((msg = br.readLine()) != null) {  
  132.                 System.out.println(msg);  
  133.             }  
  134.         } catch (Exception e) {  
  135.             e.printStackTrace();  
  136.         } finally {  
  137.             try {  
  138.                 if (isr != null) {  
  139.                     isr.close();  
  140.                 }  
  141.                 if (br != null) {  
  142.                     br.close();  
  143.                 }  
  144.             } catch (IOException e) {  
  145.                 e.printStackTrace();  
  146.             }  
  147.         }  
  148.     }  
  149. }  
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后写一个批处理即可:

  1. @echo off  
  2. ::set /p var=请拖入apk:  
  3. ::java -jar cVerify.jar %var%  
  4. java -jar cVerify.jar C:\Users\Harris\Desktop\rePackTool\apk  
  5.   
  6. echo.&echo 请按任意键退出...&pause>nul  
  7. exit   
@echo off
::set /p var=请拖入apk:
::java -jar cVerify.jar %var%
java -jar cVerify.jar C:\Users\Harris\Desktop\rePackTool\apk

echo.&echo 请按任意键退出...&pause>nul
exit 


该工具支持文件和文件夹的拖入~

全文完

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值