├── app
├── .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
│ │ │ │ └── yuong
│ │ │ │ └── loadapk
│ │ │ │ ├── PluginClassLoader.java
│ │ │ │ ├── proxy
│ │ │ │ └── ProxyActivity.java
│ │ │ │ ├── Constans.java
│ │ │ │ ├── MyApplication.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── manager
│ │ │ │ └── HookManager.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── yuong
│ │ │ └── hook
│ │ │ └── loadapk
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── yuong
│ │ └── hook
│ │ └── loadapk
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── plugin
├── .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_plugin2.xml
│ │ │ │ └── activity_plugin.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── yuong
│ │ │ │ └── plugin
│ │ │ │ ├── base
│ │ │ │ ├── BaseActivity.java
│ │ │ │ └── BaseAppCompatActivity.java
│ │ │ │ ├── Plugin2Activity.java
│ │ │ │ └── PluginActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── yuong
│ │ │ └── plugin
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── yuong
│ │ └── plugin
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── README.md
├── pms包名检测.png
├── 插件化总体笔记.png
├── settings.gradle
├── ApplicationInfo.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── render.experimental.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
├── jarRepositories.xml
├── inspectionProfiles
│ └── Project_Default.xml
└── codeStyles
│ └── Project.xml
├── .gitignore
├── gradle.properties
├── LoadApk式插件化笔记.txt
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/plugin/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LoadApkDemo
2 | LoadedApk式插件化
3 | 目前已在7.0和10.0的手机上跑过,正常运行。
4 |
--------------------------------------------------------------------------------
/pms包名检测.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/pms包名检测.png
--------------------------------------------------------------------------------
/插件化总体笔记.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/插件化总体笔记.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':plugin'
2 | rootProject.name='LoadApkDemo'
3 |
--------------------------------------------------------------------------------
/ApplicationInfo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/ApplicationInfo.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LoadApkDemo
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/plugin/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Plugin
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/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/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/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/Yuongzw/LoadApkDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yuongzw/LoadApkDemo/HEAD/plugin/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/render.experimental.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/plugin/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jun 03 08:39:57 CST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/plugin/src/main/java/com/yuong/plugin/base/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.yuong.plugin.base;
2 |
3 | import android.app.Activity;
4 |
5 | /**
6 | * @author : zhiwen.yang
7 | * date : 2020/6/5
8 | * desc :
9 | */
10 | public abstract class BaseActivity extends Activity { }
11 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/plugin/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 |
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/plugin/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/plugin/src/test/java/com/yuong/plugin/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yuong.plugin;
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/main/java/com/yuong/loadapk/PluginClassLoader.java:
--------------------------------------------------------------------------------
1 | package com.yuong.loadapk;
2 |
3 | import dalvik.system.DexClassLoader;
4 |
5 | /**
6 | * @author :
7 | * date : 2020/6/3
8 | * desc : 专门加载插件里面的class
9 | */
10 | public class PluginClassLoader extends DexClassLoader {
11 |
12 | public PluginClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent) {
13 | super(dexPath, optimizedDirectory, librarySearchPath, parent);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/test/java/com/yuong/hook/loadapk/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yuong.hook.loadapk;
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/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuong/loadapk/proxy/ProxyActivity.java:
--------------------------------------------------------------------------------
1 | package com.yuong.loadapk.proxy;
2 |
3 | import android.os.Bundle;
4 | import android.widget.Toast;
5 |
6 | import androidx.annotation.Nullable;
7 | import androidx.appcompat.app.AppCompatActivity;
8 |
9 | /**
10 | * @author :
11 | * date : 2020/6/1
12 | * desc :
13 | */
14 | public class ProxyActivity extends AppCompatActivity {
15 |
16 | @Override
17 | protected void onCreate(@Nullable Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | Toast.makeText(this, "我是代理的Activity", Toast.LENGTH_SHORT).show();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/plugin/src/main/res/layout/activity_plugin2.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuong/loadapk/Constans.java:
--------------------------------------------------------------------------------
1 | package com.yuong.loadapk;
2 |
3 | /**
4 | * @author :
5 | * date : 2020/6/1
6 | * desc :
7 | */
8 | public class Constans {
9 | public static final String ACTIVITY_MANAGER_10 = "android.app.IActivityTaskManager";
10 | public static final String ACTIVITY_MANAGER_9_DOWN = "android.app.IActivityManager";
11 | public static final String ACTIVITY_MANAGER_8_UP_SINGLETON = "IActivityTaskManagerSingleton";
12 | public static final String ACTIVITY_MANAGER_8_DOWN_SINGLETON = "gDefault";
13 | public static final String SINGLETON = "android.util.Singleton";
14 | public static final int EXECUTE_TRANSACTION = 159;
15 | public static final int LAUNCH_ACTIVITY = 100;
16 | }
17 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/plugin/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/plugin/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/plugin/src/androidTest/java/com/yuong/plugin/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.yuong.plugin;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.yuong.plugin", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/yuong/hook/loadapk/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.yuong.hook.loadapk;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.yuong.hook.loadapk", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
26 |
27 |
--------------------------------------------------------------------------------
/plugin/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.2"
6 |
7 |
8 | defaultConfig {
9 | applicationId "com.yuong.plugin"
10 | minSdkVersion 22
11 | targetSdkVersion 29
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 |
30 | implementation 'androidx.appcompat:appcompat:1.1.0'
31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.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 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.2"
6 | defaultConfig {
7 | applicationId "com.yuong.loadapk"
8 | minSdkVersion 21
9 | targetSdkVersion 29
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'androidx.appcompat:appcompat:1.1.0'
25 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'androidx.test.ext:junit:1.1.0'
28 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
29 | //android 9.0以上的@hide API要通过反射的方式来绕过系统检测
30 | implementation 'me.weishu:free_reflection:2.2.0'
31 | }
32 |
--------------------------------------------------------------------------------
/plugin/src/main/res/layout/activity_plugin.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
20 |
21 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/plugin/src/main/java/com/yuong/plugin/Plugin2Activity.java:
--------------------------------------------------------------------------------
1 | package com.yuong.plugin;
2 |
3 | import android.os.Bundle;
4 | import android.util.Log;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import com.yuong.plugin.base.BaseAppCompatActivity;
8 |
9 | public class Plugin2Activity extends BaseAppCompatActivity {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 | setTheme(android.R.style.Theme_Light_NoTitleBar);
15 | Log.d("yuongzw", "Plugin2Activity onCreate");
16 | View view = LayoutInflater.from(mContext).inflate(R.layout.activity_plugin2, null);
17 | setContentView(view);
18 | }
19 |
20 | @Override
21 | protected void onResume() {
22 | super.onResume();
23 | Log.d("yuongzw", "Plugin2Activity onResume");
24 | }
25 |
26 | @Override
27 | protected void onPause() {
28 | super.onPause();
29 | Log.d("yuongzw", "Plugin2Activity onPause");
30 | }
31 |
32 | @Override
33 | protected void onStop() {
34 | super.onStop();
35 | Log.d("yuongzw", "Plugin2Activity onStop");
36 | }
37 |
38 | @Override
39 | protected void onDestroy() {
40 | super.onDestroy();
41 | Log.d("yuongzw", "Plugin2Activity onDestroy");
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuong/loadapk/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.yuong.loadapk;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 | import com.yuong.loadapk.manager.HookManager;
6 | import me.weishu.reflection.Reflection;
7 |
8 | /**
9 | * @author :
10 | * date : 2020/6/1
11 | * desc :
12 | */
13 | public class MyApplication extends Application {
14 |
15 | public static boolean isHookFinish = false;
16 |
17 | @Override
18 | protected void attachBaseContext(Context base) {
19 | super.attachBaseContext(base);
20 | Reflection.unseal(base);
21 | }
22 |
23 | @Override
24 | public void onCreate() {
25 | super.onCreate();
26 | new Thread(new Runnable() {
27 | @Override
28 | public void run() {
29 | try {
30 | //hook LoadApk
31 | HookManager.getInstance(MyApplication.this).customLoadApkAction();
32 | //先hook AMS检查
33 | HookManager.getInstance(MyApplication.this).hookAMSAction();
34 | //hook ActivityThread
35 | HookManager.getInstance(MyApplication.this).hookLaunchActivity();
36 | isHookFinish = true;
37 | } catch (Exception e) {
38 | e.printStackTrace();
39 | }
40 | }
41 | }).start();
42 |
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/plugin/src/main/java/com/yuong/plugin/PluginActivity.java:
--------------------------------------------------------------------------------
1 | package com.yuong.plugin;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 | import android.view.View;
7 |
8 | import com.yuong.plugin.base.BaseActivity;
9 |
10 | public class PluginActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_plugin);
16 | Log.d("yuongzw", "PluginActivity onCreate");
17 | findViewById(R.id.btn_jump).setOnClickListener(new View.OnClickListener() {
18 | @Override
19 | public void onClick(View v) {
20 | startActivity(new Intent(PluginActivity.this, Plugin2Activity.class));
21 | }
22 | });
23 | }
24 |
25 | @Override
26 | protected void onResume() {
27 | super.onResume();
28 | Log.d("yuongzw", "PluginActivity onResume");
29 | }
30 |
31 | @Override
32 | protected void onPause() {
33 | super.onPause();
34 | Log.d("yuongzw", "PluginActivity onPause");
35 | }
36 |
37 | @Override
38 | protected void onStop() {
39 | super.onStop();
40 | Log.d("yuongzw", "PluginActivity onStop");
41 | }
42 |
43 | @Override
44 | protected void onDestroy() {
45 | super.onDestroy();
46 | Log.d("yuongzw", "PluginActivity onDestroy");
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.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 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/plugin/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/LoadApk式插件化笔记.txt:
--------------------------------------------------------------------------------
1 | 占位式(插桩式)插件化缺点:在插件中必须使用宿主的的环境,太依赖与宿主
2 |
3 | hook式插件化缺点:插件越多,dexElements会越来越大,内存也会随之增大
4 |
5 | LoadAPK式:控制ClassLoader
6 | PathClassLoader ---> 加载宿主的class
7 | 自定义的ClassLoader --> 加载插件的class
8 |
9 |
10 | ActivityThread源码分析:
11 | startActivity() --> Activity.startActivity() --> Activity.startActivityForResult() --> Instrumentation.execStartActivity() --> ActivityTaskManager.getService().startActivity(AMS检查)
12 | ActivityThread.handleLaunchActivity()(自己去处理LoadedApk中的ClassLoader)
13 |
14 |
15 | 拿的是宿主的 LoadedApk
16 | key --> 包名 value --> LoadedApk
17 | public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai, CompatibilityInfo compatInfo)
18 |
19 | 缓存中的
20 | @UnsupportedAppUsage
21 | final ArrayMap> mPackages = new ArrayMap<>(); 默认保存的是宿主的 LoadedApk
22 |
23 | LoadedApk ---> 宿主的 ---> LoadedApk.mClassLoader ---> 加载宿主中的class
24 |
25 | 自定义一个 LoadedApk 然后保存到 mPackages 中
26 |
27 | 自定义的 LoadedApk ---> 插件的 ---> 自定义的 LoadedApk.mClassLoader ---> 加载插件中的class
28 |
29 |
30 | 在 performLaunchActivity()里面的 java.lang.ClassLoader cl = appContext.getClassLoader();正是 LoadedApk.mClassLoader
31 | activity = mInstrumentation.newActivity()
32 | return (Activity) cl.loadClass(className).newInstance();
33 |
34 | 结论:宿主中的 LoadedApk.mClassLoader 加载宿主中的class,然后实例化
35 |
36 | 所以我们要自定义 LoadedApk 自定义一个 mClassLoader,用于加载插件里面的class然后实例化
37 |
38 |
39 | //系统是如何创建 LoadedApk 对象的
40 | @Override
41 | @UnsupportedAppUsage
42 | public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
43 | CompatibilityInfo compatInfo) {
44 | return getPackageInfo(ai, compatInfo, null, false, true, false);
45 | }
46 |
47 | 先去从 mPackages 里面拿,如果这里面没有,就新建一个,然后再放到 mPackages里面。
48 |
49 |
50 | 绕过 PMS 的检测处理
51 |
52 | 流程:startActivity --> Activity --> Instrumentation --> AMS 检查 --> ActivityThread --> 获取自定义的 LoadedApk.ClassLoader
53 | --> 实例化 Activity --> initializeJavaContextClassLoader (PMS检查要启动的包名是否安装) --> 处理生命周期方法(真正的启动插件里面的 Activity)
54 |
55 | PMS检查:pi = pm.getPackageInfo(mPackageName, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.myUserId());
56 |
57 | Hook 我们要在 getPackageInfo() 执行之前给 hook 拦截住,控制 pi 不为 null
58 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/plugin/src/main/java/com/yuong/plugin/base/BaseAppCompatActivity.java:
--------------------------------------------------------------------------------
1 | package com.yuong.plugin.base;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.content.res.Resources;
6 | import android.os.Bundle;
7 |
8 | import androidx.annotation.Nullable;
9 | import androidx.appcompat.view.ContextThemeWrapper;
10 |
11 | import java.lang.reflect.Field;
12 |
13 | /**
14 | * @author : zhiwen.yang
15 | * date : 2020/6/5
16 | * desc :
17 | */
18 | public abstract class BaseAppCompatActivity extends Activity {
19 |
20 | protected ContextThemeWrapper mContext;
21 |
22 | @Override
23 | protected void onCreate(@Nullable Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | // Resources resources = PluginManager.getInstance(getApplication()).getResource();
26 | Application application = getApplication();
27 | Resources resources = application.getResources();
28 | mContext = new ContextThemeWrapper(getBaseContext(), 0);
29 | //替換插件的
30 | Class extends ContextThemeWrapper> clazz = mContext.getClass();
31 | try {
32 | Field mResourcesFiled = clazz.getDeclaredField("mResources");
33 | mResourcesFiled.setAccessible(true);
34 | mResourcesFiled.set(mContext, resources);
35 | } catch (Exception e) {
36 | e.printStackTrace();
37 | }
38 | // try {
39 | // Class> superclass = getClass().getSuperclass().getSuperclass();
40 | // Field mParentResourcesFiled = superclass.getDeclaredField("mResources");
41 | // mParentResourcesFiled.setAccessible(true);
42 | // mParentResourcesFiled.set(this, resources);
43 | ////
44 | //// Field mDelegateFiled = superclass.getDeclaredField("mDelegate");
45 | //// mDelegateFiled.setAccessible(true);
46 | //// Object mDelegateImpl = mDelegateFiled.get(this);
47 | //// Field mDelegateContextFiled = mDelegateImpl.getClass().getDeclaredField("mContext");
48 | //// mDelegateContextFiled.setAccessible(true);
49 | //// mDelegateContextFiled.set(mDelegateImpl, mContext);
50 | // } catch (Exception e) {
51 | // e.printStackTrace();
52 | // }
53 | }
54 |
55 | // @Override
56 | // public Resources getResources() {
57 | // if (getApplication() != null && getApplication().getResources() != null) {
58 | // //如果不为空,就说明已经被添加到了宿主当中
59 | //// Application application = getApplication();
60 | //// Resources resources = application.getResources();
61 | // return getApplication().getResources();
62 | // }
63 | // return super.getResources();
64 | // }
65 | //
66 | // @Override
67 | // public AssetManager getAssets() {
68 | // if (getApplication() != null && getApplication().getAssets() != null) {
69 | // //如果不为空,就说明已经被添加到了宿主当中
70 | //// Application application = getApplication();
71 | //// AssetManager assets = application.getAssets();
72 | // return getApplication().getAssets();
73 | // }
74 | // return super.getAssets();
75 | // }
76 | ////
77 | // @Override
78 | // public Resources.Theme getTheme() {
79 | // if (getApplication() != null && getApplication().getTheme() != null) {
80 | // //如果不为空,就说明已经被添加到了宿主当中
81 | //// Application application = getApplication();
82 | //// Resources.Theme theme = application.getTheme();
83 | // return getApplication().getTheme();
84 | // }
85 | // return super.getTheme();
86 | // }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuong/loadapk/MainActivity.java:
--------------------------------------------------------------------------------
1 |
2 | package com.yuong.loadapk;
3 |
4 | import android.Manifest;
5 | import android.content.ComponentName;
6 | import android.content.DialogInterface;
7 | import android.content.Intent;
8 | import android.content.pm.PackageManager;
9 | import android.os.Bundle;
10 | import android.util.Log;
11 | import android.view.View;
12 |
13 | import androidx.appcompat.app.AlertDialog;
14 | import androidx.appcompat.app.AppCompatActivity;
15 | import androidx.core.app.ActivityCompat;
16 | import androidx.core.content.ContextCompat;
17 |
18 | import com.yuong.loadapk.manager.HookManager;
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 | requestPermission();
27 | }
28 |
29 | public void requestPermission() {
30 | if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
31 | ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
32 | ActivityCompat.requestPermissions(this, new String[]{
33 | Manifest.permission.WRITE_EXTERNAL_STORAGE,
34 |
35 | Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
36 | }
37 | }
38 |
39 |
40 | @Override
41 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
42 | //通过requestCode来识别是否同一个请求
43 | if (requestCode == 100) {
44 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
45 | //用户同意,执行操作
46 | new Thread(new Runnable() {
47 | @Override
48 | public void run() {
49 | try {
50 | //先hook AMS检查
51 | HookManager.getInstance(MainActivity.this.getApplication()).hookAMSAction();
52 | //hook ActivityThread
53 | HookManager.getInstance(MainActivity.this.getApplication()).hookLaunchActivity();
54 | //hook LoadApk
55 | HookManager.getInstance(MainActivity.this.getApplication()).customLoadApkAction();
56 | MyApplication.isHookFinish = true;
57 | } catch (Exception e) {
58 | e.printStackTrace();
59 | }
60 | }
61 | }).start();
62 |
63 | } else {
64 | //用户不同意,向用户展示该权限作用
65 | if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
66 | new AlertDialog.Builder(this)
67 | .setMessage("请设置读写SD卡权限")
68 | .setPositiveButton("OK", new DialogInterface.OnClickListener() {
69 | @Override
70 | public void onClick(DialogInterface dialog, int which) {
71 | ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
72 | }
73 | })
74 | .setNegativeButton("Cancel", null)
75 | .create()
76 | .show();
77 | }
78 | }
79 | }
80 | }
81 |
82 | public void jumpPlugin(View view) {
83 | Log.d("yuongzw", "MyApplication.isHookFinish=" + MyApplication.isHookFinish);
84 | if (MyApplication.isHookFinish) {
85 | Intent intent = new Intent();
86 | intent.setComponent(new ComponentName("com.yuong.plugin", "com.yuong.plugin.PluginActivity"));
87 | startActivity(intent);
88 | } else {
89 | Log.d("yuongzw", "没Hook住系统Api,不能启动插件Activity");
90 | }
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/plugin/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 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yuong/loadapk/manager/HookManager.java:
--------------------------------------------------------------------------------
1 | package com.yuong.loadapk.manager;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.app.AppOpsManager;
5 | import android.content.ContentResolver;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.pm.ActivityInfo;
9 | import android.content.pm.ApplicationInfo;
10 | import android.content.pm.PackageInfo;
11 | import android.content.pm.PackageManager;
12 | import android.os.Build;
13 | import android.os.Environment;
14 | import android.os.Handler;
15 | import android.os.Message;
16 | import android.provider.Settings;
17 | import android.util.ArrayMap;
18 | import android.util.Log;
19 | import androidx.annotation.NonNull;
20 | import com.yuong.loadapk.Constans;
21 | import com.yuong.loadapk.PluginClassLoader;
22 | import com.yuong.loadapk.proxy.ProxyActivity;
23 |
24 | import java.io.File;
25 | import java.io.FileNotFoundException;
26 | import java.lang.ref.WeakReference;
27 | import java.lang.reflect.Field;
28 | import java.lang.reflect.InvocationHandler;
29 | import java.lang.reflect.Method;
30 | import java.lang.reflect.Proxy;
31 | import java.util.List;
32 |
33 | /**
34 | * @author :
35 | * date : 2020/6/1
36 | * desc :
37 | */
38 | public class HookManager {
39 | private Context context;
40 | private static HookManager instance;
41 |
42 | private HookManager(Context context) {
43 | this.context = context;
44 | }
45 |
46 | public static HookManager getInstance(Context context) {
47 | if (instance == null) {
48 | synchronized (HookManager.class) {
49 | if (instance == null) {
50 | instance = new HookManager(context);
51 | }
52 | }
53 | }
54 | return instance;
55 | }
56 |
57 |
58 |
59 | /**
60 | * 自定义一个 LoadedApk 再自定义一个ClassLoader 将 LoadedApk 添加到 mPackages,此 LoadedApk 专门用来加载插件的 class
61 | */
62 | public void customLoadApkAction() throws Exception {
63 | File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "plugin2.apk");
64 | if (!file.exists()) {
65 | throw new FileNotFoundException("插件包不存在");
66 | }
67 | //获取 ActivityThread 类
68 | Class> mActivityThreadClass = Class.forName("android.app.ActivityThread");
69 | //获取 ActivityThread 的 currentActivityThread() 方法
70 | Method currentActivityThread = mActivityThreadClass.getDeclaredMethod("currentActivityThread");
71 | currentActivityThread.setAccessible(true);
72 | //获取 ActivityThread 实例
73 | Object mActivityThread = currentActivityThread.invoke(null);
74 |
75 | //final ArrayMap> mPackages = new ArrayMap<>();
76 | //获取 mPackages 属性
77 | Field mPackagesField = mActivityThreadClass.getDeclaredField("mPackages");
78 | mPackagesField.setAccessible(true);
79 | //获取 mPackages 属性的值
80 | ArrayMap mPackages = (ArrayMap) mPackagesField.get(mActivityThread);
81 | // if (mPackages.size() >= 2) {
82 | // return;
83 | // }
84 |
85 | //自定义一个 LoadedApk,系统是如何创建的我们就如何创建
86 | //执行下面的方法会返回一个 LoadedApk,我们就仿照系统执行此方法
87 | /*
88 | this.packageInfo = client.getPackageInfoNoCheck(activityInfo.applicationInfo,
89 | compatInfo);
90 | public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
91 | int flags)
92 | */
93 | Class> mCompatibilityInfoClass = Class.forName("android.content.res.CompatibilityInfo");
94 | Method getLoadedApkMethod = mActivityThreadClass.getDeclaredMethod("getPackageInfoNoCheck",
95 | ApplicationInfo.class, mCompatibilityInfoClass);
96 |
97 | /*
98 | public static final CompatibilityInfo DEFAULT_COMPATIBILITY_INFO = new CompatibilityInfo() {};
99 | */
100 | //以上注释是获取默认的 CompatibilityInfo 实例
101 | Field mCompatibilityInfoDefaultField = mCompatibilityInfoClass.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");
102 | Object mCompatibilityInfo = mCompatibilityInfoDefaultField.get(null);
103 |
104 | //获取一个 ApplicationInfo实例
105 | ApplicationInfo applicationInfo = getAppInfo(file);
106 | // applicationInfo.uid = context.getApplicationInfo().uid;
107 | //执行此方法,获取一个 LoadedApk
108 | Object mLoadedApk = getLoadedApkMethod.invoke(mActivityThread, applicationInfo, mCompatibilityInfo);
109 |
110 | //自定义一个 ClassLoader
111 | String optimizedDirectory = context.getDir("plugin", Context.MODE_PRIVATE).getAbsolutePath();
112 | PluginClassLoader classLoader = new PluginClassLoader(file.getAbsolutePath(), optimizedDirectory,
113 | null, context.getClassLoader());
114 |
115 | //private ClassLoader mClassLoader;
116 | //获取 LoadedApk 的 mClassLoader 属性
117 | Field mClassLoaderField = mLoadedApk.getClass().getDeclaredField("mClassLoader");
118 | mClassLoaderField.setAccessible(true);
119 | //设置自定义的 classLoader 到 mClassLoader 属性中
120 | mClassLoaderField.set(mLoadedApk, classLoader);
121 |
122 | WeakReference loadApkReference = new WeakReference(mLoadedApk);
123 | //添加自定义的 LoadedApk
124 | mPackages.put(applicationInfo.packageName, loadApkReference);
125 | //重新设置 mPackages
126 | mPackagesField.set(mActivityThread, mPackages);
127 | Thread.sleep(2000);
128 | }
129 |
130 | /**
131 | * 获取 ApplicationInfo 实例
132 | *
133 | * @return
134 | */
135 | private ApplicationInfo getAppInfo(File file) throws Exception {
136 | /*
137 | 执行此方法获取 ApplicationInfo
138 | public static ApplicationInfo generateApplicationInfo(Package p, int flags,PackageUserState state)
139 | */
140 | Class> mPackageParserClass = Class.forName("android.content.pm.PackageParser");
141 | Class> mPackageClass = Class.forName("android.content.pm.PackageParser$Package");
142 | Class> mPackageUserStateClass = Class.forName("android.content.pm.PackageUserState");
143 | //获取 generateApplicationInfo 方法
144 | Method generateApplicationInfoMethod = mPackageParserClass.getDeclaredMethod("generateApplicationInfo",
145 | mPackageClass, int.class, mPackageUserStateClass);
146 |
147 | //创建 PackageParser 实例
148 | Object mmPackageParser = mPackageParserClass.newInstance();
149 |
150 | //获取 Package 实例
151 | /*
152 | 执行此方法获取一个 Package 实例
153 | public Package parsePackage(File packageFile, int flags)
154 | */
155 | //获取 parsePackage 方法
156 | Method parsePackageMethod = mPackageParserClass.getDeclaredMethod("parsePackage", File.class, int.class);
157 | //执行 parsePackage 方法获取 Package 实例
158 | Object mPackage = parsePackageMethod.invoke(mmPackageParser, file, PackageManager.GET_ACTIVITIES);
159 |
160 | //执行 generateApplicationInfo 方法,获取 ApplicationInfo 实例
161 | ApplicationInfo applicationInfo = (ApplicationInfo) generateApplicationInfoMethod.invoke(null, mPackage, 0,
162 | mPackageUserStateClass.newInstance());
163 | //我们获取的 ApplicationInfo 默认路径是没有设置的,我们要自己设置
164 | // applicationInfo.sourceDir = 插件路径;
165 | // applicationInfo.publicSourceDir = 插件路径;
166 | applicationInfo.sourceDir = file.getAbsolutePath();
167 | applicationInfo.publicSourceDir = file.getAbsolutePath();
168 | return applicationInfo;
169 | }
170 |
171 |
172 | public void hookAMSAction() throws Exception {
173 | //动态代理
174 | Class> mIActivityManagerClass;
175 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
176 | mIActivityManagerClass = Class.forName("android.app.IActivityTaskManager");
177 | } else {
178 | mIActivityManagerClass = Class.forName("android.app.IActivityManager");
179 | }
180 | //获取 ActivityManager 或 ActivityManagerNative 或 ActivityTaskManager
181 | Class> mActivityManagerClass;
182 | Method getActivityManagerMethod;
183 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
184 | mActivityManagerClass = Class.forName("android.app.ActivityManagerNative");
185 | getActivityManagerMethod = mActivityManagerClass.getDeclaredMethod("getDefault");
186 | } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
187 | mActivityManagerClass = Class.forName("android.app.ActivityManager");
188 | getActivityManagerMethod = mActivityManagerClass.getDeclaredMethod("getService");
189 | } else {
190 | mActivityManagerClass = Class.forName("android.app.ActivityTaskManager");
191 | getActivityManagerMethod = mActivityManagerClass.getDeclaredMethod("getService");
192 | }
193 | getActivityManagerMethod.setAccessible(true);
194 | //这个实例本质是 IActivityManager或者IActivityTaskManager
195 | final Object IActivityManager = getActivityManagerMethod.invoke(null);
196 |
197 | //创建动态代理
198 | Object mActivityManagerProxy = Proxy.newProxyInstance(
199 | context.getClassLoader(),
200 | new Class[]{mIActivityManagerClass},//要监听的回调接口
201 | new InvocationHandler() {
202 | @Override
203 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
204 |
205 | if ("startActivity".equals(method.getName())) {
206 | //做自己的业务逻辑
207 | //换成可以通过AMS检测的Activity
208 | Intent intent = new Intent(context, ProxyActivity.class);
209 | intent.putExtra("actonIntent", (Intent) args[2]);
210 | args[2] = intent;
211 | }
212 | //让程序继续能够执行下去
213 | return method.invoke(IActivityManager, args);
214 | }
215 | }
216 | );
217 |
218 | //获取 IActivityTaskManagerSingleton 或者 IActivityManagerSingleton 或者 gDefault 属性
219 | Field mSingletonField;
220 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
221 | mSingletonField = mActivityManagerClass.getDeclaredField("gDefault");
222 | } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
223 | mSingletonField = mActivityManagerClass.getDeclaredField("IActivityManagerSingleton");
224 | } else {
225 | mSingletonField = mActivityManagerClass.getDeclaredField("IActivityTaskManagerSingleton");
226 | }
227 | mSingletonField.setAccessible(true);
228 | Object mSingleton = mSingletonField.get(null);
229 |
230 | //替换点
231 | Class> mSingletonClass = Class.forName(Constans.SINGLETON);
232 | Field mInstanceField = mSingletonClass.getDeclaredField("mInstance");
233 | mInstanceField.setAccessible(true);
234 | //将我们创建的动态代理设置到 mInstance 属性当中
235 | mInstanceField.set(mSingleton, mActivityManagerProxy);
236 | }
237 |
238 | public void hookLaunchActivity() throws Exception {
239 | //获取 ActivityThread 类
240 | Class> mActivityThreadClass = Class.forName("android.app.ActivityThread");
241 |
242 | //获取 ActivityThread 的 currentActivityThread() 方法
243 | Method currentActivityThread = mActivityThreadClass.getDeclaredMethod("currentActivityThread");
244 | currentActivityThread.setAccessible(true);
245 | //获取 ActivityThread 实例
246 | Object mActivityThread = currentActivityThread.invoke(null);
247 |
248 | //获取 ActivityThread 的 mH 属性
249 | Field mHField = mActivityThreadClass.getDeclaredField("mH");
250 | mHField.setAccessible(true);
251 | Handler mH = (Handler) mHField.get(mActivityThread);
252 |
253 | //获取 Handler 的 mCallback 属性
254 | Field mCallbackField = Handler.class.getDeclaredField("mCallback");
255 | mCallbackField.setAccessible(true);
256 | //设置我们自定义的 CallBack
257 | mCallbackField.set(mH, new MyCallBack());
258 | }
259 |
260 | class MyCallBack implements Handler.Callback {
261 |
262 | @Override
263 | public boolean handleMessage(@NonNull Message msg) {
264 | if (msg.what == Constans.EXECUTE_TRANSACTION) {
265 | try {
266 | Field mActivityCallbacksField = msg.obj.getClass().getDeclaredField("mActivityCallbacks");
267 | mActivityCallbacksField.setAccessible(true);
268 | List