下一步就是签名,先把签名文件放在这个目录下,签名的命令是:jarsigner -verbose -keystore ***.jks -signedjar signMyTest.apk MyTest.apk AliasName -storepass **** -keypass ****
如果签名也完成了,基本上就没什么问题了,接下来就是写代码来完成这些操作了。
我使用IntelliJ来完成的,以下呢就是利用IJ来生成jar包。先写代码再打成jar包。
打包的代码如下:
public static void main(String[] args) {
System.out.println("====**====By cherry=====**======");
if (args.length != 5) {
System.out
.println("==ERROR==usage:java -jar rePack.jar apkName keyFile keyPasswd aliasName storePasswd======");
System.out
.println("==INFO==Example: java -jar rePack.jar test.apk android.keystore 123456 android 123456======");
return;
}
String apk = args[0];
String keyFile = args[1];
String keyPasswd = args[2];
String aliName = args[3];
String storePasswd = args[4];
SplitApk sp = new SplitApk(apk, keyFile, keyPasswd,aliName,storePasswd);
sp.mySplit();
}
SplitApk代码如下:
ArrayList<String> channel = new ArrayList<String>();
String curPath;//
String apkName; //
String keyFile; //
String keyPasswd; //
String aliName;
String storePass;
public SplitApk(String apkName, String keyFile, String keyPasswd,String aliName,String storePass) {//
this.curPath = new File("").getAbsolutePath();
this.apkName = apkName;
this.keyFile = keyFile;
this.keyPasswd = keyPasswd;
this.aliName = aliName;
this.storePass = storePass;
}
public void mySplit() {
getCannelFile();//
modifyXudao();//
}
/**
*
*/
private void getCannelFile() {
File f = new File("channel.txt");//
if (f.exists() && f.isFile()) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(f);
br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
String[] array = line.split("\t");//
channel.add(array[0]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
}
}
/**
*
*
* @throws Exception
*/
private void modifyXudao() {
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];
System.out.println("shortApkName = "+shortApkName);
String dir = shortApkName.split(".apk")[0];
System.err.println("dir = "+dir);
File packDir = new File(dir);
String f_mani = packDir.getAbsolutePath() + "\\AndroidManifest.xml";
String f_mani_bak = curPath + "\\AndroidManifest.xml";
File manifest = new File(f_mani);
File manifest_bak = new File(f_mani_bak);
manifest.renameTo(manifest_bak);
for (int i = 0; i < 10; i++) {
if (manifest_bak.exists()) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (manifest_bak.exists()) {
} else {
}
File f = new File("apk");
if (!f.exists()) {
f.mkdir();
}
for (int i = 0; i < channel.size(); i++) {
String id = channel.get(i);
BufferedReader br = null;
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(manifest_bak);
br = new BufferedReader(fr);
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
if (line.contains("deacon_id")) {
line = line.replaceAll("deacon_id", id);
}
sb.append(line + "\n");
}
fw = new FileWriter(f_mani);
fw.write(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
String unsignApk = id + "_" + dir + "_un.apk";
String cmdPack = String.format(
"cmd.exe /C java -jar apktool.jar b %s -o %s", dir, unsignApk);
runCmd(cmdPack);
String signApk = "./apk/" + id + "_" + dir + ".apk";
String cmdKey = String
.format("cmd.exe /C jarsigner -verbose -keystore %s -signedjar %s %s %s -storepass %s -keypass %s",
keyFile, signApk, unsignApk,aliName,storePass,keyPasswd);
runCmd(cmdKey);
File unApk = new File(unsignApk);
unApk.delete();
}
String cmdKey = String.format("cmd.exe /C rd /s/q %s", dir);
runCmd(cmdKey);
manifest_bak.delete();
}
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包:
这位兄弟写的很详细可以参考,http://blog.csdn.net/fesdgasdgasdg/article/details/73556109
我就不说了,每个人习惯的ide不一样,我之前也用过jcreator,它的方式是这样子的。http://blog.csdn.net/ycyhjj2865/article/details/5485547。
注意:IJ打包成jar的时候,要配置Mani-Class,一定要写上包名。
1、打包完了以后呢,把jar包复制到跟apk同路径下,也就是apktool这个路径。
2、创建一个名为channel.txt的文件,在里面写上需要打包的渠道号,用换行符隔开。不要问为什么是channel.text文件,因为
SplitApk里面已经配置了,也可以变成你想要的名字。
我写了五个渠道。
此时,文件夹下的列表是这样子的。
3、打开cmd,在控制台输入 java -jar AutoPack.jar app-release.apk ***.jks jks的keyPassWord jks的AliasName
jks的storePasswd
打包成功的话会在当前文件夹中生成一个apk文件夹,打包成功的签名文件都会放在这个文件夹里面:
可以修改获取渠道号的文件名,可以修改mate-data中的替换的字段,多渠道打包就完成了。
文章最开始处的参考文章链接里面有批处理文件的解决方案,需要的可以去看看。再把参考文章链接发一次。http://blog.csdn.net/a641832648/article/details/52163493