|
2 | 2 | [](https://github.com/charslab/Android-Hotpatch/releases) |
3 | 3 | [](https://github.com/charslab/Android-Hotpatch/blob/master/LICENSE) |
4 | 4 |
|
5 | | -Autopdate an android app on the fly |
| 5 | +Update or fix an android app on the fly, without having to publish a new APK. |
| 6 | + |
6 | 7 |
|
7 | 8 | ## Usage: |
8 | 9 |
|
9 | | -## Example: |
| 10 | +1. Make a .jar library with your app's classes and methods that you want to be updatable (see #compiling a library) |
| 11 | +2. Grab [Hotpatch.java](https://github.com/charslab/Android-Hotpatch/blob/master/app/src/main/java/com/chars/android_hotpatch/Hotpatch.java) and add it to your project |
| 12 | +3. Load the .jar library you built earlier |
| 13 | + |
| 14 | +You might need to do a small refactor of your app's code, but **the advantages are many:** |
| 15 | + |
| 16 | +- Quickly fix & deploy a patch for a method |
| 17 | +- Add methods to classes |
| 18 | +- Hotpatch does not need the app to restart |
| 19 | + |
| 20 | + |
| 21 | +## Quick usage demo: |
| 22 | + |
| 23 | +Let's say we have a class that we want to use in our Android app, defined this way: |
10 | 24 |
|
| 25 | +```JAVA |
| 26 | +package com.chars.testlib.TestLib; |
| 27 | + |
| 28 | +public class TestLib { |
| 29 | + public String getVersionString() { |
| 30 | + return "libversion 1.0"; |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +After making a .jar library of that class, deploy it to you device i.e in */sdcard/TestLib.jar* |
| 36 | + |
| 37 | +In order to use it in your Android app, you must load it with Hotpatch |
11 | 38 |
|
12 | 39 | ```JAVA |
13 | | - final String className = "com.chars.testlib.TestLib"; |
14 | | - final String methods[] = {"getVersionString"}; |
| 40 | +final String className = "com.chars.testlib.TestLib"; |
| 41 | +final String methods[] = {"getVersionString"}; |
| 42 | + |
| 43 | +final Hotpatch hotpatch = new Hotpatch(); |
15 | 44 |
|
16 | | - final Hotpatch hotpatch = new Hotpatch(); |
| 45 | +try { |
| 46 | + hotpatch.loadLibrary("/sdcard/TestLib.jar", getApplicationContext()); |
| 47 | + hotpatch.loadClass(className); |
| 48 | + hotpatch.loadMethods(className, methods); |
| 49 | + |
| 50 | + String result = (String)hotpatch.call(className, methods[0]); |
| 51 | + Log.d("AndroidHotpatch", result); |
| 52 | + |
| 53 | +} catch (Exception e) { |
| 54 | + Log.e("AndroidHotpatch", Log.getStackTraceString(e)); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The line |
| 59 | + |
| 60 | + String result = (String)hotpatch.call(className, methods[0]); |
17 | 61 |
|
18 | | - try { |
19 | | - hotpatch.loadLibrary("/sdcard/TestLib.jar", getApplicationContext()); |
20 | | - hotpatch.loadClass(className); |
21 | | - hotpatch.loadMethods(className, methods); |
22 | | - |
23 | | - String result = (String)hotpatch.call(className, methods[0]); |
24 | | - Log.d("AndroidHotpatch", result); |
25 | | - |
26 | | - } catch (Exception e) { |
27 | | - Log.e("AndroidHotpatch", Log.getStackTraceString(e)); |
28 | | - } |
| 62 | +will execute the *getVersionString()* method, defined in class *TestLib*. |
| 63 | + |
| 64 | +To update the library, just make a new .jar from an updated version of the class. For example: |
| 65 | + |
| 66 | +```JAVA |
| 67 | +package com.chars.testlib.TestLib; |
| 68 | + |
| 69 | +public class TestLib { |
| 70 | + public String getVersionString() { |
| 71 | + return "libversion 2.0"; |
| 72 | + } |
| 73 | +} |
29 | 74 | ``` |
| 75 | +Push the updated .jar to the same path as the previous. In your Android app, you can just call |
| 76 | + |
| 77 | + hotpatch.reload(); |
| 78 | + |
| 79 | +and you'll have your updated library loaded into the app. Now, whenever you execute *getVersionString()* you will get *"libversion 2.0"* |
| 80 | + |
| 81 | +## Compiling an application as a library: |
30 | 82 |
|
31 | 83 |
|
32 | 84 |
|
0 commit comments