diff --git a/patch/src/main/java/org/lsposed/patch/LSPatch.java b/patch/src/main/java/org/lsposed/patch/LSPatch.java index fcee9f1..88adf25 100644 --- a/patch/src/main/java/org/lsposed/patch/LSPatch.java +++ b/patch/src/main/java/org/lsposed/patch/LSPatch.java @@ -16,7 +16,6 @@ import org.lsposed.lspatch.share.Constants; import org.lsposed.patch.base.BaseCommand; import org.lsposed.patch.task.BuildAndSignApkTask; import org.lsposed.patch.util.ApkSignatureHelper; -import org.lsposed.patch.util.ZipUtils; import org.lsposed.patch.util.ManifestParser; import java.io.File; diff --git a/patch/src/main/java/org/lsposed/patch/task/BuildAndSignApkTask.java b/patch/src/main/java/org/lsposed/patch/task/BuildAndSignApkTask.java index e7592d3..0f9669f 100644 --- a/patch/src/main/java/org/lsposed/patch/task/BuildAndSignApkTask.java +++ b/patch/src/main/java/org/lsposed/patch/task/BuildAndSignApkTask.java @@ -6,8 +6,6 @@ import com.android.tools.build.apkzlib.zip.ZFile; import org.apache.commons.io.IOUtils; import org.lsposed.patch.LSPatch; -import org.lsposed.patch.util.ZipUtils; -import org.lsposed.patch.util.ShellCmdUtil; import java.io.File; import java.io.FileOutputStream; @@ -38,12 +36,7 @@ public class BuildAndSignApkTask implements Runnable { File unzipApkPathFile = new File(unzipApkFilePath); File keyStoreFile = new File(unzipApkPathFile, "keystore"); String keyStoreAssetPath; - if (isAndroid()) { - keyStoreAssetPath = "assets/android.keystore"; - } - else { - keyStoreAssetPath = "assets/keystore"; - } + keyStoreAssetPath = "assets/keystore"; try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(keyStoreAssetPath); FileOutputStream out = new FileOutputStream(keyStoreFile)) { @@ -66,57 +59,9 @@ public class BuildAndSignApkTask implements Runnable { } private boolean signApk(String apkPath, String keyStorePath, String signedApkPath) { - if (signApkUsingAndroidApksigner(apkPath, keyStorePath, signedApkPath, "123456")) { - return true; - } - if (isAndroid()) { - System.out.println(" Sign apk failed, please sign it yourself."); - return false; - } - try { - long time = System.currentTimeMillis(); - File keystoreFile = new File(keyStorePath); - if (keystoreFile.exists()) { - StringBuilder signCmd; - signCmd = new StringBuilder("jarsigner "); - signCmd.append(" -keystore ") - .append(keyStorePath) - .append(" -storepass ") - .append("123456") - .append(" -signedjar ") - .append(" " + signedApkPath + " ") - .append(" " + apkPath + " ") - .append(" -digestalg SHA1 -sigalg SHA1withRSA ") - .append(" key0 "); - System.out.println("\n" + signCmd + "\n"); - String result = ShellCmdUtil.execCmd(signCmd.toString(), null); - System.out.println(" sign apk time is :" + ((System.currentTimeMillis() - time) / 1000) + - "s\n\n" + " result=" + result); - return true; - } - System.out.println(" keystore not exist :" + keystoreFile.getAbsolutePath() + - " please sign the apk by hand. \n"); - return false; - } - catch (Throwable e) { - System.out.println("use default jarsigner to sign apk failed, fail msg is :" + - e.toString()); - return false; - } + return signApkUsingAndroidApksigner(apkPath, keyStorePath, signedApkPath, "123456"); } - private boolean isAndroid() { - boolean isAndroid = true; - try { - Class.forName("android.content.Context"); - } - catch (ClassNotFoundException e) { - isAndroid = false; - } - return isAndroid; - } - - // 使用Android build-tools里自带的apksigner工具进行签名 private boolean signApkUsingAndroidApksigner(String apkPath, String keyStorePath, String signedApkPath, String keyStorePassword) { ArrayList commandList = new ArrayList<>(); @@ -139,12 +84,8 @@ public class BuildAndSignApkTask implements Runnable { commandList.add("false"); commandList.add(apkPath); - int size = commandList.size(); - String[] commandArray = new String[size]; - commandArray = commandList.toArray(commandArray); - try { - ApkSignerTool.main(commandArray); + ApkSignerTool.main(commandList.toArray(new String[0])); } catch (Exception e) { e.printStackTrace(); diff --git a/patch/src/main/java/org/lsposed/patch/util/ShellCmdUtil.java b/patch/src/main/java/org/lsposed/patch/util/ShellCmdUtil.java deleted file mode 100644 index 41a15e0..0000000 --- a/patch/src/main/java/org/lsposed/patch/util/ShellCmdUtil.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.lsposed.patch.util; - -import java.io.BufferedReader; -import java.io.Closeable; -import java.io.File; -import java.io.InputStreamReader; - -/** - * Created by Wind - */ -public class ShellCmdUtil { - - /** - * 执行系统命令, 返回执行结果 - * - * @param cmd 需要执行的命令 - * @param dir 执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同 - */ - public static String execCmd(String cmd, File dir) throws Exception { - StringBuilder result = new StringBuilder(); - - Process process = null; - BufferedReader bufrIn = null; - BufferedReader bufrError = null; - - try { - // 执行命令, 返回一个子进程对象(命令在子进程中执行) - process = Runtime.getRuntime().exec(cmd, null, dir); - - // 方法阻塞, 等待命令执行完成(成功会返回0) - process.waitFor(); - - // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入) - bufrIn = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); - bufrError = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8")); - - // 读取输出 - String line = null; - while ((line = bufrIn.readLine()) != null) { - result.append(line).append('\n'); - } - while ((line = bufrError.readLine()) != null) { - result.append(line).append('\n'); - } - - } finally { - close(bufrIn); - close(bufrError); - - // 销毁子进程 - if (process != null) { - process.destroy(); - } - } - - // 返回执行结果 - return result.toString(); - } - - public static void chmodNoException(String path, int mode) { - try { - chmod(path, mode); - } catch (Exception e) { - e.printStackTrace(); - System.err.println("chmod exception path --> " + path + " exception -->" + e.getMessage()); - } - } - - public static void chmod(String path, int mode) throws Exception { - chmodOnAndroid(path, mode); - - File file = new File(path); - String cmd = "chmod "; - if (file.isDirectory()) { - cmd += " -R "; - } - String cmode = String.format("%o", mode); - Runtime.getRuntime().exec(cmd + cmode + " " + path).waitFor(); - } - - private static void chmodOnAndroid(String path, int mode) { - Object sdk_int = ReflectUtils.getField("android.os.Build$VERSION", "SDK_INT"); - if (!(sdk_int instanceof Integer)) { - return; - } - if ((int)sdk_int >= 21) { - System.out.println("chmod on android is called, path = " + path); - ReflectUtils.callMethod("android.system.Os", "chmod", path, mode); - } - } - - private static void close(Closeable stream) { - if (stream != null) { - try { - stream.close(); - } catch (Exception e) { - // nothing - } - } - } - - public interface FileMode { - int MODE_ISUID = 04000; - int MODE_ISGID = 02000; - int MODE_ISVTX = 01000; - int MODE_IRUSR = 00400; - int MODE_IWUSR = 00200; - int MODE_IXUSR = 00100; - int MODE_IRGRP = 00040; - int MODE_IWGRP = 00020; - int MODE_IXGRP = 00010; - int MODE_IROTH = 00004; - int MODE_IWOTH = 00002; - int MODE_IXOTH = 00001; - - int MODE_755 = MODE_IRUSR | MODE_IWUSR | MODE_IXUSR - | MODE_IRGRP | MODE_IXGRP - | MODE_IROTH | MODE_IXOTH; - } -} diff --git a/patch/src/main/java/org/lsposed/patch/util/ZipUtils.java b/patch/src/main/java/org/lsposed/patch/util/ZipUtils.java deleted file mode 100644 index 2a9ae4d..0000000 --- a/patch/src/main/java/org/lsposed/patch/util/ZipUtils.java +++ /dev/null @@ -1,148 +0,0 @@ -package org.lsposed.patch.util; - -import org.apache.commons.io.IOUtils; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; -import java.util.Enumeration; -import java.util.zip.CRC32; -import java.util.zip.CheckedOutputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipOutputStream; - -/** - * Created by Wind - */ -public class ZipUtils { - - static final int BUFFER = 8192; - - /** - * 解压文件 - * - * @param zipPath 要解压的目标文件 - * @param descDir 指定解压目录 - * @return 解压结果:成功,失败 - */ - @SuppressWarnings("rawtypes") - public static void decompressZip(String zipPath, String descDir) throws IOException { - File zipFile = new File(zipPath); - if (!descDir.endsWith(File.separator)) { - descDir = descDir + File.separator; - } - File pathFile = new File(descDir); - if (!pathFile.exists()) { - if (!pathFile.mkdirs()) { - throw new IllegalStateException("mkdir fail " + pathFile.getAbsolutePath()); - } - } - - try (ZipFile zip = new ZipFile(zipFile, Charset.forName("gbk"))) { - for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) { - ZipEntry entry = (ZipEntry) entries.nextElement(); - String zipEntryName = entry.getName(); - - String outPath = (descDir + zipEntryName).replace("/", File.separator); - File file = new File(outPath); - - if (entry.isDirectory()) { - if (!file.exists()) { - if (!file.mkdirs()) { - throw new IllegalStateException("mkdir fail " + file.getAbsolutePath()); - } - } - continue; - } - - try (InputStream in = zip.getInputStream(entry)) { - if (file.getParentFile() != null && !file.getParentFile().exists()) { - if (!file.getParentFile().mkdirs()) { - throw new IllegalStateException("mkdir fail " + file.getAbsolutePath()); - } - if (System.getProperty("os.name", "").toLowerCase().contains("win")) { - Runtime.getRuntime().exec("fsutil file setCaseSensitiveInfo " + file.getParentFile().getAbsolutePath()); - System.out.println("Enable setCaseSensitiveInfo for " + file.getParentFile().getAbsolutePath()); - } - } - OutputStream out = new FileOutputStream(outPath); - IOUtils.copy(in, out); - out.close(); - } - catch (Exception err) { - throw new IllegalStateException("wtf", err); - } - } - } - } - - public static void compressToZip(String srcPath, String dstPath) { - File srcFile = new File(srcPath); - File dstFile = new File(dstPath); - if (!srcFile.exists()) { - throw new IllegalStateException("wtf", new Throwable("DUMPBT")); - } - - try (FileOutputStream out = new FileOutputStream(dstFile); - CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32()); - ZipOutputStream zipOut = new ZipOutputStream(cos) - ) { - String baseDir = ""; - compress(srcFile, zipOut, baseDir, true); - } - catch (IOException e) { - throw new IllegalStateException("wtf", e); - } - } - - private static void compress(File file, ZipOutputStream zipOut, String baseDir, boolean isRootDir) throws IOException { - if (file.isDirectory()) { - compressDirectory(file, zipOut, baseDir, isRootDir); - } - else { - compressFile(file, zipOut, baseDir); - } - } - - /** - * 压缩一个目录 - */ - private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir, boolean isRootDir) throws IOException { - File[] files = dir.listFiles(); - if (files == null) { - return; - } - for (int i = 0; i < files.length; i++) { - String compressBaseDir = ""; - if (!isRootDir) { - compressBaseDir = baseDir + dir.getName() + "/"; - } - compress(files[i], zipOut, compressBaseDir, false); - } - } - - /** - * 压缩一个文件 - */ - private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException { - if (!file.exists()) { - return; - } - - try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { - ZipEntry entry = new ZipEntry(baseDir + file.getName()); - zipOut.putNextEntry(entry); - int count; - byte[] data = new byte[BUFFER]; - while ((count = bis.read(data, 0, BUFFER)) != -1) { - zipOut.write(data, 0, count); - } - } - } -}