How to use AndFix to hot-patch your Android app?

Rui Geng
AndroidPub
Published in
4 min readApr 6, 2017

--

Let’s imagine you are developing a financial app, and suddenly you found a critical bug. What should you do? It might be take 2 or 3 days to wait for the app being updated to all the users if you re-launch it. So, in this case, I would suggest you master at least one hot-patching approach, as it might save your business in the future!

There are a number of ways to hot-patch your Android app, and AndFix is definitely one of the best ones. In this article, I will show you how I use the AndFix lib to hot-patch my Android app.

The AndFix link on github is: https://github.com/alibaba/AndFix

I won’t explain the inside structure of this lib, but just showing you how to use it, as the lib itself is open source, and if you would like to dig deeper, just go through the code. Let’s start.

Firstly, I would suggest you download the ExampleProject from the repository, from where you can start to run the app with hassle free, and don’t have to pay too much attention to initialising the example project.

In the build.gradle, you can find there is a line in the dependencies block, which is for adding the dependency. Nothing needs to explain I believe.

dependencies {
compile 'com.alipay.euler:andfix:0.5.0@aar'
}

Before touching the logic, I would suggest you take a look at your computer to find your IP address (in my case 192.168.0.119), and set it to the URL field in the AndFixUtil.class without removing the “out.apatch” before the address.

For example, if your address is 192.168.1.20, then set it as

public static final String URL = "http://https://www.linkedin.com/redir/invalid-link-page?url=192.168.1.20/out.apatch"

In the AndFixUti. class, you can find how the PatchManager being initialised.

final PatchManager patchManager = new PatchManager(context);
patchManager.init(BuildConfig.VERSION_CODE + "");//current version
patchManager.loadPatch();

After the initialisation, we should download the patch and add the patch into the app.

//... download the patch, not too many things need to be explained// add the patch
patchManager.addPatch(patchPath);

Then open the A.class, from where you can find this:

public String getHello() {
return "Bye"; // which should be "Hello"
}

You can find there is a bug in the app, as the getHello() method should print “Hello” instead of “Bye”. So this is the bug we are going to fix in this example.

But don’t worry, let’s use AndroidStudio to generate a SignedAPK file first and name it as 1.apk. Install the 1.apk file into your phone and you will find the text “Bye” being shown on the screen.

At this point, you should know we are going to fix the output content in the getHello method from A class, right?

So, let’s fix it as below.

public String getHello() {
return "Hello"; // it has been changed to Hello, which is correct.
}

And generate an APK with the same keystore being used when you generated the previous one and name it as 2.apk. Now, you have got 1.apk and 2.apk on your computer, and the 1.apk has been installed.

Now, we should download a tool called apkpatch, decompress it, and copy & paste the following files under apkpatch folder:

  • 1.apk;
  • 2.apk;
  • keystore file, “test.jks” in my case

And run the command below:

sh apkpatch.sh -f 2.apk -t 1.apk -o . -k test.jks -p 123456 -a key0 -e 123456

where the last 3 params are the passwords and alias of your keystore file, and you can get more info from here:

usage: 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.

After running this, I think you can find a .apatch file. Let’s rename is as out.apatch.

If you want your Android app to download the patch file, you should build a server. But let’s keep everything easy, just download an XAMPP, and put the out.apatch file to the /htdocs folder. Run the Apache server.

So let’s start to run the app on your phone. When you open the app, you can find the “Bye” is still shown on the screen, but you should know that the app is downloading the patch or has downloaded the patch file into your phone. Then you should try to click the UPDATE button, if everything you have configured is correct, you can find the TextView is showing “Hello” now.

So you see, the AndFix is really very easy to understand and implement. But this is only an example, which means if you want to apply it on your business project, you have to consider:

  • How often to check the updates?
  • When and how to load the patch?
  • How to design your backend server so that every client can get the patch in time?
  • How to make sure your patch is secure enough?
  • and etc…

So, good luck ;)

--

--

Rui Geng
AndroidPub

Android developer@voltbank, PhD student in AI@USYD