diff --git a/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java b/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java index 9c6b937..2621519 100644 --- a/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java +++ b/app/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java @@ -86,14 +86,12 @@ public class LSPApplication { XLog.d(TAG, "original application class " + originalApplicationName); XLog.d(TAG, "original signature info " + originalSignature); - if (isApplicationProxied()) { - try { - doHook(); - initAndLoadModules(context); - } - catch (Exception e) { - e.printStackTrace(); - } + try { + doHook(); + initAndLoadModules(context); + } + catch (Exception e) { + e.printStackTrace(); } } } @@ -214,10 +212,12 @@ public class LSPApplication { } private static void doHook() throws IllegalAccessException, ClassNotFoundException, IOException { - hookContextImplSetOuterContext(); - hookInstallContentProviders(); - hookActivityAttach(); - hookServiceAttach(); + if (isApplicationProxied()) { + hookContextImplSetOuterContext(); + hookInstallContentProviders(); + hookActivityAttach(); + hookServiceAttach(); + } if (fetchSigbypassLv() >= Constants.SIGBYPASS_LV_PM) { byPassSignature(); } @@ -318,18 +318,11 @@ public class LSPApplication { } protected void attachBaseContext(Context base) { - - // 将applicationInfo中保存的applcation class name还原为真实的application class name if (isApplicationProxied()) { modifyApplicationInfoClassName(); - } - - if (isApplicationProxied()) { attachOrignalBaseContext(base); setLoadedApkField(base); } - - // setApplicationLoadedApk(base); } private void attachOrignalBaseContext(Context base) { diff --git a/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPApplicationStub.java b/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPApplicationStub.java index 4a40432..46c55eb 100644 --- a/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPApplicationStub.java +++ b/appstub/src/main/java/org/lsposed/lspatch/appstub/LSPApplicationStub.java @@ -23,7 +23,7 @@ public class LSPApplicationStub extends Application { // load real lsp loader from asset Context context = createAppContext(); if (context == null) { - Log.e(TAG, "create context err"); + throw new IllegalStateException("create context err"); } else { try (InputStream inputStream = context.getAssets().open("lsploader.dex"); @@ -57,13 +57,11 @@ public class LSPApplicationStub extends Application { public void onCreate() { super.onCreate(); - if (realLSPApplication != null) { - try { - realLSPApplication.getClass().getDeclaredMethod("onCreate").invoke(realLSPApplication); - } - catch (Exception e) { - throw new IllegalStateException("wtf", e); - } + try { + realLSPApplication.getClass().getDeclaredMethod("onCreate").invoke(realLSPApplication); + } + catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new IllegalStateException("wtf", e); } } @@ -71,15 +69,13 @@ public class LSPApplicationStub extends Application { protected void attachBaseContext(Context base) { super.attachBaseContext(base); - if (realLSPApplication != null) { - try { - Method method = realLSPApplication.getClass().getDeclaredMethod("attachBaseContext", Context.class); - method.setAccessible(true); - method.invoke(realLSPApplication, base); - } - catch (Exception e) { - throw new IllegalStateException("wtf", e); - } + try { + Method method = realLSPApplication.getClass().getDeclaredMethod("attachBaseContext", Context.class); + method.setAccessible(true); + method.invoke(realLSPApplication, base); + } + catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new IllegalStateException("wtf", e); } } @@ -117,7 +113,7 @@ public class LSPApplicationStub extends Application { } } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) { - e.printStackTrace(); + throw new IllegalStateException("wtf", e); } return null; } diff --git a/patch/src/main/java/org/lsposed/patch/LSPatch.java b/patch/src/main/java/org/lsposed/patch/LSPatch.java index 187f2ef..a485b14 100644 --- a/patch/src/main/java/org/lsposed/patch/LSPatch.java +++ b/patch/src/main/java/org/lsposed/patch/LSPatch.java @@ -8,13 +8,14 @@ import com.wind.meditor.property.AttributeItem; import com.wind.meditor.property.ModificationProperty; import com.wind.meditor.utils.NodeValue; +import org.apache.commons.io.FileUtils; import org.lsposed.lspatch.share.Constants; import org.lsposed.patch.base.BaseCommand; import org.lsposed.patch.task.BuildAndSignApkTask; import org.lsposed.patch.task.SaveApkSignatureTask; import org.lsposed.patch.task.SaveOriginalApplicationNameTask; import org.lsposed.patch.task.SoAndDexCopyTask; -import org.lsposed.patch.util.FileUtils; +import org.lsposed.patch.util.ZipUtils; import org.lsposed.patch.util.ManifestParser; import java.io.File; @@ -123,7 +124,7 @@ public class LSPatch extends BaseCommand { new SaveApkSignatureTask(apkPath, unzipApkFilePath).run(); long currentTime = System.currentTimeMillis(); - FileUtils.decompressZip(apkPath, unzipApkFilePath); + ZipUtils.decompressZip(apkPath, unzipApkFilePath); System.out.println("decompress apk cost time: " + (System.currentTimeMillis() - currentTime) + "ms"); @@ -143,8 +144,7 @@ public class LSPatch extends BaseCommand { applicationName = pair.applicationName; } - System.out.println("Get application name cost time: " + (System.currentTimeMillis() - currentTime) + "ms"); - System.out.println("Get the application name: " + applicationName); + System.out.println("original application name: " + applicationName); // modify manifest File manifestFile = new File(manifestFilePath); @@ -152,7 +152,7 @@ public class LSPatch extends BaseCommand { File manifestFileNew = new File(manifestFilePathNew); fuckIfFail(manifestFile.renameTo(manifestFileNew)); - modifyManifestFile(manifestFilePathNew, manifestFilePath, applicationName); + modifyManifestFile(manifestFilePathNew, manifestFilePath); // new manifest may not exist if (manifestFile.exists() && manifestFile.length() > 0) { @@ -162,12 +162,10 @@ public class LSPatch extends BaseCommand { fuckIfFail(manifestFileNew.renameTo(manifestFile)); } - // save original main application name to asset file - if (isNotEmpty(applicationName)) { - new SaveOriginalApplicationNameTask(applicationName, unzipApkFilePath).run(); - } + // save original main application name to asset file even its empty + new SaveOriginalApplicationNameTask(applicationName, unzipApkFilePath).run(); - // copy xposed so and dex files into the unzipped apk + // copy so and dex files into the unzipped apk new SoAndDexCopyTask(dexFileCount, unzipApkFilePath).run(); // copy origin apk to assets @@ -178,14 +176,14 @@ public class LSPatch extends BaseCommand { File[] listAssets = new File("list-assets").listFiles(); if (listAssets == null || listAssets.length == 0) { - System.out.println("warning: No assets file copyied"); + System.out.println("Warning: No assets file copyied"); } else { copyDirectory(new File("list-assets"), new File(unzipApkFilePath, "assets")); } // save lspatch config to asset.. - org.apache.commons.io.FileUtils.write(new File(unzipApkFilePath, "assets" + File.separator + Constants.CONFIG_NAME_SIGBYPASSLV + sigbypassLevel), "lspatch", + FileUtils.write(new File(unzipApkFilePath, "assets" + File.separator + Constants.CONFIG_NAME_SIGBYPASSLV + sigbypassLevel), "lspatch", Charset.defaultCharset()); // compress all files into an apk and then sign it. @@ -194,18 +192,14 @@ public class LSPatch extends BaseCommand { System.out.println("Output APK: " + outputPath); } - private void modifyManifestFile(String filePath, String dstFilePath, String originalApplicationName) { + private void modifyManifestFile(String filePath, String dstFilePath) { ModificationProperty property = new ModificationProperty(); - boolean modifyEnabled = false; if (debuggableFlag >= 0) { - modifyEnabled = true; property.addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, debuggableFlag != 0)); } property.addApplicationAttribute(new AttributeItem("extractNativeLibs", true)); - - modifyEnabled = true; property.addApplicationAttribute(new AttributeItem(NodeValue.Application.NAME, proxyName)); FileProcesser.processManifestFile(filePath, dstFilePath, property); @@ -236,15 +230,4 @@ public class LSPatch extends BaseCommand { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); return df.format(new Date()); } - - private String[] getXposedModules(String modules) { - if (modules == null || modules.isEmpty()) { - return null; - } - return modules.split(File.pathSeparator); - } - - private static boolean isNotEmpty(String str) { - return str != null && !str.isEmpty(); - } } \ No newline at end of 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 7aecabf..a824292 100644 --- a/patch/src/main/java/org/lsposed/patch/task/BuildAndSignApkTask.java +++ b/patch/src/main/java/org/lsposed/patch/task/BuildAndSignApkTask.java @@ -2,11 +2,14 @@ package org.lsposed.patch.task; import com.android.apksigner.ApkSignerTool; +import org.apache.commons.io.IOUtils; import org.lsposed.patch.LSPatch; -import org.lsposed.patch.util.FileUtils; +import org.lsposed.patch.util.ZipUtils; import org.lsposed.patch.util.ShellCmdUtil; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; import java.util.ArrayList; /** @@ -33,7 +36,7 @@ public class BuildAndSignApkTask implements Runnable { File unzipApkFile = new File(unzipApkFilePath); String unsignedApkPath = unzipApkFile.getParent() + File.separator + "unsigned.apk"; - FileUtils.compressToZip(unzipApkFilePath, unsignedApkPath); + ZipUtils.compressToZip(unzipApkFilePath, unsignedApkPath); String keyStoreFilePath = unzipApkFile.getParent() + File.separator + "keystore"; @@ -49,7 +52,10 @@ public class BuildAndSignApkTask implements Runnable { keyStoreAssetPath = "assets/keystore"; } - FileUtils.copyFileFromJar(keyStoreAssetPath, keyStoreFilePath); + try (InputStream inputStream = getClass().getResourceAsStream(keyStoreAssetPath); + FileOutputStream out = new FileOutputStream(keyStoreFilePath)) { + IOUtils.copy(inputStream, out); + } boolean signResult = signApk(unsignedApkPath, keyStoreFilePath, signedApkPath); diff --git a/patch/src/main/java/org/lsposed/patch/task/SaveOriginalApplicationNameTask.java b/patch/src/main/java/org/lsposed/patch/task/SaveOriginalApplicationNameTask.java index ed5cab9..0b72f06 100644 --- a/patch/src/main/java/org/lsposed/patch/task/SaveOriginalApplicationNameTask.java +++ b/patch/src/main/java/org/lsposed/patch/task/SaveOriginalApplicationNameTask.java @@ -3,6 +3,7 @@ package org.lsposed.patch.task; import org.apache.commons.io.FileUtils; import java.io.File; +import java.nio.charset.Charset; /** * Created by xiawanli on 2019/4/6 @@ -25,8 +26,7 @@ public class SaveOriginalApplicationNameTask implements Runnable { @Override public void run() { try { - ensureDstFileCreated(); - FileUtils.write(new File(dstFilePath), applcationName); + FileUtils.write(new File(dstFilePath), applcationName, Charset.defaultCharset()); } catch (Exception err) { // just crash @@ -34,18 +34,4 @@ public class SaveOriginalApplicationNameTask implements Runnable { throw new IllegalStateException("wtf", err); } } - - private void ensureDstFileCreated() { - File dstParentFile = new File(dstFilePath); - if (!dstParentFile.getParentFile().getParentFile().exists()) { - if (!dstParentFile.getParentFile().getParentFile().mkdirs()) { - throw new IllegalStateException("mkdir fail"); - } - } - if (!dstParentFile.getParentFile().exists()) { - if (!dstParentFile.getParentFile().mkdirs()) { - throw new IllegalStateException("mkdir fail"); - } - } - } } \ No newline at end of file diff --git a/patch/src/main/java/org/lsposed/patch/util/FileUtils.java b/patch/src/main/java/org/lsposed/patch/util/ZipUtils.java similarity index 89% rename from patch/src/main/java/org/lsposed/patch/util/FileUtils.java rename to patch/src/main/java/org/lsposed/patch/util/ZipUtils.java index b1618d1..2a9ae4d 100644 --- a/patch/src/main/java/org/lsposed/patch/util/FileUtils.java +++ b/patch/src/main/java/org/lsposed/patch/util/ZipUtils.java @@ -20,7 +20,7 @@ import java.util.zip.ZipOutputStream; /** * Created by Wind */ -public class FileUtils { +public class ZipUtils { static final int BUFFER = 8192; @@ -82,18 +82,6 @@ public class FileUtils { } } - private static InputStream getInputStreamFromFile(String filePath) { - return FileUtils.class.getClassLoader().getResourceAsStream(filePath); - } - - // copy an asset file into a path - public static void copyFileFromJar(String inJarPath, String distPath) throws IOException { - // System.out.println("start copyFile inJarPath =" + inJarPath + " distPath = " + distPath); - try (InputStream inputStream = getInputStreamFromFile(inJarPath); FileOutputStream out = new FileOutputStream(distPath)) { - IOUtils.copy(inputStream, out); - } - } - public static void compressToZip(String srcPath, String dstPath) { File srcFile = new File(srcPath); File dstFile = new File(dstPath);