andfix Hot Patch Repair
background:
我们知道Android在发布新版本后如果发现某个方法有bug时native app是不能像webapp让用户无感知的实现更新代码的,我们必须要提醒用户下载新版本;在这种情况下我们就会希望有一种方法可以实现app的在线热修复;
Many Daniel have mentioned several methods of heat repair:
1. Https://github.com/alibaba/AndFix
2. https://github.com/alibaba/dexposed
3. Https://github.com/jasonross/Nuwa
4. Https://github.com/bunnyblue/DroidFix
5. Https://github.com/dodola/HotFix
Their rationale comes from the introduction of the Android app hot patch dynamic Repair technology, as well as the mobile Dex subcontracting program; Interested friends everyone go to see it;
The first and second are Alibaba open-source frameworks, and I don't know much about them, hahaha ...
Here mainly the use of andfix, and the specific demo, a lot of the original rational things everyone to see, I can not fraught ah ...
Andfix Specific Use (Androidstudio)
Eclipse Friends can download the project and then reference it (similar to Method 1)
One: Two ways of quoting:
- Download the Andfix project from Https://github.com/alibaba/AndFix, and the new Androidstudio project will andfix as a module reference to
- Referencing in the Build.gradle file
Compile ' com.alipay.euler:andfix:0.3.1 '
Second: Import so file
Create Jnilibs folder to import the corresponding so file
Three: Code implementation
- Initialize in application:
PackageCom.android.shub.andfixtest;ImportAndroid.app.Application;ImportAndroid.content.Context;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.PackageManager;ImportAndroid.os.Environment;ImportAndroid.util.Log;ImportCom.alipay.euler.andfix.patch.PatchManager;ImportJava.io.IOException;/** * Created by shubing on 2016/3/15. * * Public class myapplication extends application { PrivatePatchmanager Mpatchmanager;Private Static FinalString Apatch_path ="/output.apatch";Private Static FinalString DIR ="Apatch";//Patch folder @Override Public void onCreate() {Super. OnCreate (); Mpatchmanager =NewPatchmanager ( This); Mpatchmanager.init ("1.0"); Mpatchmanager.loadpatch (); String patchfilestring = Environment.getexternalstoragedirectory (). GetAbsolutePath () + Apatch_path; LOG.E ("Test hotfix-------->","Patch file Collar: "+ patchfilestring + "" ");Try{//Add the downloaded patch package pathMpatchmanager.addpatch (patchfilestring);//Remove patches after loading patches, otherwise new patches will not take effect ohMpatchmanager.removeallpatch (); }Catch(IOException e) {E.printstacktrace (); } }/** * Get app Info * * @param context * @return * * Public StaticPackageInfoGetpackageinfo(Context context) {PackageInfo PackageInfo =NULL;Try{PackageInfo = Context.getpackagemanager (). Getpackageinfo (Context.getpackagename (),0); }Catch(Packagemanager.namenotfoundexception e) {E.printstacktrace (); }returnPackageInfo; }}
Mainactivity simple, on the two Toast method, the original package on a toast method, modified to add a toast, successively generated two apk
PackageCom.android.shub.andfixtest;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast; Public class mainactivity extends appcompatactivity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Showtoast ();//Showtoastnew ();}Private void Showtoast() {Toast.maketext ( This,"First Toast", Toast.length_short). Show (); }Private void showtoastnew() {Toast.maketext ( This,"a second toast! ", Toast.length_short). Show (); }@Override protected void OnDestroy() {Super. OnDestroy (); Android.os.Process.killProcess (Android.os.Process.myPid ()); }}
IV: Generate a patch pack
Use the Apkpatch tool to generate a patch pack for the above two new and old apk; Apkpatch Download point here
GITHTB on the other hand, I passed it up here, download the decompression, open the cmd command line, CD to Apkpatch.bat directory:
Specific commands can be referenced on GitHub, which I quote down here:
Apkpatch-f ' new apk path '-t ' old apk path '-o ' generated patch package output directory '-K ' package signed KeyStore file path '-P ' signature file password '-a ' keyStore alias '-e ' KeyStore entry's Secret Code
(single quotation mark when the command is lost Oh!!) )
After the command is executed, the corresponding patch package is generated, and the fix pack is renamed to the Output.apatch defined in the MyApplication and placed in the specified path in the MyApplication.
Note: application's OnCreate method, XML binding method, and construction method cannot be fixed
Summary:
At this point the use of Andfix is all over, it is quite simple to use, the principle is still worth probing
demo:http://download.csdn.net/detail/shubing521/9512028
Andfix Hot Patch Repair