Skip to content

Commit

Permalink
1. [enhancement] osdetector compatible 1.2 and 1.4
Browse files Browse the repository at this point in the history
2. [bugfixx] fix multi optimize dex bug
  • Loading branch information
shwenzhang committed Dec 18, 2016
1 parent 9919eba commit 3f79366
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import dalvik.system.DexFile;

/**
* Created by zhangshaowen on 16/4/12.
*/
Expand Down Expand Up @@ -95,30 +97,54 @@ private static boolean patchDexExtractViaDexDiff(Context context, String patchVe
boolean isSuccess = TinkerParallelDexOptimizer.optimizeAll(
files, optimizeDexDirectoryFile,
new TinkerParallelDexOptimizer.ResultCallback() {
long start;
long startTime;
@Override
public void onStart(File dexFile, File optimizedDir) {
start = System.currentTimeMillis();
startTime = System.currentTimeMillis();
TinkerLog.i(TAG, "start to optimize dex %s", dexFile.getPath());
}

@Override
public void onSuccess(File dexFile, File optimizedDir) {
// Do nothing.
TinkerLog.i(TAG, "success to optimize dex %s use time %d",
dexFile.getPath(), (System.currentTimeMillis() - start));
dexFile.getPath(), (System.currentTimeMillis() - startTime));
}

@Override
public void onFailed(File dexFile, File optimizedDir, Throwable thr) {
TinkerLog.i(TAG, "fail to optimize dex %s use time %d",
dexFile.getPath(), (System.currentTimeMillis() - start));

dexFile.getPath(), (System.currentTimeMillis() - startTime));
SharePatchFileUtil.safeDeleteFile(dexFile);
manager.getPatchReporter().onPatchDexOptFail(patchFile, dexFile, optimizeDexDirectory, dexFile.getName(), thr);
}
}
);
//list again
if (isSuccess) {
for (File file : files) {
try {
if (!SharePatchFileUtil.isLegalFile(file)) {
TinkerLog.e(TAG, "single dex optimizer file %s is not exist, just return false", file);
return false;
}
String outputPathName = SharePatchFileUtil.optimizedPathFor(file, optimizeDexDirectoryFile);
File outputFile = new File(outputPathName);
if (!SharePatchFileUtil.isLegalFile(outputFile)) {
TinkerLog.e(TAG, "parallel dex optimizer file %s fail, optimize again", outputPathName);
long start = System.currentTimeMillis();
DexFile.loadDex(file.getAbsolutePath(), outputPathName, 0);
TinkerLog.i(TAG, "success single dex optimize file, path: %s, use time: %d", file.getPath(), (System.currentTimeMillis() - start));
}
} catch (Throwable e) {
TinkerLog.e(TAG, "dex optimize or load failed, path:" + file.getPath());
//delete file
SharePatchFileUtil.safeDeleteFile(file);
manager.getPatchReporter().onPatchDexOptFail(patchFile, file, optimizeDexDirectory, file.getName(), e);
return false;
}
}
}

return isSuccess;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ public TinkerPatchService() {
}

public static void runPatchService(Context context, String path) {
Intent intent = new Intent(context, TinkerPatchService.class);
intent.putExtra(PATCH_PATH_EXTRA, path);
intent.putExtra(RESULT_CLASS_EXTRA, resultServiceClass.getName());
context.startService(intent);
try {
Intent intent = new Intent(context, TinkerPatchService.class);
intent.putExtra(PATCH_PATH_EXTRA, path);
intent.putExtra(RESULT_CLASS_EXTRA, resultServiceClass.getName());
context.startService(intent);
} catch (Throwable throwable) {
TinkerLog.e(TAG, "start patch service fail, exception:" + throwable);
}
}

public static void setPatchProcessor(AbstractPatch upgradePatch, Class<? extends AbstractResultService> serviceClass) {
Expand Down
2 changes: 1 addition & 1 deletion tinker-build/tinker-patch-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
compile localGroovy()
// compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':tinker-build:tinker-patch-lib')
compile 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
compile 'com.google.gradle:osdetector-gradle-plugin:1.2.1'
compile 'com.android.tools.build:gradle:2.1.0'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ class TinkerPatchPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.apply plugin: 'com.google.osdetector'
//osdetector change its plugin name in 1.4.0
try {
project.apply plugin: 'osdetector'
} catch (Throwable e) {
project.apply plugin: 'com.google.osdetector'
}

project.extensions.create('tinkerPatch', TinkerPatchExtension)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package tinker.sample.android.reporter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.MessageQueue;

import com.tencent.tinker.lib.reporter.DefaultLoadReporter;
import com.tencent.tinker.lib.util.TinkerLog;
import com.tencent.tinker.loader.shareutil.ShareConstants;
import com.tencent.tinker.loader.shareutil.SharePatchFileUtil;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;

import java.io.File;

Expand All @@ -33,7 +35,7 @@
* Created by zhangshaowen on 16/4/13.
*/
public class SampleLoadReporter extends DefaultLoadReporter {
private Handler handler = new Handler();
private final static String TAG = "Tinker.SampleLoadReporter";

public SampleLoadReporter(Context context) {
super(context);
Expand Down Expand Up @@ -63,6 +65,17 @@ public void onLoadResult(File patchDirectory, int loadCode, long cost) {
@Override
public void onLoadException(Throwable e, int errorCode) {
super.onLoadException(e, errorCode);
switch (errorCode) {
case ShareConstants.ERROR_LOAD_EXCEPTION_UNCAUGHT:
String uncaughtString = SharePatchFileUtil.checkTinkerLastUncaughtCrash(context);
if (!ShareTinkerInternals.isNullOrNil(uncaughtString)) {
File laseCrashFile = SharePatchFileUtil.getPatchLastCrashFile(context);
SharePatchFileUtil.safeDeleteFile(laseCrashFile);
// found really crash reason
TinkerLog.e(TAG, "tinker uncaught real exception:" + uncaughtString);
}
break;
}
SampleTinkerReport.onLoadException(e, errorCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private void copyToTempFile(File patchFile) {
try {
SharePatchFileUtil.copyFileUsingStream(patchFile, tempPatchFile);
} catch (IOException e) {
TinkerLog.e(TAG, "fail to copy file: %s to %s", patchFile.getAbsolutePath(), tempPatchFile.getAbsolutePath());
}
}

Expand All @@ -238,7 +239,7 @@ static RetryInfo readRetryProperty(File infoFile) {
md5 = properties.getProperty(RETRY_FILE_MD5_PROPERTY);
times = properties.getProperty(RETRY_COUNT_PROPERTY);
} catch (IOException e) {
e.printStackTrace();
TinkerLog.e(TAG, "fail to readRetryProperty:" + e);
} finally {
SharePatchFileUtil.closeQuietly(inputStream);
}
Expand Down

0 comments on commit 3f79366

Please sign in to comment.