Every time I go home lazy, do not want to tidy up, this weekend, forcing themselves to organize, the content must be very full.
Objective
With the app version upgrade iteration, inevitably some bugs will appear, users upgrade the new version of the cost is higher, if you can make hot patches to the app, hot update off the app bug, not better.
Andfix
Andfix is a hot repair framework of Ali, updated so far, has been relatively perfect to meet our daily needs. It has many advantages, such as:
1. Hot Fix free Restart app
2. Small update package
3. Support 360 reinforcement (many blogs say no support, in fact, support, the following will explain how to use)
As for the shortcomings, I do not say, haha.
Plot for heat recovery
How to use 1. Add Dependencies in Android Studio
compile ‘com.alipay.euler:andfix:[email protected]‘
1. Initialize the Andfix in the OnCreate method of the application
// 初始化patch管理类 new PatchManager(this); // 初始化patch版本 String"1.0"; try { 0).versionName;//获取app版本号 catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } mPatchManager.init(appVersion); // 加载已经添加到PatchManager中的patch mPatchManager.loadPatch();
The appversion does not change at the time of the hot fix version.
Page layout
For the convenience of testing, the main page has 3 buttons, the first one to show whether there is a bug, the second is to download the hot fix package from the network (need to build a Web service), and then hot fix, the third is used to load the hot fix package from the local file (no self-built Web service).
Code
Public class mainactivity extends appcompatactivity { PrivateMyApplication app;Private Static FinalString Local_name ="Local.apatch";//update file on local SD card [2 Select 1] Private Static FinalString Net_name ="Net.apatch";//Update files on the network [2 Select 1] Private FinalString url="Http://192.168.1.2/net.apatch";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//To simulate whether there is a bugFindviewbyid (R.id.button2). Setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View view) {Toast.maketext (mainactivity). This,"There is a bug", Toast.length_short). Show ();//Toast.maketext (mainactivity.this, "Bug fixed", Toast.length_short). Show (); //[Second version comment first line, cancel second line comment]} });//network download and hot fixFindviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View view) {fix (); } });//local hot fixFindviewbyid (R.id.button3). Setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View view) {update (); } }); } .....
Two ways to update
//local Hot update Private void Update() {String patchfilestr = Environment.getexternalstoragedirectory (). GetAbsolutePath () +file.separator+ LOCAL_NAME;Try{MyApplication.mPatchManager.addPatch (PATCHFILESTR); }Catch(IOException e) {E.printstacktrace (); } }//Network download hot update Private void Fix() {NewThread () {@Override Public void Run() {Try{String patchfilestr = Environment.getexternalstoragedirectory (). GetAbsolutePath (); Downloadfromurl (URL, net_name, patchfilestr); MyApplication.mPatchManager.addPatch (PATCHFILESTR);//Hot fix}Catch(Exception e) {E.printstacktrace (); }}}.start (); }
To reduce the amount of code, the network request here does not use the framework, but instead writes the network request method
/** * Download files from the network URL * * @param urlstr * @param fileName * @param Savepath * @throws ioexception * * Public Static void Downloadfromurl(String urlstr, String fileName, String savepath)throwsIOException {URL url =NewURL (URLSTR); HttpURLConnection conn = (httpurlconnection) url.openconnection ();//Set timeout to 3 secondsConn.setconnecttimeout (3* +);//Prevent the masking program from crawling and return 403 errorConn.setrequestproperty ("User-agent","mozilla/4.0 (compatible; MSIE 5.0; Windows NT; Digext) ");//Get input streamInputStream InputStream = Conn.getinputstream ();//Get yourself an array byte[] GetData = Readinputstream (InputStream);//File Save locationFile Savedir =NewFile (Savepath);if(!savedir.exists ()) {Savedir.mkdir (); } File File =NewFile (Savedir + file.separator + fileName); FileOutputStream fos =NewFileOutputStream (file); Fos.write (GetData);if(Fos! =NULL) {fos.close (); }if(InputStream! =NULL) {inputstream.close (); } System.out.println ("INFO:"+ URL +"Download Success"); }/** * Get byte array from input stream * * @param inputstream * @return * @throws ioexceptio n * / Public Static byte[]Readinputstream(InputStream InputStream)throwsIOException {byte[] buffer =New byte[1024x768];intLen =0; Bytearrayoutputstream BOS =NewBytearrayoutputstream (); while(len = inputstream.read (buffer))! =-1) {bos.write (buffer,0, Len); } bos.close ();returnBos.tobytearray (); }
Compile
Project compilation requires your own signature files, and if not, create a
Generate APK with bug
Rename the generated app-release.apk file to bug.apk
Generate apk to fix bug
Comment out the toast that has the bug and change it to a bug-repaired toast
Compile again to rename the generated app-release.apk file to fixedbug.apk
Generate Apatch
Need to download tool here
https://github.com/alibaba/AndFix
Tools folder is a tool that downloads to a local
By the way, put just two bug.apk and fixedbug.apk and signed files into the folder
Because we only have one fixedbug.apk, so we use the following generated code
apkpatch -f <new> -t <old> -o <output> -k <keystore> -p <***> -a <alias> -e <***> -a,--alias <alias> keystore entry alias. -e,--epassword <***> keystore entry password. -f,--from <loc> new Apk file path. -k,--keystore <loc> keystore path. -n,--name <name> patch name. -o,--out <dir> output dir. -p,--kpassword <***> keystore password. -t,--to <loc> old Apk file path.
Specific practices
1. First open cmd or terminal, CD to the current directory
2. Typing
apkpatch-1.0.3/apkpatch.sh -f fixedbug.apk -t bug.apk -o Out -k a.jks -p 123456 -a 654321 -e 654321
3. At this time the Apatch file under this out folder is the Hot update package
4. Rename the file Net.apatch into the WWW directory
Ensure that just the Http://192.168.1.2/net.apatch path can be accessed
Start the test first install bug.apk on the phone
1. Open Ddms
2. Copy the Net.apatch into the phone named Local.apatch
BUG.APK can also be put into mobile phone, easy to install (or other way to install bug.apk)
3. Open the app and run
A bug occurred
4. Click Local file Hot fix, then click Show again
Prompt bug fixed
This is done from the local file hot fix demo
5. Uninstall the app, reinstall and then click on the display, the bug, click the network download hot fix, click the show again, prompt bug fixed (same)
Summarize
At this point two methods of hot repair have solved the Appbug, code is not complex, need to write a message source.
Add
When a team is collaborating, multiple teams generate multiple Apatch files that can be combined using tools
apkpatch -m <apatch_path...> -o <output> -k <keystore> -p <***> -a <alias> -e <***> -a,--alias <alias> keystore entry alias. -e,--epassword <***> keystore entry password. -k,--keystore <loc> keystore path. -m,--merge <loc...> path of .apatch files. -n,--name <name> patch name. -o,--out <dir> output dir. -p,--kpassword <***> keystore password.
Confusing code
-keep class * extends java.lang.annotation.Annotation-keepclasseswithmembernames class * { native <methods>;}
Close contact with Android Thermal repair Andfix Technology