阿里AndFix热修复代码逻辑分析

AndFix is a solution to fix the bugs online instead of redistributing Android App. It is distributed as Android Library.

Andfix is an acronym for "Android hot-fix".

AndFix supports Android version from 2.3 to 7.0, both ARM and X86 architecture, both Dalvik and ART runtime, both 32bit and 64bit.

The compressed file format of AndFix's patch is .apatch. It is dispatched from your own server to client to fix your App's bugs.

上面是从AndFix开源库copy过来的一段介绍。AndFix思路清晰,代码简洁,使用简单。git地址:https://github.com/alibaba/AndFix

使用:初始化:
patchManager = new PatchManager(context);
patchManager.init(appversion);//current version
patchManager.addPatch(path);
添加补丁:
patchManager.addPatch(path)

下面是我对AndFix分析得出的一点思路:

patchManager = new PatchManager(this);
	存Context,
	初始化AndFixManager,
		初始化mSecurityChecker
			存Context,
			获取当前应用的mPublicKey,
			如果应用的签名是Android默认签名文件mDebuggable=true
		初始化mOptDir
	初始化mPatchDir
	初始化mPatchs,mLoaders
	
patchManager.init("1.0");
	获取SP_VERSION
	如果SP_VERSION!=当前版本,删除mPatchDir中的所有patchs
	如果SP_VERSION=当前版本,添加mPatchDir中patch到mPatchs集合
		Patch(File file):
			mFile = file;
			mName=PATCH_NAME
			mTime=CREATED_TIME
			mClassesMap保存PATCH.MF文件中name以-Classes结尾的键值对
patchManager.loadPatch();
	mLoaders添加mContext.getClassLoader()
	遍历mPatchs中的变化的classes
	
	验证Patch中的文件的签名是否和app的签名一致,不是的话就不使用次Patch
	验证mOptDir中的文件的MD5是否和SP_NAME中保存的一致
		如果一致:optfile.delete(),否则saveFingerprint = false;
	如果saveFingerprint = false,则保存次optfile的MD5到SP_NAME中
	根据Patch文件初始化一个dexFile
	查找dexFile类并FIX带有MethodReplace注解的方法
			初始化TargetClass
			设置TargetClass的fields to public(JNI)
		mFixedClass保存要修复的类
		AndFix.addReplaceMethod(src, dest);(src:应用内,dest:Patch内)
			replaceMethod(src, dest);(JNI)
			initFields(dest.getDeclaringClass());

patchManager.addPatch(String path)
	复制指定的patch文件到mPatchDir目录
	new一个Patch并添加到mPatchs
	FIX当前Patch中的classes
	验证Patch中的文件的签名是否和app的签名一致,不是的话就不使用次Patch
	。
	。
	。



说明:java这块的代码较简单,容易看到,就是到JNI层 的方法替换,都是C++写的,本人水平有限,就不做说明了。

缺点:这个框架只能用于类方法替换。字段添加,方法添加都是不行的

使用结论:

在Android模拟器上Ok,小米2s也是Ok,但三星就不行,替换没效果!



猜你喜欢

转载自blog.csdn.net/u014763302/article/details/53509648