Fix cached origin_apk.bin not changed after app update

This commit is contained in:
Nullptr 2021-08-26 22:27:43 +08:00
parent fd2b6c9e58
commit 5f8159ec65
2 changed files with 33 additions and 4 deletions

View File

@ -13,10 +13,12 @@ import android.util.Log;
import org.lsposed.lspatch.util.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipFile;
import dalvik.system.PathClassLoader;
@ -41,10 +43,25 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
final String originalAppComponentFactoryClass = FileUtils.readTextFromInputStream(baseClassLoader.getResourceAsStream(ORIGINAL_APP_COMPONENT_FACTORY_ASSET_PATH));
try {
try (InputStream inputStream = baseClassLoader.getResourceAsStream(ORIGINAL_APK_ASSET_PATH)) {
Files.copy(inputStream, Paths.get(cacheApkPath));
} catch (FileAlreadyExistsException ignored) {
File cacheApk = new File(cacheApkPath);
if (cacheApk.exists()) {
try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir);
InputStream is = new FileInputStream(cacheApk)) {
var sourceCrc = sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
var cacheCrc = FileUtils.calculateCrc(is);
if (sourceCrc != cacheCrc) {
Log.i(TAG, "Application updated, extract original apk again");
cacheApk.delete();
}
}
}
if (!cacheApk.exists()) {
try (InputStream is = baseClassLoader.getResourceAsStream(ORIGINAL_APK_ASSET_PATH)) {
Files.copy(is, Paths.get(cacheApkPath));
}
}
appClassLoader = new PathClassLoader(cacheApkPath, aInfo.nativeLibraryDir, baseClassLoader.getParent());
try {

View File

@ -1,9 +1,11 @@
package org.lsposed.lspatch.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
public class FileUtils {
@ -19,4 +21,14 @@ public class FileUtils {
}
return null;
}
public static long calculateCrc(InputStream is) throws IOException {
CRC32 crcMaker = new CRC32();
byte[] buffer = new byte[65536];
int bytesRead;
while((bytesRead = is.read(buffer)) != -1) {
crcMaker.update(buffer, 0, bytesRead);
}
return crcMaker.getValue();
}
}