`

Java之apk 解压、修改、打包、签名(1)--调用工具

阅读更多
场景:
     页面上传APK包及修改的内容(修改文件固定),修改完成后提供下载链接下载。


实现步骤:
1、通过apktool工具,对APK包进行解压及打包(参考:http://showlike.iteye.com/admin/blogs/1686103)

2、文件内容的修改

3、通过JDK的jarsigner对修改后打包的APK进行签名(参考:http://www.tttabc.com/android/keytool-keystore-jarsigner-apk.htm


具体实现:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


/**
 * @author showlike
 * @version v1.0 
 * 2012-6-29 下午12:05:10 
 */
public class Test {
	public static void main(String args[]) throws IOException{
		BufferedReader br =null;
		OutputStreamWriter osw =null;
		Process process = null;
		try {
			
			//1----解压
			//apktool路径
			String path = "D:\\My Documents\\Desktop\\apktool-install-windows-r04-brut1";
			//保存路径
			String appPath = "F:\\document\\APK\\new\\";
			File file =new File(path);
			
			process = Runtime.getRuntime().exec("cmd.exe /c apktool d "+appPath+"iGouShop.apk "+appPath+"app",null,file);
			if(process.waitFor()!=0)System.out.println("解压失败。。。");
			
			//2----内容修改
			String targetPath = appPath+"app\\res\\raw\\channel";
			String content="TT201209291653";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(targetPath)));
			while((br.readLine())!=null)
			{
				osw = new OutputStreamWriter(new FileOutputStream(targetPath));  
				osw.write(content,0,content.length());  
				osw.flush();  
			}
			
			//3----打包
			process = Runtime.getRuntime().exec("cmd.exe /c apktool b "+appPath+"app "+appPath+"app.apk",null,file);
			if(process.waitFor()!=0)System.out.println("打包失败。。。");
			
			//4----签名 (文件名称中不能包含空格)
			String jdkBinPath="C:\\Program Files\\Java\\jdk1.6.0_26\\bin";
			File bin =new File(jdkBinPath);
			String cmd = "cmd.exe /c jarsigner -keystore F:\\document\\APK\\sdk.keystore -storepass winadsdk -signedjar "+appPath+"appT.apk "+appPath+"\\app.apk"+" sdk.keystore";
			process = Runtime.getRuntime().exec(cmd,null,bin);
			if(process.waitFor()!=0)System.out.println("签名失败。。。");
		}catch (Exception e)
		{
			e.printStackTrace();
		}finally{
			br.close();
			osw.close();
		}
	}
}
分享到:
评论
2 楼 chishangyang 2015-08-31  
签名的时候需要输入密钥库密码短语,你这是怎么解决的
1 楼 sunsongwen2 2015-03-03  
//3----打包 
            process = Runtime.getRuntime().exec("cmd.exe /c apktool b "+appPath+"app "+appPath+"app.apk",null,file);


您好,可以解释下 "cmd.exe /c apktool b "+appPath+"app "+appPath+"app.apk",null,file 这句话的意思么

相关推荐

Global site tag (gtag.js) - Google Analytics