├── .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 | 9 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 | 8 | 9 | 10 | 11 | 12 | 24 | 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 | 67 | 68 | 70 | 71 | 72 | 73 | 74 | Baidu 75 | 76 | 81 | 82 | 83 | 84 | 85 | 86 | 1.8 87 | 88 | 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 | 289 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /HotFix.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /HotFix_Git.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 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 | ![](img/patch1.png) 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 | ![](img/patch2.png) 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 | ![](img/patch3.png) 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 | 8 | 9 | 10 | 11 | 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 | 8 | 12 | 16 | 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 |