Skip to content

Commit

Permalink
Merge branch 'dev_1_9_14_25_2' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tys282000 committed Jun 12, 2023
2 parents 260d383 + 6ed897d commit fc4ec5a
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public DefaultPatchListener(Context context) {

@Override
public int onPatchReceived(String path) {
return checkPackageAndRunPatchService(path, false);
}

protected int checkPackageAndRunPatchService(String path, boolean useEmergencyMode) {
final int returnCode = patchCheck(path, null);
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
return returnCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
*/
public abstract class AbstractPatch {

public abstract boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult);
public abstract boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UpgradePatch extends AbstractPatch {
private static final String TAG = "Tinker.UpgradePatch";

@Override
public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
public boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult) {
ShareTinkerLog.e(TAG, "[-] Ignore this invocation since I'm no-op version.");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,34 @@ public DefaultPatchListener(Context context) {
*/
@Override
public int onPatchReceived(String path) {
return checkPackageAndRunPatchService(path, false);
}

/**
* Check patch package then start patch service to generate patched artifacts.
* @param path
* Path to your patch package.
* @param useEmergencyMode
* true for using emergency mode, otherwise false.
*
* By using emergency mode, dex2oat triggering procedure will be done asynchronously on Android Q and newer
* system to save costs. If your app lives too short to wait for generating patch artifacts, this mode should
* help. **However, the performance of your patched app will become terribly worse since odex of patched dex(es)
* may not be generated before loading patched artifacts in this mode.**
*/
protected int checkPackageAndRunPatchService(String path, boolean useEmergencyMode) {
final File patchFile = new File(path);
final String patchMD5 = SharePatchFileUtil.getMD5(patchFile);
final int returnCode = patchCheck(path, patchMD5);
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
runForgService();
TinkerPatchService.runPatchService(context, path);
TinkerPatchService.runPatchService(context, path, useEmergencyMode);
} else {
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
}
return returnCode;
}


private void runForgService() {
try {
connection = new ServiceConnection() {
Expand Down Expand Up @@ -156,5 +171,4 @@ protected int patchCheck(String path, String patchMd5) {

return ShareConstants.ERROR_PATCH_OK;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
*/
public abstract class AbstractPatch {

public abstract boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult);
public abstract boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public class DexDiffPatchInternal extends BasePatchInternal {


protected static boolean tryRecoverDexFiles(Tinker manager, ShareSecurityCheck checker, Context context,
String patchVersionDirectory, File patchFile, PatchResult patchResult) {
String patchVersionDirectory, File patchFile, boolean useEmergencyMode,
PatchResult patchResult) {
if (!manager.isEnabledForDex()) {
ShareTinkerLog.w(TAG, "patch recover, dex is not enabled");
return true;
Expand All @@ -84,7 +85,8 @@ protected static boolean tryRecoverDexFiles(Tinker manager, ShareSecurityCheck c
}

long begin = SystemClock.elapsedRealtime();
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile, patchResult);
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile,
useEmergencyMode, patchResult);
long cost = SystemClock.elapsedRealtime() - begin;
patchResult.dexCostTime = cost;
ShareTinkerLog.i(TAG, "recover dex result:%b, cost:%d", result, cost);
Expand Down Expand Up @@ -166,7 +168,9 @@ protected static boolean waitAndCheckDexOptFile(File patchFile, Tinker manager)
return true;
}

private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta, final File patchFile, PatchResult patchResult) {
private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta,
final File patchFile, boolean useEmergencyMode,
PatchResult patchResult) {
String dir = patchVersionDirectory + "/" + DEX_PATH + "/";

if (!extractDexDiffInternals(context, dir, meta, patchFile, TYPE_DEX)) {
Expand Down Expand Up @@ -194,7 +198,7 @@ private static boolean patchDexExtractViaDexDiff(Context context, String patchVe
ShareTinkerLog.i(TAG, "legal files to do dexopt: " + legalFiles);

final String optimizeDexDirectory = patchVersionDirectory + "/" + DEX_OPTIMIZE_PATH + "/";
return dexOptimizeDexFiles(context, legalFiles, optimizeDexDirectory, patchFile, patchResult);
return dexOptimizeDexFiles(context, legalFiles, optimizeDexDirectory, patchFile, useEmergencyMode, patchResult);

}

Expand Down Expand Up @@ -347,7 +351,9 @@ private static boolean mergeClassNDexFiles(final Context context, final File pat
return result;
}

private static boolean dexOptimizeDexFiles(Context context, List<File> dexFiles, String optimizeDexDirectory, final File patchFile, final PatchResult patchResult) {
private static boolean dexOptimizeDexFiles(Context context, List<File> dexFiles, String optimizeDexDirectory,
final File patchFile, boolean useEmergencyMode,
final PatchResult patchResult) {
final Tinker manager = Tinker.with(context);

optFiles.clear();
Expand Down Expand Up @@ -381,7 +387,7 @@ private static boolean dexOptimizeDexFiles(Context context, List<File> dexFiles,
// try parallel dex optimizer
TinkerDexOptimizer.optimizeAll(
context, dexFiles, optimizeDexDirectoryFile,
useDLC,
useDLC, useEmergencyMode,
new TinkerDexOptimizer.ResultCallback() {
long startTime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class UpgradePatch extends AbstractPatch {
private static final String TAG = "Tinker.UpgradePatch";

@Override
public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
public boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult) {
Tinker manager = Tinker.with(context);

final File patchFile = new File(tempPatchPath);
Expand Down Expand Up @@ -169,7 +169,7 @@ public boolean tryPatch(Context context, String tempPatchPath, PatchResult patch
}

//we use destPatchFile instead of patchFile, because patchFile may be deleted during the patch process
if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, patchResult)) {
if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, useEmergencyMode, patchResult)) {
ShareTinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch dex failed");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class PatchResult implements Serializable {

public String rawPatchFilePath;

public boolean useEmergencyMode;

public long totalCostTime;

public long dexCostTime;
Expand All @@ -55,6 +57,7 @@ public String toString() {
sb.append("\nPatchResult: \n");
sb.append("isSuccess:" + isSuccess + "\n");
sb.append("rawPatchFilePath:" + rawPatchFilePath + "\n");
sb.append("useEmergencyMode:" + useEmergencyMode + "\n");
sb.append("costTime:" + totalCostTime + "\n");
sb.append("dexoptTriggerTime:" + dexoptTriggerTime + "\n");
sb.append("isOatGenerated:" + isOatGenerated + "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class TinkerPatchService extends IntentService {
private static final String TAG = "Tinker.TinkerPatchService";

private static final String PATCH_PATH_EXTRA = "patch_path_extra";
private static final String PATCH_USE_EMERGENCY_MODE = "patch_use_emergency_mode";
private static final String RESULT_CLASS_EXTRA = "patch_result_class";

private static AbstractPatch upgradePatchProcessor = null;
Expand All @@ -59,9 +60,14 @@ public TinkerPatchService() {
}

public static void runPatchService(final Context context, final String path) {
runPatchService(context, path, false);
}

public static void runPatchService(final Context context, final String path, boolean useEmergencyMode) {
ShareTinkerLog.i(TAG, "run patch service...");
Intent intent = new Intent(context, TinkerPatchService.class);
intent.putExtra(PATCH_PATH_EXTRA, path);
intent.putExtra(PATCH_USE_EMERGENCY_MODE, useEmergencyMode);
intent.putExtra(RESULT_CLASS_EXTRA, resultServiceClass.getName());
try {
context.startService(intent);
Expand All @@ -88,6 +94,13 @@ public static String getPatchPathExtra(Intent intent) {
return ShareIntentUtil.getStringExtra(intent, PATCH_PATH_EXTRA);
}

public static boolean getPatchUseEmergencyMode(Intent intent) {
if (intent == null) {
throw new TinkerRuntimeException("getPatchUseEmergencyMode, but intent is null");
}
return ShareIntentUtil.getBooleanExtra(intent, PATCH_USE_EMERGENCY_MODE, false);
}

public static String getPatchResultExtra(Intent intent) {
if (intent == null) {
throw new TinkerRuntimeException("getPatchResultExtra, but intent is null");
Expand Down Expand Up @@ -210,6 +223,8 @@ private static void doApplyPatch(Context context, Intent intent) {
}
File patchFile = new File(path);

final boolean useEmergencyMode = getPatchUseEmergencyMode(intent);

long begin = SystemClock.elapsedRealtime();
boolean result;
long cost;
Expand All @@ -220,7 +235,7 @@ private static void doApplyPatch(Context context, Intent intent) {
if (upgradePatchProcessor == null) {
throw new TinkerRuntimeException("upgradePatchProcessor is null.");
}
result = upgradePatchProcessor.tryPatch(context, path, patchResult);
result = upgradePatchProcessor.tryPatch(context, path, useEmergencyMode, patchResult);
} catch (Throwable throwable) {
e = throwable;
result = false;
Expand All @@ -233,6 +248,7 @@ private static void doApplyPatch(Context context, Intent intent) {

patchResult.isSuccess = result;
patchResult.rawPatchFilePath = path;
patchResult.useEmergencyMode = useEmergencyMode;
patchResult.totalCostTime = cost;
patchResult.type = tinker.getCustomPatcher() == null ? PatchResult.PATCH_TYPE_BSDIFF : PatchResult.PATCH_TYPE_CUSTOM;
patchResult.e = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,19 @@ private static void doInject(Application app, ClassLoader classLoader) throws Th
final Object basePackageInfo = findField(baseContext.getClass(), "mPackageInfo").get(baseContext);
findField(basePackageInfo.getClass(), "mClassLoader").set(basePackageInfo, classLoader);

if (Build.VERSION.SDK_INT < 27) {
final Resources res = app.getResources();
try {
findField(res.getClass(), "mClassLoader").set(res, classLoader);

final Object drawableInflater = findField(res.getClass(), "mDrawableInflater").get(res);
if (drawableInflater != null) {
findField(drawableInflater.getClass(), "mClassLoader").set(drawableInflater, classLoader);
}
} catch (Throwable ignored) {
// Ignored.
final Resources res = app.getResources();
try {
findField(res.getClass(), "mClassLoader").set(res, classLoader);
} catch (Throwable ignored) {
// Ignored.
}
try {
final Object drawableInflater = findField(res.getClass(), "mDrawableInflater").get(res);
if (drawableInflater != null) {
findField(drawableInflater.getClass(), "mClassLoader").set(drawableInflater, classLoader);
}
} catch (Throwable ignored) {
// Ignored.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static boolean loadTinkerJars(final TinkerApplication application, String

TinkerDexOptimizer.optimizeAll(
application, legalFiles, optimizeDir, true,
application.isUseDelegateLastClassLoader(), targetISA,
application.isUseDelegateLastClassLoader(), targetISA, false,
new TinkerDexOptimizer.ResultCallback() {
long start;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public final class TinkerDexOptimizer {
* @return If all dexes are optimized successfully, return true. Otherwise return false.
*/
public static boolean optimizeAll(Context context, Collection<File> dexFiles, File optimizedDir,
boolean useDLC, ResultCallback cb) {
boolean useDLC, boolean useEmergencyMode, ResultCallback cb) {
final String targetISA = ShareTinkerInternals.getCurrentInstructionSet();
return optimizeAll(context, dexFiles, optimizedDir, false, useDLC, targetISA, cb);
return optimizeAll(context, dexFiles, optimizedDir, false, useDLC, targetISA, useEmergencyMode, cb);
}

public static boolean optimizeAll(Context context, Collection<File> dexFiles, File optimizedDir,
boolean useInterpretMode, boolean useDLC,
String targetISA, ResultCallback cb) {
String targetISA, boolean useEmergencyMode, ResultCallback cb) {
ArrayList<File> sortList = new ArrayList<>(dexFiles);
// sort input dexFiles with its file length in reverse order.
Collections.sort(sortList, new Comparator<File>() {
Expand All @@ -105,7 +105,7 @@ public int compare(File lhs, File rhs) {
});
for (File dexFile : sortList) {
OptimizeWorker worker = new OptimizeWorker(context, dexFile, optimizedDir, useInterpretMode,
useDLC, targetISA, cb);
useDLC, targetISA, useEmergencyMode, cb);
if (!worker.run()) {
return false;
}
Expand All @@ -129,17 +129,19 @@ private static class OptimizeWorker {
private final File optimizedDir;
private final boolean useInterpretMode;
private final boolean useDLC;
private final boolean useEmergencyMode;
private final ResultCallback callback;

OptimizeWorker(Context context, File dexFile, File optimizedDir, boolean useInterpretMode,
boolean useDLC, String targetISA, ResultCallback cb) {
boolean useDLC, String targetISA, boolean useEmergencyMode, ResultCallback cb) {
this.context = context;
this.dexFile = dexFile;
this.optimizedDir = optimizedDir;
this.useInterpretMode = useInterpretMode;
this.useDLC = useDLC;
this.callback = cb;
this.targetISA = targetISA;
this.useEmergencyMode = useEmergencyMode;
}

boolean run() {
Expand Down Expand Up @@ -177,15 +179,27 @@ boolean run() {
createFakeODexPathStructureOnDemand(optimizedPath);
patchClassLoaderStrongRef = NewClassLoaderInjector.triggerDex2Oat(context, optimizedDir,
useDLC, dexFile.getAbsolutePath());
try {
triggerPMDexOptOnDemand(context, dexFile.getAbsolutePath(), optimizedPath);
} catch (Throwable thr) {
ShareTinkerLog.printErrStackTrace(TAG, thr,
"Fail to call triggerPMDexOptAsyncOnDemand.");
} finally {
final String vdexPath = optimizedPath.substring(0,
optimizedPath.lastIndexOf(ODEX_SUFFIX)) + VDEX_SUFFIX;
waitUntilFileGeneratedOrTimeout(context, vdexPath);
final Runnable task = new Runnable() {
@Override
public void run() {
try {
triggerPMDexOptOnDemand(context, dexFile.getAbsolutePath(), optimizedPath);
} catch (Throwable thr) {
ShareTinkerLog.printErrStackTrace(TAG, thr,
"Fail to call triggerPMDexOptAsyncOnDemand.");
} finally {
if (!useEmergencyMode) {
final String vdexPath = optimizedPath.substring(0,
optimizedPath.lastIndexOf(ODEX_SUFFIX)) + VDEX_SUFFIX;
waitUntilFileGeneratedOrTimeout(context, vdexPath);
}
}
}
};
if (useEmergencyMode) {
new Thread(task, "TinkerDex2oatTrigger").start();
} else {
task.run();
}
} else {
patchClassLoaderStrongRef = NewClassLoaderInjector.triggerDex2Oat(context, optimizedDir,
Expand Down
Loading

0 comments on commit fc4ec5a

Please sign in to comment.