I am committed to the latest cutting-edge Android technology analysis and use of teaching, do not intend to be a lot of deep things, because how many people are willing to sink your mind to learn the difficulties? I would normally simply mention it. The words are unavoidable and I hope the students like
Hot Fix Introduction
What is hot fix? If you have a project that is already online and has a serious flaw, then your first reaction is to push the new version. So the problem comes. I just got off your app and you asked me to download it again? It's unloading. This results in a decrease in user traffic. And hot fix is to push a patch file to the client (very small), Automatically installs when the user opens the app. Isn't it great?
Andfix Heat Recovery principle
Already translated source code analysis
Ali GitHub website (English)
Recommended self-crossing network
If time permits, I'll help you translate the full version.
Andfix the place of attention
- Add Field method Resource layout modification class is not supported
- Although adding a field is not supported but you can add a local variable inside the method (explained later)
Error:
1. If added in the build patch package will have the following error (everyone first to see do not need to know how to build, explained later)
Success:
Look closely at the text above will have what add modifie (add modified) com.xxx (you modified the file)
Andfix use
-
Add Dependencies First
dependencies { compile‘com.alipay.euler:andfix:0.5.0@aar‘}
-
Initializes an implementation class that is typically in application
We first create a class that inherits the application and then initialize it in the Oncreat method,
Let me introduce a few APIs:
This API returns your app version name ()
java
String appversion; appversion=getPackageManager().getPackageInfo(getPackageName(),0).ersionName;
Take a look at the class Patchmanager of the dependency package
public PatchManager patchManager ;
There are several methods for this class
Parameters are context-generally in application
new PatchManager(this);
初始化 这里传入的是版本名 可以用上面介绍的一个api填入 也可以直接写死
patchManager.init("1.0"); patchManager.init(versionName);
Initialize the load patch (you'll just have to write it down.) The default is called after new comes out and the initial version. This is still not a real load method)
patchManager.loadPatch();
Enter the patch parameter as a string after the entry into effect path you write the file name as well.
Addpatch (Your patch path)
removing patch paths
Patchmanager.removeallpatch ();
String Path = Environment. getExternalStorageDirectory(). Getabsolutefile() + File. Separator+"Out.apatch";File File = new file (path);if (file. Exists()) {Log. E("Fmy","File exists");try {Patchmanager. Addpatch(path);Patchmanager. Removeallpatch();Log. E("Fmy","Hot fix succeeded");} catch (IOException e) {Log. E("Fmy","Hot fix failed:"+e. GetMessage());}}else{Log. E("Fmy","file does not exist");}
Full code:
PackageCom.example.administrator.myapplication;ImportAndroid.app.Application;ImportAndroid.os.Environment;ImportAndroid.util.Log;ImportCom.alipay.euler.andfix.patch.PatchManager;ImportJava.io.File;ImportJava.io.IOException;/** * Created by Administrator on 2016/11/9. * * Public class mainappliacation extends application { PublicPatchmanager Patchmanager;PrivateString path;@Override Public void onCreate() {Super. OnCreate (); Patchmanager =NewPatchmanager ( This); Patchmanager.init ("1.0"); Patchmanager.loadpatch (); Path = Environment.getexternalstoragedirectory (). Getabsolutefile () + File.separator +"Out.apatch"; File File =NewFile (path);if(File.exists ()) {LOG.E ("Fmy","File exists");Try{Patchmanager.addpatch (path); Patchmanager.removeallpatch (); LOG.E ("Fmy","Hot fix succeeded"); }Catch(IOException e) {LOG.E ("Fmy","Hot fix failed:"+e.getmessage ()); } }Else{LOG.E ("Fmy","file does not exist"); } }}
-
Add permissions and associate our custom application
Permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
Associate our Custom application class
<application android:name=".MainAppliacation" ....
Full manifest file:
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.example.administrator.myapplication"> <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" / > <applicationandroid:name=". Mainappliacation "android:allowbackup=" true "android:icon=" @mipmap/ic_ Launcher "android:label=" @string/app_name "android:supportsrtl=" true "android:theme=" @style/apptheme "> <activity android:name=". Mainactivity "> <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </Application></manifest>
-
Generate two signed apk (same signature as required) for old and new versions
Suppose our original APK two class one is the activity class and one of our custom application classes (as already stated above)
Let's look at the activity file
PackageCom.example.administrator.myapplication;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public class mainactivity extends appcompatactivity {String name ="Hello";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); }//Click event for a button Public void OnClick(View view) {Toast.maketext ( This, name, Toast.length_short). Show (); }}
Look at the layout file.
<?xml version= "1.0" encoding= "Utf-8"?><Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools" Android:id="@+id/activity_main" Android:layout_width="Match_parent" Android:layout_height="Match_parent" Android:paddingbottom="@dimen/activity_vertical_margin" Android:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" Tools:context="Com.example.administrator.myapplication.MainActivity"> <buttonandroid:onclick="OnClick"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" See if the hot fix succeeded " / > </relativelayout>
Probably.
Pack and sign this apk and name it old.apk (here is a good name for the back)
Modify the Activity file
Changed the value of name
PackageCom.example.administrator.myapplication;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public class mainactivity extends appcompatactivity {String name ="fixed!!!!";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); }//Click event for a button Public void OnClick(View view) {Toast.maketext ( This, name, Toast.length_short). Show (); }}
Pack and sign this apk and name it new.apk (here is a good name for the back)
-
Download Build Patch Tool
Ali GitHub website (English)
在github下载源码不用我教吧?把阿里整个master下载到本地 并解压
Open the Tools directory of the above directory to unzip the toolkit to your preferred location
Now I open the directory that I unzipped
-
Put our generated new.apk and old.apk and signed files in this folder
-
Generate Patches
In this directory, press shit + right mouse button to select to see an option to "open a command Window here"->> It's not good to turn it off so I'm so sorry
In the Jump Command window, enter the following command
Command: Apkpatch.Bat- F New.apk- TOld.apk- oOutput1- kDebug.KeyStore- PAndroid -AAndroiddebugkey- eAndroid- F <New.apk>: New version- T <Old.apk>: Old version- o <Output>: Output Directory- k <KeyStore>: KeyStore used for packaging- P <Password>: KeyStore's password -A <Alias>: KeyStore User Alias- e <Alias password>: KeyStore User alias password
Prompt in the DOS Window press TAB to enter a file name under this folder the first letter or a paragraph can be prompted OH
-o o 我这里的用法是 在此目录下创建一个o文件夹然后将生产的东西放入此文件夹
Let's open up and see what's in our O folder.
Can see there is a call
Files for "New-4d459be7271c372742862b1a885b176a.apatch"
This is the other file that we need. I'll add some of the issues like reinforcement later.
We renamed this file "Out.apatch"
Now, let's do a demo:
First install old.apk to the phone we run to see
We import the file Out.apath into the emulator's SD card root directory in the shutdown program (remember to clear the background!!!!!) Otherwise you will not recall the application Oncreat
What? Why didn't it become a string that we changed back to? Actually, I deliberately wrote down a pit, and my purpose is simple. If you let your classmates write down plain sailing you will never be affected profoundly on andfix which parts can be hot repaired such as the previous layout file etc.
The reason why this is not coming out is because he has to re-create a field in the name Class (The new field is not hot-repaired), so the Unlock method is as follows
PackageCom.example.administrator.myapplication;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public class mainactivity extends appcompatactivity {String name ="Hello";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); }//Click event for a button Public void OnClick(View view) {name ="Fixed"; Toast.maketext ( This, name, Toast.length_short). Show (); }}
Then regenerate a new.apk in the Times and sign the file to generate a new patch effect on it out. Here I don't repeat the boring steps, I'm now importing the correct patch
(^__^) I'm bored, right?
Later on I will teach you a new hot fix
Ali Hundred Chuan Plan ...
The Andfix of Androge repair