Skip to content

Commit fc4ec5a

Browse files
committed
Merge branch 'dev_1_9_14_25_2' into dev
2 parents 260d383 + 6ed897d commit fc4ec5a

File tree

13 files changed

+148
-48
lines changed

13 files changed

+148
-48
lines changed

tinker-android/tinker-android-lib-no-op/src/main/java/com/tencent/tinker/lib/listener/DefaultPatchListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public DefaultPatchListener(Context context) {
3535

3636
@Override
3737
public int onPatchReceived(String path) {
38+
return checkPackageAndRunPatchService(path, false);
39+
}
40+
41+
protected int checkPackageAndRunPatchService(String path, boolean useEmergencyMode) {
3842
final int returnCode = patchCheck(path, null);
3943
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
4044
return returnCode;

tinker-android/tinker-android-lib-no-op/src/main/java/com/tencent/tinker/lib/patch/AbstractPatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
*/
2626
public abstract class AbstractPatch {
2727

28-
public abstract boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult);
28+
public abstract boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult);
2929
}

tinker-android/tinker-android-lib-no-op/src/main/java/com/tencent/tinker/lib/patch/UpgradePatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class UpgradePatch extends AbstractPatch {
3131
private static final String TAG = "Tinker.UpgradePatch";
3232

3333
@Override
34-
public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
34+
public boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult) {
3535
ShareTinkerLog.e(TAG, "[-] Ignore this invocation since I'm no-op version.");
3636
return false;
3737
}

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/listener/DefaultPatchListener.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,34 @@ public DefaultPatchListener(Context context) {
5858
*/
5959
@Override
6060
public int onPatchReceived(String path) {
61+
return checkPackageAndRunPatchService(path, false);
62+
}
63+
64+
/**
65+
* Check patch package then start patch service to generate patched artifacts.
66+
* @param path
67+
* Path to your patch package.
68+
* @param useEmergencyMode
69+
* true for using emergency mode, otherwise false.
70+
*
71+
* By using emergency mode, dex2oat triggering procedure will be done asynchronously on Android Q and newer
72+
* system to save costs. If your app lives too short to wait for generating patch artifacts, this mode should
73+
* help. **However, the performance of your patched app will become terribly worse since odex of patched dex(es)
74+
* may not be generated before loading patched artifacts in this mode.**
75+
*/
76+
protected int checkPackageAndRunPatchService(String path, boolean useEmergencyMode) {
6177
final File patchFile = new File(path);
6278
final String patchMD5 = SharePatchFileUtil.getMD5(patchFile);
6379
final int returnCode = patchCheck(path, patchMD5);
6480
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
6581
runForgService();
66-
TinkerPatchService.runPatchService(context, path);
82+
TinkerPatchService.runPatchService(context, path, useEmergencyMode);
6783
} else {
6884
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
6985
}
7086
return returnCode;
7187
}
7288

73-
7489
private void runForgService() {
7590
try {
7691
connection = new ServiceConnection() {
@@ -156,5 +171,4 @@ protected int patchCheck(String path, String patchMd5) {
156171

157172
return ShareConstants.ERROR_PATCH_OK;
158173
}
159-
160174
}

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/AbstractPatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
*/
2626
public abstract class AbstractPatch {
2727

28-
public abstract boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult);
28+
public abstract boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult);
2929
}

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/DexDiffPatchInternal.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public class DexDiffPatchInternal extends BasePatchInternal {
7171

7272

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

8687
long begin = SystemClock.elapsedRealtime();
87-
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile, patchResult);
88+
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile,
89+
useEmergencyMode, patchResult);
8890
long cost = SystemClock.elapsedRealtime() - begin;
8991
patchResult.dexCostTime = cost;
9092
ShareTinkerLog.i(TAG, "recover dex result:%b, cost:%d", result, cost);
@@ -166,7 +168,9 @@ protected static boolean waitAndCheckDexOptFile(File patchFile, Tinker manager)
166168
return true;
167169
}
168170

169-
private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta, final File patchFile, PatchResult patchResult) {
171+
private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta,
172+
final File patchFile, boolean useEmergencyMode,
173+
PatchResult patchResult) {
170174
String dir = patchVersionDirectory + "/" + DEX_PATH + "/";
171175

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

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

199203
}
200204

@@ -347,7 +351,9 @@ private static boolean mergeClassNDexFiles(final Context context, final File pat
347351
return result;
348352
}
349353

