use of com.tencent.tinker.lib.tinker.TinkerLoadResult in project tinker by Tencent.
the class TinkerLoadLibrary method loadLibraryFromTinker.
/**
* sample usage for native library
*
* @param context
* @param relativePath such as lib/armeabi
* @param libName for the lib libTest.so, you can pass Test or libTest, or libTest.so
* @return boolean
* @throws UnsatisfiedLinkError
*/
public static boolean loadLibraryFromTinker(Context context, String relativePath, String libName) throws UnsatisfiedLinkError {
final Tinker tinker = Tinker.with(context);
libName = libName.startsWith("lib") ? libName : "lib" + libName;
libName = libName.endsWith(".so") ? libName : libName + ".so";
String relativeLibPath = relativePath + "/" + libName;
//TODO we should add cpu abi, and the real path later
if (tinker.isEnabledForNativeLib() && tinker.isTinkerLoaded()) {
TinkerLoadResult loadResult = tinker.getTinkerLoadResultIfPresent();
if (loadResult.libs != null) {
for (String name : loadResult.libs.keySet()) {
if (name.equals(relativeLibPath)) {
String patchLibraryPath = loadResult.libraryDirectory + "/" + name;
File library = new File(patchLibraryPath);
if (library.exists()) {
//whether we check md5 when load
boolean verifyMd5 = tinker.isTinkerLoadVerify();
if (verifyMd5 && !SharePatchFileUtil.verifyFileMd5(library, loadResult.libs.get(name))) {
tinker.getLoadReporter().onLoadFileMd5Mismatch(library, ShareConstants.TYPE_LIBRARY);
} else {
System.load(patchLibraryPath);
TinkerLog.i(TAG, "loadLibraryFromTinker success:" + patchLibraryPath);
return true;
}
}
}
}
}
}
return false;
}
use of com.tencent.tinker.lib.tinker.TinkerLoadResult in project tinker by Tencent.
the class SamplePatchListener method patchCheck.
/**
* because we use the defaultCheckPatchReceived method
* the error code define by myself should after {@code ShareConstants.ERROR_RECOVER_INSERVICE
*
* @param path
* @param newPatch
* @return
*/
@Override
public int patchCheck(String path) {
File patchFile = new File(path);
TinkerLog.i(TAG, "receive a patch file: %s, file size:%d", path, SharePatchFileUtil.getFileOrDirectorySize(patchFile));
int returnCode = super.patchCheck(path);
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
returnCode = Utils.checkForPatchRecover(NEW_PATCH_RESTRICTION_SPACE_SIZE_MIN, maxMemory);
}
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
String patchMd5 = SharePatchFileUtil.getMD5(patchFile);
SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
//optional, only disable this patch file with md5
int fastCrashCount = sp.getInt(patchMd5, 0);
if (fastCrashCount >= SampleUncaughtExceptionHandler.MAX_CRASH_COUNT) {
returnCode = Utils.ERROR_PATCH_CRASH_LIMIT;
} else {
//for upgrade patch, version must be not the same
//for repair patch, we won't has the tinker load flag
Tinker tinker = Tinker.with(context);
if (tinker.isTinkerLoaded()) {
TinkerLoadResult tinkerLoadResult = tinker.getTinkerLoadResultIfPresent();
if (tinkerLoadResult != null) {
String currentVersion = tinkerLoadResult.currentVersion;
if (patchMd5.equals(currentVersion)) {
returnCode = Utils.ERROR_PATCH_ALREADY_APPLY;
}
}
}
}
//check whether retry so many times
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
returnCode = UpgradePatchRetry.getInstance(context).onPatchListenerCheck(patchMd5) ? ShareConstants.ERROR_PATCH_OK : Utils.ERROR_PATCH_RETRY_COUNT_LIMIT;
}
}
// Interception some of the request
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
Properties properties = ShareTinkerInternals.fastGetPatchPackageMeta(patchFile);
if (properties == null) {
returnCode = Utils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
} else {
String platform = properties.getProperty(Utils.PLATFORM);
TinkerLog.i(TAG, "get platform:" + platform);
// check patch platform require
if (platform == null || !platform.equals(BuildInfo.PLATFORM)) {
returnCode = Utils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
}
}
}
SampleTinkerReport.onTryApply(returnCode == ShareConstants.ERROR_PATCH_OK);
return returnCode;
}
use of com.tencent.tinker.lib.tinker.TinkerLoadResult in project RxSample by Aload.
the class TinkerPatchListener method patchCheck.
@Override
public int patchCheck(String path) {
File patchFile = new File(path);
TinkerLog.i(TAG, "receive a patch file: %s, file size:%d", path, SharePatchFileUtil.getFileOrDirectorySize(patchFile));
int returnCode = super.patchCheck(path);
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
returnCode = Utils.checkForPatchRecover(NEW_PATCH_RESTRICTION_SPACE_SIZE_MIN, maxMemory);
}
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
String patchMd5 = SharePatchFileUtil.getMD5(patchFile);
SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
int fastCrashCount = sp.getInt(patchMd5, 0);
if (fastCrashCount >= TinkerUncaughtExceptionHandler.MAX_CRASH_COUNT) {
returnCode = Utils.ERROR_PATCH_CRASH_LIMIT;
} else {
Tinker tinker = Tinker.with(context);
if (tinker.isTinkerLoaded()) {
TinkerLoadResult tinkerLoadResult = tinker.getTinkerLoadResultIfPresent();
if (tinkerLoadResult != null) {
String currentVersion = tinkerLoadResult.currentVersion;
if (patchMd5.equals(currentVersion)) {
returnCode = Utils.ERROR_PATCH_ALREADY_APPLY;
}
}
}
}
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
returnCode = UpgradePatchRetry.getInstance(context).onPatchListenerCheck(patchMd5) ? ShareConstants.ERROR_PATCH_OK : Utils.ERROR_PATCH_RETRY_COUNT_LIMIT;
}
}
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
Properties properties = ShareTinkerInternals.fastGetPatchPackageMeta(patchFile);
if (properties == null) {
returnCode = Utils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
} else {
String platform = properties.getProperty(Utils.PLATFORM);
TinkerLog.i(TAG, "get platform:" + platform);
if (platform == null || !platform.equals(BuildInfo.PLATFORM)) {
returnCode = Utils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
}
}
}
TinkerResultReport.onTryApply(returnCode == ShareConstants.ERROR_PATCH_OK);
return returnCode;
}
use of com.tencent.tinker.lib.tinker.TinkerLoadResult in project tinker by Tencent.
the class TinkerLoadLibrary method installNavitveLibraryABI.
/**
* you can reflect your current abi to classloader library path
* as you don't need to use load*Library method above
* @param context
* @param currentABI
*/
public static void installNavitveLibraryABI(Context context, String currentABI) {
Tinker tinker = Tinker.with(context);
if (!tinker.isTinkerLoaded()) {
TinkerLog.i(TAG, "tinker is not loaded, just return");
return;
}
TinkerLoadResult loadResult = tinker.getTinkerLoadResultIfPresent();
if (loadResult.libs == null) {
TinkerLog.i(TAG, "tinker libs is null, just return");
return;
}
File soDir = new File(loadResult.libraryDirectory, "lib/" + currentABI);
if (!soDir.exists()) {
TinkerLog.e(TAG, "current libraryABI folder is not exist, path: %s", soDir.getPath());
return;
}
ClassLoader classLoader = context.getClassLoader();
if (classLoader == null) {
TinkerLog.e(TAG, "classloader is null");
return;
}
TinkerLog.i(TAG, "before hack classloader:" + classLoader.toString());
try {
installNativeLibraryPath(classLoader, soDir);
} catch (Throwable throwable) {
TinkerLog.e(TAG, "installNativeLibraryPath fail:" + throwable);
}
TinkerLog.i(TAG, "after hack classloader:" + classLoader.toString());
}
use of com.tencent.tinker.lib.tinker.TinkerLoadResult in project tinker by Tencent.
the class DefaultLoadReporter method checkAndCleanPatch.
/**
* other process may have installed old patch version,
* if we try to clean patch, we should kill other process first
*/
public void checkAndCleanPatch() {
Tinker tinker = Tinker.with(context);
//only main process can load a new patch
if (tinker.isMainProcess()) {
TinkerLoadResult tinkerLoadResult = tinker.getTinkerLoadResultIfPresent();
//if versionChange and the old patch version is not ""
if (tinkerLoadResult.versionChanged) {
SharePatchInfo sharePatchInfo = tinkerLoadResult.patchInfo;
if (sharePatchInfo != null && !ShareTinkerInternals.isNullOrNil(sharePatchInfo.oldVersion)) {
TinkerLog.w(TAG, "checkAndCleanPatch, oldVersion %s is not null, try kill all other process", sharePatchInfo.oldVersion);
ShareTinkerInternals.killAllOtherProcess(context);
}
}
}
tinker.cleanPatch();
}
Aggregations