Update or fix an android app on the fly, without having to publish a new APK.
- Make a .jar library with your app's classes and methods that you want to be updatable (see compiling your application as a library)
- Grab Hotpatch.java and add it to your project
- Load the .jar library you built earlier
You might need to do a small refactor of your app's code, but the advantages are many:
- Quickly fix & deploy a patch for a method
- Add methods to classes
- Hotpatch does not need the app to restart
- Updating an app using Hotpatch does not require root!
Let's say we have a class that we want to use in our Android app, defined this way:
package com.chars.testlib.TestLib;
public class TestLib {
public String getVersionString() {
return "libversion 1.0";
}
}
After making a .jar library of that class, deploy it to you device i.e in /sdcard/TestLib.jar
In order to use it in your Android app, you must load it with Hotpatch
final String className = "com.chars.testlib.TestLib";
final String methods[] = {"getVersionString"};
final Hotpatch hotpatch = new Hotpatch();
try {
hotpatch.loadLibrary("/sdcard/TestLib.jar", getApplicationContext());
hotpatch.loadClass(className);
hotpatch.loadMethods(className, methods);
String result = (String)hotpatch.call(className, methods[0]);
Log.d("AndroidHotpatch", result);
} catch (Exception e) {
Log.e("AndroidHotpatch", Log.getStackTraceString(e));
}
The line
String result = (String)hotpatch.call(className, methods[0]);
will execute the getVersionString() method, defined in class TestLib.
To update the library, just make a new .jar from an updated version of the class. For example:
package com.chars.testlib.TestLib;
public class TestLib {
public String getVersionString() {
return "libversion 2.0";
}
}
Push the updated .jar to the same path as the previous. In your Android app, you can just call
hotpatch.reload();
and you'll have your updated library loaded into the app. Now, whenever you execute getVersionString() you will get "libversion 2.0"
- Start a new android project
- Add the classes that you want to be updatable
- Build an APK
- Rename the .apk file to .jar
- v0.1 Alpha:
- Support for methods
- Implemented Hotpatch.loadLibrary()
- Implemented Hotpatch.loadClass()
- Implemented Hotpatch.loadMethods()
- Implemented Hotpatch.reload()
- Implemented Hotpatch.call()