350-
private static boolean dexOptimizeDexFiles(Context context, List<File> dexFiles, String optimizeDexDirectory, final File patchFile, final PatchResult patchResult) {
354+
private static boolean dexOptimizeDexFiles(Context context, List<File> dexFiles, String optimizeDexDirectory,
355+
final File patchFile, boolean useEmergencyMode,
356+
final PatchResult patchResult) {
351357
final Tinker manager = Tinker.with(context);
352358

353359
optFiles.clear();
@@ -381,7 +387,7 @@ private static boolean dexOptimizeDexFiles(Context context, List<File> dexFiles,
381387
// try parallel dex optimizer
382388
TinkerDexOptimizer.optimizeAll(
383389
context, dexFiles, optimizeDexDirectoryFile,
384-
useDLC,
390+
useDLC, useEmergencyMode,
385391
new TinkerDexOptimizer.ResultCallback() {
386392
long startTime;
387393

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/UpgradePatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class UpgradePatch extends AbstractPatch {
4242
private static final String TAG = "Tinker.UpgradePatch";
4343

4444
@Override
45-
public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
45+
public boolean tryPatch(Context context, String tempPatchPath, boolean useEmergencyMode, PatchResult patchResult) {
4646
Tinker manager = Tinker.with(context);
4747

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

171171
//we use destPatchFile instead of patchFile, because patchFile may be deleted during the patch process
172-
if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, patchResult)) {
172+
if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, useEmergencyMode, patchResult)) {
173173
ShareTinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch dex failed");
174174
return false;
175175
}

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/service/PatchResult.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class PatchResult implements Serializable {
3030

3131
public String rawPatchFilePath;
3232

33+
public boolean useEmergencyMode;
34+
3335
public long totalCostTime;
3436

3537
public long dexCostTime;
@@ -55,6 +57,7 @@ public String toString() {
5557
sb.append("\nPatchResult: \n");
5658
sb.append("isSuccess:" + isSuccess + "\n");
5759
sb.append("rawPatchFilePath:" + rawPatchFilePath + "\n");
60+
sb.append("useEmergencyMode:" + useEmergencyMode + "\n");
5861
sb.append("costTime:" + totalCostTime + "\n");
5962
sb.append("dexoptTriggerTime:" + dexoptTriggerTime + "\n");
6063
sb.append("isOatGenerated:" + isOatGenerated + "\n");

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/service/TinkerPatchService.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class TinkerPatchService extends IntentService {
4747
private static final String TAG = "Tinker.TinkerPatchService";
4848

4949
private static final String PATCH_PATH_EXTRA = "patch_path_extra";
50+
private static final String PATCH_USE_EMERGENCY_MODE = "patch_use_emergency_mode";
5051
private static final String RESULT_CLASS_EXTRA = "patch_result_class";
5152

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

6162
public static void runPatchService(final Context context, final String path) {
63+
runPatchService(context, path, false);
64+
}
65+
66+
public static void runPatchService(final Context context, final String path, boolean useEmergencyMode) {
6267
ShareTinkerLog.i(TAG, "run patch service...");
6368
Intent intent = new Intent(context, TinkerPatchService.class);
6469
intent.putExtra(PATCH_PATH_EXTRA, path);
70+
intent.putExtra(PATCH_USE_EMERGENCY_MODE, useEmergencyMode);
6571
intent.putExtra(RESULT_CLASS_EXTRA, resultServiceClass.getName());
6672
try {
6773
context.startService(intent);
@@ -88,6 +94,13 @@ public static String getPatchPathExtra(Intent intent) {
8894
return ShareIntentUtil.getStringExtra(intent, PATCH_PATH_EXTRA);
8995
}
9096

97+
public static boolean getPatchUseEmergencyMode(Intent intent) {
98+
if (intent == null) {
99+
throw new TinkerRuntimeException("getPatchUseEmergencyMode, but intent is null");
100+
}
101+
return ShareIntentUtil.getBooleanExtra(intent, PATCH_USE_EMERGENCY_MODE, false);
102+
}
103+
91104
public static String getPatchResultExtra(Intent intent) {
92105
if (intent == null) {
93106
throw new TinkerRuntimeException("getPatchResultExtra, but intent is null");
@@ -210,6 +223,8 @@ private static void doApplyPatch(Context context, Intent intent) {
210223
}
211224
File patchFile = new File(path);
212225

226+
final boolean useEmergencyMode = getPatchUseEmergencyMode(intent);
227+
213228
long begin = SystemClock.elapsedRealtime();
214229
boolean result;
215230
long cost;
@@ -220,7 +235,7 @@ private static void doApplyPatch(Context context, Intent intent) {
220235
if (upgradePatchProcessor == null) {
221236
throw new TinkerRuntimeException("upgradePatchProcessor is null.");
222237
}
223-
result = upgradePatchProcessor.tryPatch(context, path, patchResult);
238+
result = upgradePatchProcessor.tryPatch(context, path, useEmergencyMode, patchResult);
224239
} catch (Throwable throwable) {
225240
e = throwable;
226241
result = false;
@@ -233,6 +248,7 @@ private static void doApplyPatch(Context context, Intent intent) {
233248

234249
patchResult.isSuccess = result;
235250
patchResult.rawPatchFilePath = path;
251+
patchResult.useEmergencyMode = useEmergencyMode;
236252
patchResult.totalCostTime = cost;
237253
patchResult.type = tinker.getCustomPatcher() == null ? PatchResult.PATCH_TYPE_BSDIFF : PatchResult.PATCH_TYPE_CUSTOM;
238254
patchResult.e = e;

tinker-android/tinker-android-loader/src/main/java/com/tencent/tinker/loader/NewClassLoaderInjector.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,19 @@ private static void doInject(Application app, ClassLoader classLoader) throws Th
139139
final Object basePackageInfo = findField(baseContext.getClass(), "mPackageInfo").get(baseContext);
140140
findField(basePackageInfo.getClass(), "mClassLoader").set(basePackageInfo, classLoader);
141141

142-
if (Build.VERSION.SDK_INT < 27) {
143-
final Resources res = app.getResources();
144-
try {
145-
findField(res.getClass(), "mClassLoader").set(res, classLoader);
146-
147-
final Object drawableInflater = findField(res.getClass(), "mDrawableInflater").get(res);
148-
if (drawableInflater != null) {
149-
findField(drawableInflater.getClass(), "mClassLoader").set(drawableInflater, classLoader);
150-
}
151-
} catch (Throwable ignored) {
152-
// Ignored.
142+
final Resources res = app.getResources();
143+
try {
144+
findField(res.getClass(), "mClassLoader").set(res, classLoader);
145+
} catch (Throwable ignored) {
146+
// Ignored.
147+
}
148+
try {
149+
final Object drawableInflater = findField(res.getClass(), "mDrawableInflater").get(res);
150+
if (drawableInflater != null) {
151+
findField(drawableInflater.getClass(), "mClassLoader").set(drawableInflater, classLoader);
153152
}
153+
} catch (Throwable ignored) {
154+
// Ignored.
154155
}
155156
}
156157

0 commit comments

Comments
 (0)