├── app
├── proguard-rules.pro
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── dasinwong
│ │ │ │ └── fettlerpro
│ │ │ │ ├── CalculateUtils.java
│ │ │ │ ├── BaseApplication.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── dasinwong
│ │ │ └── fettlerpro
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── dasinwong
│ │ └── fettlerpro
│ │ └── ExampleInstrumentedTest.java
└── build.gradle
├── fettler
├── proguard-rules.pro
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── dasinwong
│ │ │ └── fettler
│ │ │ ├── FixListener.java
│ │ │ ├── Constants.java
│ │ │ ├── ArrayUtils.java
│ │ │ ├── FileUtils.java
│ │ │ ├── ReflectUtils.java
│ │ │ └── Fettler.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── dasinwong
│ │ │ └── fettler
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── dasinwong
│ │ └── fettler
│ │ └── ExampleInstrumentedTest.java
└── build.gradle
├── gradle.properties
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── misc.xml
├── runConfigurations.xml
└── codeStyles
│ └── Project.xml
├── .gitignore
├── README.md
├── gradlew.bat
└── gradlew
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/fettler/proguard-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/fettler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | org.gradle.jvmargs=-Xmx1536m
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':fettler'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FettlerPro
3 |
4 |
--------------------------------------------------------------------------------
/fettler/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Fettler
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/fettler/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DasinWong/Fettler/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/fettler/src/main/java/com/dasinwong/fettler/FixListener.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | public interface FixListener {
4 | void onComplete();
5 | }
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | zipStoreBase=GRADLE_USER_HOME
4 | zipStorePath=wrapper/dists
5 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dasinwong/fettlerpro/CalculateUtils.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettlerpro;
2 |
3 | import android.content.Context;
4 | import android.widget.Toast;
5 |
6 | public class CalculateUtils {
7 |
8 | public void calculate(Context context) {
9 | int a = 666;
10 | int b = 0;
11 | Toast.makeText(context, "result is " + a / b, Toast.LENGTH_SHORT).show();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dasinwong/fettlerpro/BaseApplication.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettlerpro;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | import com.dasinwong.fettler.Fettler;
7 |
8 | public class BaseApplication extends Application {
9 |
10 | @Override
11 | protected void attachBaseContext(Context context) {
12 | super.attachBaseContext(context);
13 | Fettler.init(context);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/fettler/src/test/java/com/dasinwong/fettler/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/dasinwong/fettlerpro/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettlerpro;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/fettler/src/main/java/com/dasinwong/fettler/Constants.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | public class Constants {
4 | public static final String DEX_SUFFIX = ".dex";
5 | public static final String MAIN_DEX_NAME = "classes.dex";
6 | public static final String TEMP_FOLDER = "temp_folder";
7 | public static final String TEMP_UNZIP_FOLDER = "temp_unzip_folder";
8 | public static final String DEX_ELEMENTS = "dexElements";
9 | public static final String PATH_LIST = "pathList";
10 | public static final String BASE_DEX_CLASS_LOADER = "dalvik.system.BaseDexClassLoader";
11 | }
12 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
14 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/dasinwong/fettlerpro/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettlerpro;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.dasinwong.fettlerpro", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/fettler/src/androidTest/java/com/dasinwong/fettler/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.dasinwong.fettler.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/fettler/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 | defaultConfig {
7 | minSdkVersion 21
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 |
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 |
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 |
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 |
28 | implementation 'com.android.support:appcompat-v7:28.0.0'
29 | testImplementation 'junit:junit:4.12'
30 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
32 | }
33 |
--------------------------------------------------------------------------------
/fettler/src/main/java/com/dasinwong/fettler/ArrayUtils.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | import java.lang.reflect.Array;
4 |
5 | public class ArrayUtils {
6 |
7 | /**
8 | * 数组合并
9 | *
10 | * @param patchArray 补丁包数组
11 | * @param oldArray 应用原数组
12 | */
13 | public static Object merge(Object patchArray, Object oldArray) {
14 | int patchLength = Array.getLength(patchArray);
15 | int oldLength = Array.getLength(oldArray);
16 | Object newArray = Array.newInstance(patchArray.getClass().getComponentType(), patchLength + oldLength);
17 | int newLength = Array.getLength(newArray);
18 | for (int i = 0; i < newLength; ++i) {
19 | if (i < patchLength) {
20 | Array.set(newArray, i, Array.get(patchArray, i));
21 | } else {
22 | Array.set(newArray, i, Array.get(oldArray, i - patchLength));
23 | }
24 | }
25 | return newArray;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 | defaultConfig {
7 | applicationId "com.dasinwong.fettlerpro"
8 | minSdkVersion 21
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 |
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(include: ['*.jar'], dir: 'libs')
25 | implementation 'com.android.support:appcompat-v7:28.0.0'
26 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
27 | testImplementation 'junit:junit:4.12'
28 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
29 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
30 | implementation project(':fettler')
31 | }
32 |
--------------------------------------------------------------------------------
/fettler/src/main/java/com/dasinwong/fettler/FileUtils.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | import java.io.BufferedInputStream;
4 | import java.io.BufferedOutputStream;
5 | import java.io.File;
6 | import java.io.FileInputStream;
7 | import java.io.FileNotFoundException;
8 | import java.io.FileOutputStream;
9 | import java.io.IOException;
10 |
11 | public class FileUtils {
12 |
13 | /**
14 | * 拷贝文件
15 | *
16 | * @param sourceFile 源文件
17 | * @param targetFile 新文件
18 | */
19 | public static void copy(File sourceFile, File targetFile) {
20 | try {
21 | FileInputStream fis = new FileInputStream(sourceFile);
22 | BufferedInputStream bis = new BufferedInputStream(fis);
23 | FileOutputStream fos = new FileOutputStream(targetFile);
24 | BufferedOutputStream bos = new BufferedOutputStream(fos);
25 | byte[] b = new byte[1024 * 4];
26 | int len;
27 | while ((len = bis.read(b)) != -1) {
28 | bos.write(b, 0, len);
29 | }
30 | bos.flush();
31 | bis.close();
32 | bos.close();
33 | fos.close();
34 | fis.close();
35 | } catch (FileNotFoundException e) {
36 | e.printStackTrace();
37 | } catch (IOException e) {
38 | e.printStackTrace();
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 | *.aab
5 |
6 | # Files for the ART/Dalvik VM
7 | *.dex
8 |
9 | # Java class files
10 | *.class
11 |
12 | # Generated files
13 | bin/
14 | gen/
15 | out/
16 |
17 | # Gradle files
18 | .gradle/
19 | build/
20 |
21 | # Local configuration file (sdk path, etc)
22 | local.properties
23 |
24 | # Proguard folder generated by Eclipse
25 | proguard/
26 |
27 | # Log Files
28 | *.log
29 |
30 | # Android Studio Navigation editor temp files
31 | .navigation/
32 |
33 | # Android Studio captures folder
34 | captures/
35 |
36 | # IntelliJ
37 | *.iml
38 | .idea/workspace.xml
39 | .idea/tasks.xml
40 | .idea/gradle.xml
41 | .idea/assetWizardSettings.xml
42 | .idea/dictionaries
43 | .idea/libraries
44 | .idea/caches
45 | # Android Studio 3 in .gitignore file.
46 | .idea/caches/build_file_checksums.ser
47 | .idea/modules.xml
48 |
49 | # Keystore files
50 | # Uncomment the following lines if you do not want to check your keystore files in.
51 | #*.jks
52 | #*.keystore
53 |
54 | # External native build folder generated in Android Studio 2.2 and later
55 | .externalNativeBuild
56 |
57 | # Google Services (e.g. APIs or Firebase)
58 | # google-services.json
59 |
60 | # Freeline
61 | freeline.py
62 | freeline/
63 | freeline_project_description.json
64 |
65 | # fastlane
66 | fastlane/report.xml
67 | fastlane/Preview.html
68 | fastlane/screenshots
69 | fastlane/test_output
70 | fastlane/readme.md
71 |
72 | # Version control
73 | vcs.xml
74 |
75 | # lint
76 | lint/intermediates/
77 | lint/generated/
78 | lint/outputs/
79 | lint/tmp/
80 | # lint/reports/
81 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dasinwong/fettlerpro/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettlerpro;
2 |
3 | import android.Manifest;
4 | import android.os.Bundle;
5 | import android.os.Environment;
6 | import android.support.v4.app.ActivityCompat;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.Toast;
10 |
11 | import com.dasinwong.fettler.Fettler;
12 | import com.dasinwong.fettler.FixListener;
13 |
14 | import java.io.File;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 | initPermission();
23 | }
24 |
25 | private void initPermission() {
26 | ActivityCompat.requestPermissions(this,
27 | new String[]{
28 | Manifest.permission.READ_EXTERNAL_STORAGE,
29 | Manifest.permission.WRITE_EXTERNAL_STORAGE
30 | }, 1);
31 | }
32 |
33 | public void calculate(View view) {
34 | new CalculateUtils().calculate(this);
35 | }
36 |
37 | public void hotfix(View view) {
38 | File dexFile = new File(Environment.getExternalStorageDirectory(), "CalculateUtils.dex");
39 | Fettler.with(this).add(dexFile).listen(new FixListener() {
40 | @Override
41 | public void onComplete() {
42 | Toast.makeText(MainActivity.this, "修复完成", Toast.LENGTH_SHORT).show();
43 | }
44 | }).start();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Fettler
2 | Android 热修复框架 (基于类加载机制的代码修复)
3 | - 支持 Android 5.0 以上设备
4 | - 运行时修复,应用无需重启
5 | - 版本更新时要注意dex修复包的清理
6 | ## 1.如何接入
7 | Project层级下的build.gradle文件
8 | ```
9 | allprojects {
10 | repositories {
11 | ...
12 | maven { url 'https://jitpack.io' }
13 | }
14 | }
15 | ```
16 | Module层级下的build.gradle文件
17 | ```
18 | dependencies {
19 | ...
20 | implementation 'com.github.dasinwong:Fettler:1.0'
21 | }
22 | ```
23 | ## 2.类及其方法介绍
24 | ### Fettler
25 | 热修复核心类
26 |
27 | | 方法 | 描述 |
28 | | :-------------: | :-------------: |
29 | | init | 初始化,程序启动时调用 |
30 | | with | 创建Fettler对象的静态方法 |
31 | | add | 添加dex补丁文件(可连续添加多个) |
32 | | listen | 添加修复监听(选用) |
33 | | start | 开始修复 |
34 | | clear | 清理dex补丁文件(版本更新时) |
35 | ### FixListener
36 | 修复监听接口
37 |
38 | | 方法 | 描述 |
39 | | :-------------: | :-------------: |
40 | | onComplete | 修复完成时回调 |
41 | ## 3.使用方法
42 | #### 3.1 程序启动时进行初始化
43 | ```
44 | public class BaseApplication extends Application {
45 |
46 | @Override
47 | protected void attachBaseContext(Context context) {
48 | super.attachBaseContext(context);
49 | //初始化Fettler
50 | Fettler.init(context);
51 | }
52 | }
53 | ```
54 | #### 3.2 执行修复
55 | ```
56 | //获取补丁包文件(实际环境中通过网络下载到本地)
57 | File dexFile = new File(Environment.getExternalStorageDirectory(), "XXX.dex");
58 |
59 | //添加dex文件和监听,开始修复
60 | Fettler.with(this).add(dexFile).listen(new FixListener() {
61 | @Override
62 | public void onComplete() {
63 | Toast.makeText(XXX.this, "修复完成", Toast.LENGTH_SHORT).show();
64 | }
65 | }).start();
66 | ```
67 | #### 3.3 清理修复包
68 | 在版本更新时清理
69 | ```
70 | //直接调用静态方法清理
71 | Fettler.clear(this);
72 |
73 | //创建Fettler对象清理
74 | Fettler.with(this).clear();
75 | ```
76 | ## 4.需要权限
77 | ```
78 |
79 |
80 | ```
81 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/fettler/src/main/java/com/dasinwong/fettler/ReflectUtils.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | public class ReflectUtils {
6 |
7 | /**
8 | * 获取某个属性对象
9 | *
10 | * @param obj 该属性所属类的对象
11 | * @param clazz 该属性的所属类
12 | * @param field 属性名
13 | */
14 | private static Object getField(Object obj, Class> clazz, String field) {
15 | try {
16 | Field declaredField = clazz.getDeclaredField(field);
17 | declaredField.setAccessible(true);
18 | return declaredField.get(obj);
19 | } catch (NoSuchFieldException e) {
20 | e.printStackTrace();
21 | } catch (IllegalAccessException e) {
22 | e.printStackTrace();
23 | }
24 | return null;
25 | }
26 |
27 | /**
28 | * 给某个属性赋值
29 | *
30 | * @param obj 该属性所属类的对象
31 | * @param clazz 该属性的所属类
32 | * @param field 属性名
33 | * @param value 值
34 | */
35 | public static void setField(Object obj, Class> clazz, String field, Object value) {
36 | try {
37 | Field declaredField = clazz.getDeclaredField(field);
38 | declaredField.setAccessible(true);
39 | declaredField.set(obj, value);
40 | } catch (NoSuchFieldException e) {
41 | e.printStackTrace();
42 | } catch (IllegalAccessException e) {
43 | e.printStackTrace();
44 | }
45 | }
46 |
47 | /**
48 | * 获取BaseDexClassLoader对象中的pathList对象
49 | *
50 | * @param baseDexClassLoader baseDexClassLoader对象
51 | */
52 | public static Object getPathList(Object baseDexClassLoader) {
53 | try {
54 | return getField(baseDexClassLoader, Class.forName(Constants.BASE_DEX_CLASS_LOADER), Constants.PATH_LIST);
55 | } catch (ClassNotFoundException e) {
56 | e.printStackTrace();
57 | }
58 | return null;
59 | }
60 |
61 | /**
62 | * 获取PathList对象中的dexElements对象
63 | *
64 | * @param pathList pathList对象
65 | */
66 | public static Object getDexElements(Object pathList) {
67 | return getField(pathList, pathList.getClass(), Constants.DEX_ELEMENTS);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
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 Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/fettler/src/main/java/com/dasinwong/fettler/Fettler.java:
--------------------------------------------------------------------------------
1 | package com.dasinwong.fettler;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import java.io.File;
7 | import java.util.HashSet;
8 |
9 | import dalvik.system.DexClassLoader;
10 |
11 | public class Fettler {
12 |
13 | private static final String TAG = "Fettler";
14 | private HashSet fixDexSet;
15 | private Context context;
16 | private FixListener listener;
17 |
18 | private Fettler(Context context) {
19 | fixDexSet = new HashSet<>();
20 | this.context = context;
21 | }
22 |
23 | /**
24 | * 构造Fettler对象,初始化成员变量
25 | */
26 | public static Fettler with(Context context) {
27 | return new Fettler(context);
28 | }
29 |
30 | /**
31 | * 初始化,在application的attachBaseContext()调用
32 | */
33 | public static void init(Context context) {
34 | with(context).start();
35 | }
36 |
37 | /**
38 | * 清理磁盘缓存的dex文件(慎用,会导致需要修复的dex文件失效)
39 | */
40 | public static void clear(Context context) {
41 | with(context).clear();
42 | }
43 |
44 | /**
45 | * 添加补丁包
46 | */
47 | public Fettler add(String dexPath) {
48 | File dexFile = new File(dexPath);
49 | File targetFile = new File(context.getDir(Constants.TEMP_FOLDER, Context.MODE_PRIVATE) + File.separator + dexFile.getName());
50 | if (targetFile.exists()) targetFile.delete();
51 | FileUtils.copy(dexFile, targetFile);
52 | Log.i(TAG, "===== 成功添加 " + targetFile.getName() + " =====");
53 | return this;
54 | }
55 |
56 | /**
57 | * 添加补丁包
58 | */
59 | public Fettler add(File dexFile) {
60 | File targetFile = new File(context.getDir(Constants.TEMP_FOLDER, Context.MODE_PRIVATE) + File.separator + dexFile.getName());
61 | if (targetFile.exists()) targetFile.delete();
62 | FileUtils.copy(dexFile, targetFile);
63 | Log.i(TAG, "===== 成功添加 " + targetFile.getName() + " =====");
64 | return this;
65 | }
66 |
67 | /**
68 | * 添加监听
69 | */
70 | public Fettler listen(FixListener listener) {
71 | this.listener = listener;
72 | return this;
73 | }
74 |
75 | /**
76 | * 热修复
77 | */
78 | public void start() {
79 | Log.i(TAG, "===== 开始修复 =====");
80 | fixDexSet.clear(); //清理集合
81 | File dexDir = context.getDir(Constants.TEMP_FOLDER, Context.MODE_PRIVATE);
82 | //遍历所有dex文件添加到集合
83 | for (File dex : dexDir.listFiles()) {
84 | String fileName = dex.getName();
85 | //非主dex文件
86 | if (fileName.endsWith(Constants.DEX_SUFFIX) && !fileName.equals(Constants.MAIN_DEX_NAME))
87 | fixDexSet.add(dex);
88 | }
89 | //开始插桩修复
90 | createDexClassLoader(dexDir);
91 | }
92 |
93 | /**
94 | * 创建自有类加载器生成dexElements对象
95 | */
96 | private void createDexClassLoader(File dexDir) {
97 | //创建临时解压目录
98 | File tempDir = new File(dexDir.getAbsolutePath() + File.separator + Constants.TEMP_UNZIP_FOLDER);
99 | if (!tempDir.exists()) tempDir.mkdirs();
100 | //遍历dex集合进行插桩修复
101 | for (File dex : fixDexSet) {
102 | Log.i(TAG, "===== 正在修复 " + dex.getName() + " =====");
103 | DexClassLoader classLoader = new DexClassLoader(dex.getAbsolutePath(), tempDir.getAbsolutePath(), null, context.getClassLoader());
104 | hotFix(classLoader);
105 | }
106 | if (listener != null) listener.onComplete();
107 | Log.i(TAG, "===== 修复完成 =====");
108 | }
109 |
110 | /**
111 | * 插桩修复
112 | */
113 | private void hotFix(DexClassLoader loader) {
114 | //获取自有类加载器中的dexElements对象
115 | Object patchElements = ReflectUtils.getDexElements(ReflectUtils.getPathList(loader));
116 | //获取系统类加载器中的dexElements对象
117 | Object oldElements = ReflectUtils.getDexElements(ReflectUtils.getPathList(context.getClassLoader()));
118 | //合并dexElements数组
119 | Object newElements = ArrayUtils.merge(patchElements, oldElements);
120 | //获取系统类加载器中的pathList对象
121 | Object pathList = ReflectUtils.getPathList(context.getClassLoader());
122 | //将合并后的数组赋值给系统的类加载器pathList对象的dexElements属性
123 | ReflectUtils.setField(pathList, pathList.getClass(), Constants.DEX_ELEMENTS, newElements);
124 | }
125 |
126 | /**
127 | * 清理磁盘缓存的dex文件(慎用,会导致需要修复的dex文件失效)
128 | */
129 | public void clear() {
130 | fixDexSet.clear();
131 | String dexDir = context.getDir(Constants.TEMP_FOLDER, Context.MODE_PRIVATE).getAbsolutePath();
132 | File tempFile = new File(dexDir + File.separator + Constants.TEMP_UNZIP_FOLDER);
133 | for (File dex : tempFile.listFiles()) {
134 | if (dex.exists()) dex.delete();
135 | }
136 | File dexFile = new File(dexDir);
137 | for (File dex : dexFile.listFiles()) {
138 | if (dex.exists()) dex.delete();
139 | }
140 | Log.i(TAG, "===== 清理完成 =====");
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
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 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------