java服务器动态打包apk 代码,可以动态吧一些数据写入raw文件里面,然后,自动使用签名,打包代码为可以安装的apk包
使用这种方式可以 很好实现 用户推广包的制作
public static void getAppUrl(Long uid1, long uid2, long uid3,long uid4,long uid5,
DaoBaoOnErrorListener listener) throws IOException { //uid1.。。。推广用户的id号。
OutputStreamWriter osw = null;
Process process = null;
try {
String path = "D:\\Java\\apktool\\apktool"; //apktool的路径
String appPath = "D:\\myeclipse_workplace\\.metadata\\.me_tcat7\\webapps\\MyMoney\\apk\\"; //需要打包的apk的地址
File file = new File(path);
process = Runtime.getRuntime().exec(
"cmd.exe /c apktool d " + appPath + "app.apk " + appPath
+ "app", null, file); //这个命令,先解压apk包
if (process.waitFor() != 0) {
// listener.onErrorListener("解压失败");
}
String targetPath = appPath + "\\app\\res\\raw\\id.txt"; //解压后的raw下面的文本文件路径,我们把id写里面,apk安装后,从里面取出,写入服务器,用户和推广用户绑定
String content = uid1 + "," + uid2 + "," + uid3+","+uid4+","+uid5;
osw = new OutputStreamWriter(new FileOutputStream(targetPath));
osw.write(content, 0, content.length());
osw.flush(); //把推广用户id写入文本里面去
process = Runtime.getRuntime().exec(
"cmd.exe /c apktool b " + appPath + "app " + appPath
+ "dabao.apk", null, file);
if (process.waitFor() != 0) {
listener.onErrorListener("打包失败");
return;
}
String jdkBinPath = "D:\\Java\\jdk1.7.0_67\\bin"; //jdk路径
File bin = new File(jdkBinPath);
String cmd1 = "cmd.exe /c jarsigner -keystore D:\\Java\\qianming\\my.keystore -storepass 123456 -signedjar D:\\myeclipse_workplace\\.metadata\\.me_tcat7\\webapps\\MyMoney\\apk\\"; // 含有秘钥地址和密码
String cmd2 = uid1 + ".apk";
String cmd3 = " -digestalg SHA1 -sigalg MD5withRSA D:\\myeclipse_workplace\\.metadata\\.me_tcat7\\webapps\\MyMoney\\apk\\dabao.apk my.keystore";
process = Runtime.getRuntime().exec(cmd1 + cmd2 + cmd3, null, bin);
if (process.waitFor() != 0) {
listener.onErrorListener("签名失败");
return;
}
} catch (Exception e) {
e.printStackTrace();
listener.onErrorListener("打包失败");
} finally {
osw.close();
}
listener.onSuccess();
}
package com.hck.dabao;
public interface DaoBaoOnErrorListener {
public void onErrorListener(String errorString);
public void onSuccess();
}
我的网站的 apk,就是用这个方式动态打包产生的: http://androidyuanma.com/downApp?uid=1