├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ ├── Baidu.xml
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── qaplug_profiles.xml
├── vcs.xml
└── workspace.xml
├── HotFix.iml
├── HotFix_Git.iml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── app.iml
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── dodola
│ │ └── hotfix
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── assets
│ │ ├── hackdex_dex.jar
│ │ └── path_dex.jar
│ ├── java
│ │ └── dodola
│ │ │ └── hotfix
│ │ │ ├── BugClass.java
│ │ │ ├── HotfixApplication.java
│ │ │ ├── LoadBugClass.java
│ │ │ ├── MainActivity.java
│ │ │ └── Utils.java
│ └── res
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── content_main.xml
│ │ ├── menu
│ │ └── menu_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-v21
│ │ └── styles.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── dodola
│ └── hotfix
│ └── ExampleUnitTest.java
├── build.gradle
├── buildSrc
├── build.gradle
├── buildSrc.iml
└── src
│ └── main
│ └── groovy
│ └── dodola
│ └── patch
│ └── PatchClass.groovy
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── hackdex
├── .gitignore
├── build.gradle
├── hackdex.iml
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── dodola
│ │ └── hackdex
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── dodola
│ │ │ └── hackdex
│ │ │ └── AntilazyLoad.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── dodola
│ └── hackdex
│ └── ExampleUnitTest.java
├── hotfixlib
├── .gitignore
├── build.gradle
├── hotfixlib.iml
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── dodola
│ │ └── hotfixlib
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── dodola
│ │ │ └── hotfixlib
│ │ │ └── HotFix.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── dodola
│ └── hotfixlib
│ └── ExampleUnitTest.java
├── img
├── patch1.png
├── patch2.png
└── patch3.png
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | /*/build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | .DS_Store
30 | .classpath
31 | .project
32 | /.settings
33 | /bin
34 | /build
35 | /gen
36 | /obj
37 | project.properties
38 | local.properties
39 | /assets
40 | /res
41 | /dist
42 |
43 |
44 | .idea/
45 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | HotFix_Git
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/.idea/copyright/Baidu.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | Abstraction issues
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 | Baidu
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 | 1.8
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/qaplug_profiles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/HotFix.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/HotFix_Git.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 dodola
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HotFix(Deprecated)
2 |
3 | ## 请关注 [RocooFix](https://github.com/dodola/RocooFix)
4 | 我重新写了一个[RocooFix](https://github.com/dodola/RocooFix)框架,解决了Nuwa因为Gradle1.40 里Transform API无法打包的情况,现在兼容Gradle1.3-Gradle2.1.0版本
5 |
6 | 安卓App热补丁动态修复框架
7 |
8 | ##介绍
9 | 该项目是基于QQ空间终端开发团队的技术文章实现的,完成了文章中提到的基本功能。
10 |
11 |
12 | 文章地址:[安卓App热补丁动态修复技术介绍](http://zhuanlan.zhihu.com/magilu/20308548)
13 |
14 | 项目部分代码从 [dalvik_patch](https://github.com/simpleton/dalvik_patch) 项目中修改而来,这个项目本来是用来实现multidex的,发现可以用来实现方法替换的效果。
15 |
16 | 项目包括核心类库,补丁制作库,例子。可以直接运行代码看效果。
17 |
18 | **文章作者Github:** [jiqimaogou](https://github.com/jiqimaogou)
19 |
20 | **类似项目:** [Nuwa](https://github.com/jasonross/Nuwa) 这个项目补丁自动化那块做的很完整,感兴趣的可以去看
21 |
22 |
23 | ##详细说明
24 | ###补丁制作
25 | 该技术的原理很简单,其实就是用ClassLoader加载机制,覆盖掉有问题的方法。所以我们的补丁其实就是有问题的类打成的一个包。
26 |
27 | 例子中的出现问题的类是 `dodola.hotfix.BugClass`
28 | 原始代码如下:
29 |
30 | ```java
31 | public class BugClass {
32 |
33 | public String bug() {
34 | return "bug class";
35 | }
36 | }
37 | ```
38 |
39 | 我们假设`BugClass`类里的`bug()`方法出现错误,需要修复,修复代码如下:
40 |
41 | ```java
42 |
43 | public class BugClass {
44 |
45 | public String bug() {
46 | return "fixed class";
47 | }
48 | }
49 |
50 | ```
51 |
52 | 那么我们只需要将修复过的类编译后打包成dex即可
53 |
54 | 步骤如下:
55 |
56 | 1. 将补丁类提取出来到一个文件夹里
57 | 
58 |
59 | 2. 将class文件打入一个jar包中 `jar cvf path.jar *`
60 | 3. 将jar包转换成dex的jar包 `dx --dex --output=path_dex.jar path.jar`
61 |
62 | 这样就生成了补丁包`path_dex.jar`
63 |
64 | 
65 |
66 |
67 | ###实现javassist动态代码注入
68 |
69 |
70 | 实现这一部分功能的原因主要是因为出现如下异常
71 |
72 | ` java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
73 | `
74 |
75 | 问题原因在文档中已经描述的比较清楚。
76 |
77 | >
78 | 就是如果以上方法中直接引用到的类(第一层级关系,不会进行递归搜索)和clazz都在同一个dex中的话,那么这个类就会被打上**CLASS_ISPREVERIFIED**
79 |
80 | 很明显,解决的方法就是在类中引用一个其他dex中的类,但是源码方式的引用会将引用的类打入同一个dex中,所以我们需要找到一种既能编译通过并且将两个互相引用的类分离到不同的dex中,于是就有了这个动态的代码植入方式。
81 |
82 | 首先我们需要制作引用类的dex包,代码在`hackdex`中,我直接使用了文档中的类名 `AntilazyLoad` 这样可以和文章中对应起来,方便一些。
83 |
84 | 我们将这个库打包成dex的jar包,方法跟制作补丁一样。
85 |
86 | 下面是重点,我们要用`javassist`将这个类在编译打包的过程中插入到目标类中。
87 |
88 | 为了方便,我将这个过程做成了一个Gradle的Task,代码在`buildSrc`中。
89 |
90 | 这个项目是使用Groovy开发的,需要配置Groovy SDK才可以编译成功。
91 |
92 | 核心代码如下:
93 |
94 | ```groovy
95 | /**
96 | * 植入代码
97 | * @param buildDir 是项目的build class目录,就是我们需要注入的class所在地
98 | * @param lib 这个是hackdex的目录,就是AntilazyLoad类的class文件所在地
99 | */
100 | public static void process(String buildDir, String lib) {
101 |
102 | println(lib)
103 | ClassPool classes = ClassPool.getDefault()
104 | classes.appendClassPath(buildDir)
105 | classes.appendClassPath(lib)
106 |
107 | //下面的操作比较容易理解,在将需要关联的类的构造方法中插入引用代码
108 | CtClass c = classes.getCtClass("dodola.hotfix.BugClass")
109 | println("====添加构造方法====")
110 | def constructor = c.getConstructors()[0];
111 | constructor.insertBefore("System.out.println(dodola.hackdex.AntilazyLoad.class);")
112 | c.writeFile(buildDir)
113 |
114 |
115 |
116 | CtClass c1 = classes.getCtClass("dodola.hotfix.LoadBugClass")
117 | println("====添加构造方法====")
118 | def constructor1 = c1.getConstructors()[0];
119 | constructor1.insertBefore("System.out.println(dodola.hackdex.AntilazyLoad.class);")
120 | c1.writeFile(buildDir)
121 |
122 |
123 | growl("ClassDumper", "${c.frozen}")
124 | }
125 | ```
126 |
127 | 下面在代码编译完成,打包之前,执行植入代码的task就可以了。
128 |
129 | 在 app 项目的 build.gradle 中插入如下代码
130 |
131 | ```groovy
132 | task('processWithJavassist') << {
133 | String classPath = file('build/intermediates/classes/debug')//项目编译class所在目录
134 | dodola.patch.PatchClass.process(classPath, project(':hackdex').buildDir
135 | .absolutePath + '/intermediates/classes/debug')//第二个参数是hackdex的class所在目录
136 |
137 | }
138 |
139 | android{
140 | .......
141 | applicationVariants.all { variant ->
142 | variant.dex.dependsOn << processWithJavassist //在执行dx命令之前将代码打入到class中
143 | }
144 | }
145 |
146 | ```
147 |
148 | 反编译编译后的apk可以发现,代码已经植入进去,而且包里并不存在` dodola.hackdex.AntilazyLoad` 这个类
149 |
150 | 
151 |
152 |
153 | ###补丁加载过程分析
154 |
155 | ##关于混淆
156 | 文档里已经给出了解决方案。
157 |
158 | >1. 在正式版本发布的时候,会生成一份缓存文件,里面记录了所有class文件的md5,还有一份mapping混淆文件。
159 | 2. 在后续的版本中使用-applymapping选项,应用正式版本的mapping文件,然后计算编译完成后的class文件的md5和正式版本进行比较,把不相同的class文件打包成补丁包。
160 |
161 |
162 | 总的来说就是从有补丁功能的版本开始,保存一份mapping混淆文件,后续编译用同一个混淆mapping文件,这样就能保证混淆过后的类名始终是一致的。
163 |
164 | 这样打补丁混淆就能完全的自动化了。
165 |
166 |
167 | ##ISSUE
168 | 1. 开发测试过程中遇到一些问题,这种方法无法在已经加载好的类中实现动态替换,只能在类加载之前替换掉。就是说,补丁下载下来后,只能等待用户重启应用才能完成补丁效果。
169 | 2. 有同学反馈在一加手机上会出现` Class ref in pre-verified class resolved to unexpected`的错误,待找到手机后修复。。
170 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | task('processWithJavassist') << {
4 | String classPath = file('build/intermediates/classes/debug')//项目编译class所在目录
5 | dodola.patch.PatchClass.process(classPath, project(':hackdex').buildDir
6 | .absolutePath + '/intermediates/classes/debug')//第二个参数是hackdex的class所在目录
7 |
8 | }
9 |
10 | android {
11 | compileSdkVersion 23
12 | buildToolsVersion "23.0.1"
13 |
14 | defaultConfig {
15 | applicationId "dodola.hotfix"
16 | minSdkVersion 15
17 | targetSdkVersion 23
18 | versionCode 1
19 | versionName "1.0"
20 | }
21 | buildTypes {
22 | debug {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
25 | }
26 | release {
27 | minifyEnabled false
28 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
29 | }
30 | }
31 | applicationVariants.all { variant ->
32 | variant.dex.dependsOn << processWithJavassist //在执行dx命令之前将代码打入到class中
33 | }
34 | }
35 |
36 |
37 |
38 | dependencies {
39 | compile fileTree(include: ['*.jar'], dir: 'libs')
40 | testCompile 'junit:junit:4.12'
41 | compile 'com.android.support:appcompat-v7:23.1.0'
42 | compile 'com.android.support:design:23.1.0'
43 | compile project(':hotfixlib')
44 | }
45 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/baidu/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/dodola/hotfix/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package dodola.hotfix;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
7 |
8 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/assets/hackdex_dex.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/assets/hackdex_dex.jar
--------------------------------------------------------------------------------
/app/src/main/assets/path_dex.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/assets/path_dex.jar
--------------------------------------------------------------------------------
/app/src/main/java/dodola/hotfix/BugClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfix;
5 |
6 |
7 | /**
8 | * Created by sunpengfei on 15/11/3.
9 | */
10 | public class BugClass {
11 |
12 | public String bug() {
13 | return "bug class";
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/hotfix/HotfixApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfix;
5 |
6 | import java.io.File;
7 |
8 | import android.app.Application;
9 | import android.content.Context;
10 | import dodola.hotfixlib.HotFix;
11 |
12 | /**
13 | * Created by sunpengfei on 15/11/4.
14 | */
15 | public class HotfixApplication extends Application {
16 |
17 | @Override
18 | public void onCreate() {
19 | super.onCreate();
20 | File dexPath = new File(getDir("dex", Context.MODE_PRIVATE), "hackdex_dex.jar");
21 | Utils.prepareDex(this.getApplicationContext(), dexPath, "hackdex_dex.jar");
22 | HotFix.patch(this, dexPath.getAbsolutePath(), "dodola.hackdex.AntilazyLoad");
23 | try {
24 | this.getClassLoader().loadClass("dodola.hackdex.AntilazyLoad");
25 | } catch (ClassNotFoundException e) {
26 | e.printStackTrace();
27 | }
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/hotfix/LoadBugClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfix;
5 |
6 | /**
7 | * Created by sunpengfei on 15/11/4.
8 | */
9 | public class LoadBugClass {
10 | public String getBugString() {
11 | BugClass bugClass = new BugClass();
12 | return bugClass.bug();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/hotfix/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfix;
5 |
6 | import java.io.File;
7 |
8 | import android.content.Context;
9 | import android.os.Bundle;
10 | import android.support.design.widget.FloatingActionButton;
11 | import android.support.design.widget.Snackbar;
12 | import android.support.v7.app.AppCompatActivity;
13 | import android.support.v7.widget.Toolbar;
14 | import android.view.View;
15 | import android.view.Menu;
16 | import android.view.MenuItem;
17 | import android.widget.Toast;
18 | import dodola.hotfixlib.HotFix;
19 |
20 | public class MainActivity extends AppCompatActivity {
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_main);
26 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
27 | setSupportActionBar(toolbar);
28 |
29 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
30 | fab.setOnClickListener(new View.OnClickListener() {
31 | @Override
32 | public void onClick(View view) {
33 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
34 | .setAction("Action", null).show();
35 | }
36 | });
37 | }
38 |
39 | @Override
40 | protected void onResume() {
41 | super.onResume();
42 | }
43 |
44 | @Override
45 | public boolean onCreateOptionsMenu(Menu menu) {
46 | // Inflate the menu; this adds items to the action bar if it is present.
47 | getMenuInflater().inflate(R.menu.menu_main, menu);
48 | return true;
49 | }
50 |
51 | @Override
52 | public boolean onOptionsItemSelected(MenuItem item) {
53 | // Handle action bar item clicks here. The action bar will
54 | // automatically handle clicks on the Home/Up button, so long
55 | // as you specify a parent activity in AndroidManifest.xml.
56 |
57 | switch (item.getItemId()) {
58 | case R.id.action_fix: {
59 | //准备补丁,从assert里拷贝到dex里
60 | File dexPath = new File(getDir("dex", Context.MODE_PRIVATE), "path_dex.jar");
61 | Utils.prepareDex(this.getApplicationContext(), dexPath, "path_dex.jar");
62 | // DexInjector.inject(dexPath.getAbsolutePath(), defaultDexOptPath, "dodola.hotfix
63 | // .BugClass");
64 |
65 | HotFix.patch(this, dexPath.getAbsolutePath(), "dodola.hotfix.BugClass");
66 |
67 | }
68 |
69 | break;
70 | case R.id.action_test:
71 | LoadBugClass bugClass = new LoadBugClass();
72 | Toast.makeText(this, "测试调用方法:" + bugClass.getBugString(), Toast.LENGTH_SHORT).show();
73 | break;
74 | }
75 |
76 | return super.onOptionsItemSelected(item);
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/app/src/main/java/dodola/hotfix/Utils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfix;
5 |
6 | import java.io.BufferedInputStream;
7 | import java.io.BufferedOutputStream;
8 | import java.io.File;
9 | import java.io.FileOutputStream;
10 | import java.io.IOException;
11 | import java.io.OutputStream;
12 |
13 | import android.content.Context;
14 |
15 | /**
16 | * Created by sunpengfei on 15/11/4.
17 | */
18 | public class Utils {
19 | private static final int BUF_SIZE = 2048;
20 |
21 | public static boolean prepareDex(Context context, File dexInternalStoragePath, String dex_file) {
22 | BufferedInputStream bis = null;
23 | OutputStream dexWriter = null;
24 |
25 | try {
26 | bis = new BufferedInputStream(context.getAssets().open(dex_file));
27 | dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath));
28 | byte[] buf = new byte[BUF_SIZE];
29 | int len;
30 | while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
31 | dexWriter.write(buf, 0, len);
32 | }
33 | dexWriter.close();
34 | bis.close();
35 | return true;
36 | } catch (IOException e) {
37 | if (dexWriter != null) {
38 | try {
39 | dexWriter.close();
40 | } catch (IOException ioe) {
41 | ioe.printStackTrace();
42 | }
43 | }
44 | if (bis != null) {
45 | try {
46 | bis.close();
47 | } catch (IOException ioe) {
48 | ioe.printStackTrace();
49 | }
50 | }
51 | return false;
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
13 |
14 |
18 |
19 |
25 |
26 |
27 |
28 |
29 |
30 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
18 |
19 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
4 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
4 | >
5 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
8 | 64dp
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | #3F51B5
7 | #303F9F
8 | #FF4081
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | 16dp
7 | 16dp
8 | 16dp
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 | HotFix
6 | 打补丁
7 | 测试
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
14 |
18 |
20 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/test/java/dodola/hotfix/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package dodola.hotfix;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/buildSrc/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | apply plugin: 'groovy'
3 |
4 | repositories {
5 | mavenCentral()
6 | }
7 |
8 | dependencies {
9 | compile gradleApi()
10 | compile localGroovy()
11 | compile 'org.javassist:javassist:3.18.+'
12 | }
13 |
--------------------------------------------------------------------------------
/buildSrc/buildSrc.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
--------------------------------------------------------------------------------
/buildSrc/src/main/groovy/dodola/patch/PatchClass.groovy:
--------------------------------------------------------------------------------
1 | package dodola.patch;
2 |
3 | import javassist.ClassPool
4 | import javassist.CtClass
5 | import javassist.CtConstructor
6 | import javassist.CtMethod
7 | import javassist.CtNewConstructor
8 | import javassist.CtNewMethod
9 |
10 | public class PatchClass {
11 | /**
12 | * 植入代码
13 | * @param buildDir 是项目的build class目录,就是我们需要注入的class所在地
14 | * @param lib 这个是hackdex的目录,就是AntilazyLoad类的class文件所在地
15 | */
16 | public static void process(String buildDir, String lib) {
17 |
18 | println(lib)
19 | ClassPool classes = ClassPool.getDefault()
20 | classes.appendClassPath(buildDir)
21 | classes.appendClassPath(lib)
22 |
23 | //下面的操作比较容易理解,在将需要关联的类的构造方法中插入引用代码
24 | CtClass c = classes.getCtClass("dodola.hotfix.BugClass")
25 | if (c.isFrozen()) {
26 | c.defrost()
27 | }
28 | println("====添加构造方法====")
29 | def constructor = c.getConstructors()[0];
30 | constructor.insertBefore("System.out.println(dodola.hackdex.AntilazyLoad.class);")
31 | c.writeFile(buildDir)
32 |
33 |
34 |
35 | CtClass c1 = classes.getCtClass("dodola.hotfix.LoadBugClass")
36 | if (c1.isFrozen()) {
37 | c1.defrost()
38 | }
39 | println("====添加构造方法====")
40 | def constructor1 = c1.getConstructors()[0];
41 | constructor1.insertBefore("System.out.println(dodola.hackdex.AntilazyLoad.class);")
42 | c1.writeFile(buildDir)
43 |
44 |
45 | }
46 |
47 | static void growl(String title, String message) {
48 | def proc = ["osascript", "-e", "display notification \"${message}\" with title \"${title}\""].execute()
49 | if (proc.waitFor() != 0) {
50 | println "[WARNING] ${proc.err.text.trim()}"
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Nov 03 16:49:32 CST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=http\://android-mirror.bugly.qq.com:8080/gradle/gradle-2.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/hackdex/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/hackdex/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | apply plugin: 'com.android.library'
5 |
6 | android {
7 | compileSdkVersion 23
8 | buildToolsVersion "23.0.1"
9 |
10 | defaultConfig {
11 | minSdkVersion 15
12 | targetSdkVersion 23
13 | versionCode 1
14 | versionName "1.0"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 |
23 | }
24 |
25 | task makeJar(type: Copy) {
26 | copy{
27 | delete 'build/libs/hackdex.jar'
28 | from('build/intermediates/bundles/debug/')
29 | into('build/libs/')
30 | include('classes.jar')
31 | rename('classes.jar', 'hackdex.jar')
32 |
33 | }
34 | }
35 | makeJar.dependsOn(build)
36 | build.doLast{
37 | makeJar
38 | }
39 | dependencies {
40 | compile fileTree(dir: 'libs', include: ['*.jar'])
41 | }
42 |
--------------------------------------------------------------------------------
/hackdex/hackdex.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/hackdex/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/baidu/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/hackdex/src/androidTest/java/dodola/hackdex/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hackdex;
5 |
6 | import android.app.Application;
7 | import android.test.ApplicationTestCase;
8 |
9 | /**
10 | * Testing Fundamentals
11 | */
12 | public class ApplicationTest extends ApplicationTestCase {
13 | public ApplicationTest() {
14 | super(Application.class);
15 | }
16 | }
--------------------------------------------------------------------------------
/hackdex/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
6 |
7 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/hackdex/src/main/java/dodola/hackdex/AntilazyLoad.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hackdex;
5 |
6 | /**
7 | * Created by sunpengfei on 15/11/3.
8 | */
9 | public class AntilazyLoad {
10 | }
11 |
--------------------------------------------------------------------------------
/hackdex/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 | HackDex
6 |
7 |
--------------------------------------------------------------------------------
/hackdex/src/test/java/dodola/hackdex/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hackdex;
5 |
6 | import org.junit.Test;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | /**
11 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
12 | */
13 | public class ExampleUnitTest {
14 | @Test
15 | public void addition_isCorrect() throws Exception {
16 | assertEquals(4, 2 + 2);
17 | }
18 | }
--------------------------------------------------------------------------------
/hotfixlib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/hotfixlib/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | apply plugin: 'com.android.library'
5 |
6 | android {
7 | compileSdkVersion 23
8 | buildToolsVersion "23.0.1"
9 |
10 | defaultConfig {
11 | minSdkVersion 15
12 | targetSdkVersion 23
13 | versionCode 1
14 | versionName "1.0"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | testCompile 'junit:junit:4.12'
27 | compile 'com.android.support:appcompat-v7:23.1.0'
28 | }
29 |
--------------------------------------------------------------------------------
/hotfixlib/hotfixlib.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/hotfixlib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/baidu/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/hotfixlib/src/androidTest/java/dodola/hotfixlib/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfixlib;
5 |
6 | import android.app.Application;
7 | import android.test.ApplicationTestCase;
8 |
9 | /**
10 | * Testing Fundamentals
11 | */
12 | public class ApplicationTest extends ApplicationTestCase {
13 | public ApplicationTest() {
14 | super(Application.class);
15 | }
16 | }
--------------------------------------------------------------------------------
/hotfixlib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
6 |
7 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/hotfixlib/src/main/java/dodola/hotfixlib/HotFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfixlib;
5 |
6 | import java.io.File;
7 | import java.lang.reflect.Array;
8 | import java.lang.reflect.Field;
9 | import java.lang.reflect.InvocationTargetException;
10 |
11 | import android.annotation.TargetApi;
12 | import android.content.Context;
13 | import dalvik.system.DexClassLoader;
14 | import dalvik.system.PathClassLoader;
15 |
16 | /* compiled from: ProGuard */
17 | public final class HotFix {
18 | public static void patch(Context context, String patchDexFile, String patchClassName) {
19 | if (patchDexFile != null && new File(patchDexFile).exists()) {
20 | try {
21 | if (hasLexClassLoader()) {
22 | injectInAliyunOs(context, patchDexFile, patchClassName);
23 | } else if (hasDexClassLoader()) {
24 | injectAboveEqualApiLevel14(context, patchDexFile, patchClassName);
25 | } else {
26 |
27 | injectBelowApiLevel14(context, patchDexFile, patchClassName);
28 |
29 | }
30 | } catch (Throwable th) {
31 | }
32 | }
33 | }
34 |
35 | private static boolean hasLexClassLoader() {
36 | try {
37 | Class.forName("dalvik.system.LexClassLoader");
38 | return true;
39 | } catch (ClassNotFoundException e) {
40 | return false;
41 | }
42 | }
43 |
44 | private static boolean hasDexClassLoader() {
45 | try {
46 | Class.forName("dalvik.system.BaseDexClassLoader");
47 | return true;
48 | } catch (ClassNotFoundException e) {
49 | return false;
50 | }
51 | }
52 |
53 | private static void injectInAliyunOs(Context context, String patchDexFile, String patchClassName)
54 | throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException,
55 | InstantiationException, NoSuchFieldException {
56 | PathClassLoader obj = (PathClassLoader) context.getClassLoader();
57 | String replaceAll = new File(patchDexFile).getName().replaceAll("\\.[a-zA-Z0-9]+", ".lex");
58 | Class cls = Class.forName("dalvik.system.LexClassLoader");
59 | Object newInstance =
60 | cls.getConstructor(new Class[] {String.class, String.class, String.class, ClassLoader.class}).newInstance(
61 | new Object[] {context.getDir("dex", 0).getAbsolutePath() + File.separator + replaceAll,
62 | context.getDir("dex", 0).getAbsolutePath(), patchDexFile, obj});
63 | cls.getMethod("loadClass", new Class[] {String.class}).invoke(newInstance, new Object[] {patchClassName});
64 | setField(obj, PathClassLoader.class, "mPaths",
65 | appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(newInstance, cls, "mRawDexPath")));
66 | setField(obj, PathClassLoader.class, "mFiles",
67 | combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(newInstance, cls, "mFiles")));
68 | setField(obj, PathClassLoader.class, "mZips",
69 | combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(newInstance, cls, "mZips")));
70 | setField(obj, PathClassLoader.class, "mLexs",
71 | combineArray(getField(obj, PathClassLoader.class, "mLexs"), getField(newInstance, cls, "mDexs")));
72 | }
73 |
74 | @TargetApi(14)
75 | private static void injectBelowApiLevel14(Context context, String str, String str2)
76 | throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
77 | PathClassLoader obj = (PathClassLoader) context.getClassLoader();
78 | DexClassLoader dexClassLoader =
79 | new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader());
80 | dexClassLoader.loadClass(str2);
81 | setField(obj, PathClassLoader.class, "mPaths",
82 | appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(dexClassLoader, DexClassLoader.class,
83 | "mRawDexPath")
84 | ));
85 | setField(obj, PathClassLoader.class, "mFiles",
86 | combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(dexClassLoader, DexClassLoader.class,
87 | "mFiles")
88 | ));
89 | setField(obj, PathClassLoader.class, "mZips",
90 | combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(dexClassLoader, DexClassLoader.class,
91 | "mZips")));
92 | setField(obj, PathClassLoader.class, "mDexs",
93 | combineArray(getField(obj, PathClassLoader.class, "mDexs"), getField(dexClassLoader, DexClassLoader.class,
94 | "mDexs")));
95 | obj.loadClass(str2);
96 | }
97 |
98 | private static void injectAboveEqualApiLevel14(Context context, String str, String str2)
99 | throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
100 | PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
101 | Object a = combineArray(getDexElements(getPathList(pathClassLoader)),
102 | getDexElements(getPathList(
103 | new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader()))));
104 | Object a2 = getPathList(pathClassLoader);
105 | setField(a2, a2.getClass(), "dexElements", a);
106 | pathClassLoader.loadClass(str2);
107 | }
108 |
109 | private static Object getPathList(Object obj) throws ClassNotFoundException, NoSuchFieldException,
110 | IllegalAccessException {
111 | return getField(obj, Class.forName("dalvik.system.BaseDexClassLoader"), "pathList");
112 | }
113 |
114 | private static Object getDexElements(Object obj) throws NoSuchFieldException, IllegalAccessException {
115 | return getField(obj, obj.getClass(), "dexElements");
116 | }
117 |
118 | private static Object getField(Object obj, Class cls, String str)
119 | throws NoSuchFieldException, IllegalAccessException {
120 | Field declaredField = cls.getDeclaredField(str);
121 | declaredField.setAccessible(true);
122 | return declaredField.get(obj);
123 | }
124 |
125 | private static void setField(Object obj, Class cls, String str, Object obj2)
126 | throws NoSuchFieldException, IllegalAccessException {
127 | Field declaredField = cls.getDeclaredField(str);
128 | declaredField.setAccessible(true);
129 | declaredField.set(obj, obj2);
130 | }
131 |
132 | private static Object combineArray(Object obj, Object obj2) {
133 | Class componentType = obj2.getClass().getComponentType();
134 | int length = Array.getLength(obj2);
135 | int length2 = Array.getLength(obj) + length;
136 | Object newInstance = Array.newInstance(componentType, length2);
137 | for (int i = 0; i < length2; i++) {
138 | if (i < length) {
139 | Array.set(newInstance, i, Array.get(obj2, i));
140 | } else {
141 | Array.set(newInstance, i, Array.get(obj, i - length));
142 | }
143 | }
144 | return newInstance;
145 | }
146 |
147 | private static Object appendArray(Object obj, Object obj2) {
148 | Class componentType = obj.getClass().getComponentType();
149 | int length = Array.getLength(obj);
150 | Object newInstance = Array.newInstance(componentType, length + 1);
151 | Array.set(newInstance, 0, obj2);
152 | for (int i = 1; i < length + 1; i++) {
153 | Array.set(newInstance, i, Array.get(obj, i - 1));
154 | }
155 | return newInstance;
156 | }
157 | }
--------------------------------------------------------------------------------
/hotfixlib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 | HotFixLib
6 |
7 |
--------------------------------------------------------------------------------
/hotfixlib/src/test/java/dodola/hotfixlib/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
3 | */
4 | package dodola.hotfixlib;
5 |
6 | import org.junit.Test;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | /**
11 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
12 | */
13 | public class ExampleUnitTest {
14 | @Test
15 | public void addition_isCorrect() throws Exception {
16 | assertEquals(4, 2 + 2);
17 | }
18 | }
--------------------------------------------------------------------------------
/img/patch1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/img/patch1.png
--------------------------------------------------------------------------------
/img/patch2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/img/patch2.png
--------------------------------------------------------------------------------
/img/patch3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dodola/HotFix/afee240bec1e27bdd0a614f914d04b114e3af5de/img/patch3.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':hackdex', ':buildSrc', ':hotfixlib'
2 |
--------------------------------------------------------------------------------