Skip to content

Commit

Permalink
refactor(core): remove duplicated codes for invoking CMD
Browse files Browse the repository at this point in the history
ref #218
  • Loading branch information
simpleton committed Jan 26, 2018
1 parent 026b1be commit 48e055f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,7 @@ private void signWithV1sign(File unSignedApk, File signedApk) throws IOException
config.mStoreAlias
};
dumpParams(argv);
Process pro = null;
try {
pro = Runtime.getRuntime().exec(argv);
//destroy the stream
pro.waitFor();
System.out.print(pro.exitValue());
if (pro.exitValue() != 0) {
System.err.println("Jarsigner Failed! Please check your signature file.\n");
throw new RuntimeException(StringUtil.readInputStream(pro.getInputStream()));
}
} finally {
if (pro != null) {
pro.destroy();
}
}
Utils.runExec(argv);
}

private void dumpParams(String[] params) {
Expand Down Expand Up @@ -297,12 +283,7 @@ private void alignApk(File before, File after) throws IOException, InterruptedEx
before.getAbsolutePath()));
}
String cmd = Utils.isPresent(config.mZipalignPath) ? config.mZipalignPath : TypedValue.COMMAND_ZIPALIGIN;
ProcessBuilder pb = new ProcessBuilder(cmd, "4", before.getAbsolutePath(), after.getAbsolutePath());
Process pro = pb.start();

//destroy the stream
pro.waitFor();
pro.destroy();
Utils.runCmd(cmd, "4", before.getAbsolutePath(), after.getAbsolutePath());
if (!after.exists()) {
throw new IOException(
String.format("can not found the aligned apk file, the ZipAlign path is correct? path=%s", mAlignedApk.getAbsolutePath())
Expand Down Expand Up @@ -385,41 +366,22 @@ private void addNonSignatureFiles(List<File> collectFiles, File metaFolder) {
}

private void addStoredFileIn7Zip(ArrayList<String> storedFiles, File outSevenZipAPK) throws IOException, InterruptedException {
System.out.printf("[addStoredFileIn7Zip]rewrite the stored file into the 7zip, file count:%d\n", storedFiles.size());
System.out.printf("[addStoredFileIn7Zip]rewrite the stored file into the 7zip, file count: %d\n", storedFiles.size());
if (storedFiles.size() == 0) return;
String storedParentName = mOutDir.getAbsolutePath() + File.separator + "storefiles" + File.separator;
String outputName = m7zipOutPutDir.getAbsolutePath() + File.separator;
for (String name : storedFiles) {
FileOperation.copyFileUsingStream(new File(outputName + name), new File(storedParentName + name));
}
storedParentName = storedParentName + File.separator + "*";
//极限压缩
String cmd = Utils.isPresent(config.m7zipPath) ? config.m7zipPath : TypedValue.COMMAND_7ZIP;
ProcessBuilder pb = new ProcessBuilder(cmd, "a", "-tzip", outSevenZipAPK.getAbsolutePath(), storedParentName, "-mx0");
Process pro = pb.start();

InputStreamReader ir = new InputStreamReader(pro.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//如果不读会有问题,被阻塞
while (input.readLine() != null) { }
//destroy the stream
pro.waitFor();
pro.destroy();
Utils.runCmd(cmd, "a", "-tzip", outSevenZipAPK.getAbsolutePath(), storedParentName, "-mx0");
}

private void generalRaw7zip(File outSevenZipApk) throws IOException, InterruptedException {
String outPath = m7zipOutPutDir.getAbsoluteFile().getAbsolutePath();
String path = outPath + File.separator + "*";
//极限压缩
String cmd = Utils.isPresent(config.m7zipPath) ? config.m7zipPath : TypedValue.COMMAND_7ZIP;
ProcessBuilder pb = new ProcessBuilder(cmd, "a", "-tzip", outSevenZipApk.getAbsolutePath(), path, "-mx9");
Process pro = pb.start();

InputStreamReader ir = new InputStreamReader(pro.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//如果不读会有问题,被阻塞
while (input.readLine() != null) { }
//destroy the stream
pro.waitFor();
pro.destroy();
Utils.runCmd(cmd, "a", "-tzip", outSevenZipApk.getAbsolutePath(), path, "-mx9");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static boolean isBlank(final String string) {

public static String readInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
Expand Down
57 changes: 54 additions & 3 deletions AndResGuard-core/src/main/java/com/tencent/mm/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.tencent.mm.util;

import com.tencent.mm.androlib.res.util.StringUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* Created by sun on 1/9/16.
*/
public class Utils {
public static boolean isPresent(String str) {
return str != null && str.length() > 0;
Expand Down Expand Up @@ -44,6 +46,55 @@ public static void cleanDir(File dir) {
}
}

public static String runCmd(String... cmd) throws IOException, InterruptedException {
String output;
Process process = null;
try {
process = new ProcessBuilder(cmd).start();
output = StringUtil.readInputStream(process.getInputStream());
process.waitFor();
if (process.exitValue() != 0) {
System.err.println(
String.format("%s Failed! Please check your signature file.\n", cmd[0])
);
throw new RuntimeException(StringUtil.readInputStream(process.getErrorStream()));
}
} finally {
if (process != null) {
process.destroy();
}
}
return output;
}

public static String runExec(String[] argv) throws IOException, InterruptedException {
Process process = null;
String output;
try {
process = Runtime.getRuntime().exec(argv);
output = StringUtil.readInputStream(process.getInputStream());
process.waitFor();
if (process.exitValue() != 0) {
System.err.println(
String.format("%s Failed! Please check your signature file.\n", argv[0])
);
throw new RuntimeException(StringUtil.readInputStream(process.getErrorStream()));
}
} finally {
if (process != null) {
process.destroy();
}
}
return output;
}

private static void processOutputStreamInThread(Process process) throws IOException {
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//如果不读会有问题,被阻塞
while (input.readLine() != null) { }
}

private static String replaceEach(String text, String[] searchList, String[] replacementList) {
// TODO: throw new IllegalArgumentException() if any param doesn't make sense
//validateParams(text, searchList, replacementList);
Expand Down
2 changes: 1 addition & 1 deletion AndResGuard-example/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ org.gradle.jvmargs=-Xmx2584M
org.gradle.parallel=true
org.gradle.daemon=true

ANDRESGUARD_VERSION=1.2.11
ANDRESGUARD_VERSION=1.2.12-DEBUG
ANDRESGUARD_SEVENZIP_VERSION=1.2.11
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

ANDRESGUARD_VESSION=1.2.11
ANDRESGUARD_VESSION=1.2.12-DEBUG

0 comments on commit 48e055f

Please sign in to comment.