批量解包打包签名apk文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ReplaceStr {
	static String replaceStr;
	static String channel;//渠道号数字部分
	static String channelPre;//渠道号前缀
	static int channelMax;//渠道数量
	static String unpacked="unpacked";
	static String packed="packed";
	static String signed="signed";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		channelPre= PropertyHelper.getKeyValue("channelPre");
		channelMax=Integer.parseInt(PropertyHelper.getKeyValue("channelMax"));
		step1();
		step2();
		step3();
		step4();
	}

	private static void step4() {
		// TODO Auto-generated method stub
		System.out.println("清空临时文件夹→→→→→");
			Utils.deleteFolder(unpacked);
		
		List<String> strings= readFiles(packed);
		for(String string:strings){
			System.out.println("当前文件名:"+string);
			if(string.contains(".apk")){
				new File("packed\\"+string).delete();		
			}

		}
		System.out.println("临时文件清理完成!");
	}

	private static void step1() {
		Utils.createFolder(unpacked);
		Utils.createFolder(signed);

		System.out.println("第一步:批量解包→→→→→");
		String dir="apk";
		List<String> strings= readFiles(dir);
		for(String string : strings){
			String temp="cmd /c apktool d \"apk\\"+string+"\" \"unpacked\\"+string.split("\\.")[0]+"\"";
			System.out.println("执行的cmd语句:"+temp);
			runbat(temp);			
		}
	}

	private static void step2() {
		System.out.println("第二步:批量替换并打包中→→→→→");
		for (int i = 1; i <= channelMax; i++) {
			if(i<10){
				channel="0"+i;
			}else{
				channel=String.valueOf(i);
			}
			System.out.println("正在操作渠道"+channelPre+channel+"→→→→→");
			System.out.println("批量替换Strings.xml的fuid中→→→→→");
//			PropertyHelper.writeProperties("anqu", "anqu01");
			String dirs = "unpacked";
			List<String> stringss = readAbFiles(dirs);
			for (String temp : stringss) {
				File file = new File(temp + "\\res\\values\\strings.xml");
					//正则替换
				regularExp("", "<string name=\"fuid\">"+channelPre+channel+"</string>", file);
			}
			System.out.println("批量打包中→→→→→");
			List<String> s_unpacks = readFiles("unpacked");
			for (String string : s_unpacks) {
				String temp = "cmd /c apktool b \"unpacked\\" + string + "\" \"packed\\" + string +channelPre+channel+ ".apk\"";
				System.out.println("执行的cmd语句:" + temp);
				runbat(temp);
			}
		}
	}

	private static void step3() {
		System.out.println("第四步:批量签名中→→→→→");
		List<String> s_packedApks= readFiles("packed");
		for(String string : s_packedApks){
			if(!string.contains(".apk")){
				continue;
			}
			String temp="cmd /c java -jar packed\\signapk.jar packed\\testkey.x509.pem packed\\testkey.pk8 \"packed\\"+string+"\" \"signed\\"+string+"\"";
			System.out.println("执行的cmd语句:"+temp);
			runbat(temp);			
		}
		System.out.println("签名完成,存放于signed文件夹中。");
	}

	private static void replace(File file,String searchStr,String replaceStr) {
		// TODO Auto-generated method stub
	        if(searchStr == null){
	            return;
	        }
	        try{
/*	            FileReader reader = new FileReader(file);
	            char[] dates = new char[1024];
	            int count = 0;
	            StringBuilder sb = new StringBuilder();
	            while((count = reader.read(dates)) > 0){
	                String str = String.valueOf(dates, 0, count);
	                sb.append(str);
	            }
	            reader.close();*/
	            
	            
	            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
                StringBuffer sbread = new StringBuffer();
                while (isr.ready()) {
                	sbread.append((char) isr.read());
                }
                isr.close();
	            // 从构造器中生成字符串,并替换搜索文本
	            String str = sbread.toString().replace(searchStr, replaceStr);
/*	            FileWriter writer = new FileWriter(file);
	            writer.write(str.toCharArray());
	            writer.close();*/
	            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
	            out.write(str.toCharArray());
	            out.flush();
	            out.close();
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        System.out.println("替换完成!");
	}
	/**
	 * 运行cmd命令
	 */
	public static boolean exec(String[] cmdAry) throws Exception {
		Runtime rt = Runtime.getRuntime();
		Process proc = null;
		try {
			proc = rt.exec(cmdAry); 
			/* 
			 * Runtime的exec()方法类似线程,不会在cmd命令执行完成后再继续运行下面的代码,
			 * 所以导致可能cmd命令还没执行完毕,程序就运行到了Process的destroy()方法,因
			 * 此需要一个方法去等待cmd命令执行完毕后,再运行exec()之后的方法
			 */
			return waitForProcess(proc) > 0;
		} finally {
			if (proc != null) {
				proc.destroy();
				proc = null;
			}
		}
	}
	
	/**
	 * 得到cmd命令返回的信息数据流,该流的运行周期与cmd命令的实行时间相同
	 */
	public static int waitForProcess(Process proc) throws Exception {
		// cmd命令有返回正确的信息流,和错误信息流,不过不能绝对表示cmd命令是否执行正确
		BufferedReader in = null;
		BufferedReader err = null;
		String msg = null;
		int exitValue = -1;
		try {
			in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
			while ((msg = in.readLine()) != null) {
				System.out.println(msg);
				if (1 != exitValue) {
					exitValue = 1;
				}
			}
			err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
			while ((msg = err.readLine()) != null) {
				System.out.println(msg);
				if (0 != exitValue) {
					exitValue = 0;
				}
			}
			return exitValue;
			
		} finally {
			if (null != in) {
				in.close();
				in = null;
			}
			if (null != err) {
				err.close();
				err = null;
			}
		}
	}
	  public static void runbat(String bat) {

	        try {
	            Process process = Runtime.getRuntime().exec(bat);
	            process.waitFor( ); 
//	            System.out.println(ps.getInputStream());
	        } catch(IOException ioe) {
	            ioe.printStackTrace();
	        } catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }  

	  public static List<String> readFiles(String filePath) {  
	        File f = null;  
	        f = new File(filePath);  
	        File[] files = f.listFiles(); // 得到f文件夹下面的所有文件。  
	        List<File> list = new ArrayList<File>();  
	        List<String> strings=new ArrayList<String>();
	        for (File file : files) {  
	            list.add(file);  
	        }  
	        for(File file : files) {  
	            System.out.println(file.getName());
	            strings.add(file.getName());
	            
	        }  
	        return strings;
	    }  
	  public static List<String> readAbFiles(String filePath) {  
		  File f = null;  
		  f = new File(filePath);  
		  File[] files = f.listFiles(); // 得到f文件夹下面的所有文件。  
		  List<File> list = new ArrayList<File>();  
		  List<String> strings=new ArrayList<String>();
		  for (File file : files) {  
			  list.add(file);  
		  }  
		  for(File file : files) {  
			  strings.add(file.getAbsolutePath());
			  
		  }  
		  return strings;
	  }  
	  
	/**
	 * @param regex 正则
	 * @param to 要替换成的内容
	 * @param file 被替换的文件
	 */
	public static void regularExp(String regex,String to,File file){
//			     regex = "^\"fuid\">(.*)</string>$";      
			     regex = "<string name=\"fuid\">(.*?)</string>";      
/*			     source = "<a href=\"http://***.***.***" onclick=\"co('**')\" class=\"lr\">***</a>";*/
				String source=Utils.read_UTF8_FileContent(file);
			    Matcher matcher = Pattern.compile(regex).matcher(source);
			    while (matcher.find()) {
			        System.out.println("匹配的group():"+matcher.group());
			        replace(file, matcher.group(),to);
			        
			    }
			/*output:
			href="http://***.***.*" onclick="co('**')" class="lr">***
			*/
	}
}

转载于:https://my.oschina.net/u/150685/blog/281739

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值