执行编译和签名命令
我把反编译和签名工具都放在了同一目录,并且事先把基础apk反编译好,现在只需要用代码来执行编译和签名命令就行了。在Java中可以通过Runtime类来执行DOS命令
private static void createApk(String apkName) throws IOException, InterruptedException {
File dir = new File(wpPath );
// 编译命令,其中azbz是基础apk反编译后的文件夹
String backCommand = "cmd /c apktool.bat b azbz " +apkName+".apk" ;
// 签名命令
String signCommand = "cmd /c java -jar signapk.jar platform.x509.pem platform.pk8 "+apkName+ ".apk " +apkName+"_signed.apk" ;
// 这个命令执行完成会生成一个未签名的 apk
Runtime backR = Runtime. getRuntime();
Process backP = backR.exec(backCommand, null , dir);
// 等待执行完再往下执行
backP.waitFor();
// 签名 apk, 这里使用的google提供的证书
Runtime signR = Runtime. getRuntime();
Process signP = signR.exec(signCommand, null , dir);
signP.waitFor();
}
下面是随手写的一个生成两个icon和名称不同的Apk例子
public class ExecDosCommand {
static String wpPath_app = "E:" +File. separator+ "decode apk"+File. separator+ "azbz" +File.separator ;
static String iconPath = wpPath_app +"res" +File. separator+ "drawable-hdpi"+File. separator ;
static String stringPath = wpPath_app +"res" +File. separator+ "values-zh-rCN"+File. separator +"strings.xml" ;
static String manifestPath = wpPath_app+ "AndroidManifest.xml";
static String wpPath = "E:" + File. separator + "decode apk"+File. separator;
public static void main(String[] args) throws Exception {
AndroidManifestParser parser = new AndroidManifestParser();
AppInfo appInfo = parser.parse( new FileInputStream( manifestPath));
for (int i = 0; i < 2; i++) {
coverIcon(appInfo, i);
modifyAppName(appInfo, i);
createApk( "修改"+(i+1));
}
}
private static void modifyAppName(AppInfo appInfo, int i) {
XmlModifyUtil. modifyXML( new File( stringPath ),
appInfo.getAppName(), "修改" +(i+1));
}
private static void coverIcon(AppInfo appInfo, int i)
throws FileNotFoundException, IOException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(iconPath +appInfo.getIconName()+ ".png"));
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(wpPath +File. separator+ "image"+File. separator +"icon" +(i+1)+".png" ));
byte [] buffer = new byte[1024];
int temp = 0;
while ((temp = bis.read(buffer)) != -1 ){
bos.write(buffer, 0, temp);
}
bos.flush();
bos.close();
bis.close();
}
private static void createApk(String apkName) throws IOException, InterruptedException {
File dir = new File(wpPath );
// 编译命令
String backCommand = "cmd /c apktool.bat b azbz " +apkName+".apk" ;
// 签名命令
String signCommand = "cmd /c java -jar signapk.jar platform.x509.pem platform.pk8 "+apkName+ ".apk " +apkName+"_signed.apk" ;
// 这个命令执行完成会生成一个未签名的 apk
Runtime backR = Runtime .getRuntime();
Process backP = backR.exec(backCommand, null , dir);
// 等待执行完再往下执行
backP.waitFor();
// 签名 apk, 这里使用的google提供的证书
Runtime signR = Runtime .getRuntime();
Process signP = signR.exec(signCommand, null , dir);
signP.waitFor();
}