├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── styles.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── dimens.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── assets
│ │ │ └── plugins
│ │ │ │ ├── plugin-release-unsigned.apk
│ │ │ │ └── pluginlib-release-unsigned.apk
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── lrhehe
│ │ │ │ └── hostplugin
│ │ │ │ ├── MyApplication.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── HostPlugin.java
│ │ │ │ ├── PluginLoader.java
│ │ │ │ └── ReflectAccelerator.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── lrhehe
│ │ │ └── hostplugin
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── lrhehe
│ │ └── hostplugin
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── plugin
├── .gitignore
├── plibs
│ └── classes.jar
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── dimens.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── lrhehe
│ │ │ └── hostplugin
│ │ │ └── plugin
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── lrhehe
│ │ │ └── hostplugin
│ │ │ └── plugin
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── lrhehe
│ │ └── hostplugin
│ │ └── plugin
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── pluginlib
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ └── layout
│ │ │ │ └── activity_pluginlib.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── lrhehe
│ │ │ │ └── hostplugin
│ │ │ │ └── pluginlib
│ │ │ │ ├── PluginLibUtils.java
│ │ │ │ └── PluginLibActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── lrhehe
│ │ │ └── hostplugin
│ │ │ └── pluginlib
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── lrhehe
│ │ └── hostplugin
│ │ └── pluginlib
│ │ └── ApplicationTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── hostplugin.jks
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── README.md
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/plugin/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/pluginlib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':pluginlib'
2 | include ':plugin'
--------------------------------------------------------------------------------
/hostplugin.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/hostplugin.jks
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 |
--------------------------------------------------------------------------------
/plugin/plibs/classes.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/plugin/plibs/classes.jar
--------------------------------------------------------------------------------
/plugin/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | HostPlugin
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/plugin/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/plugin/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/plugin/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/plugin/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/plugin/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/plugin/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/assets/plugins/plugin-release-unsigned.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/assets/plugins/plugin-release-unsigned.apk
--------------------------------------------------------------------------------
/app/src/main/assets/plugins/pluginlib-release-unsigned.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lrhehe/AndroidHostPlugin/HEAD/app/src/main/assets/plugins/pluginlib-release-unsigned.apk
--------------------------------------------------------------------------------
/pluginlib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PluginLib
3 | greet form PluginLib
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/plugin/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/pluginlib/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Mar 23 21:05:54 CST 2016
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-2.9-all.zip
7 |
--------------------------------------------------------------------------------
/plugin/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Plugin
3 | Settings
4 | goto PluginLibActivity
5 | greet from plugin
6 |
7 |
--------------------------------------------------------------------------------
/plugin/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/plugin/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/test/java/com/lrhehe/hostplugin/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/plugin/src/test/java/com/lrhehe/hostplugin/plugin/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.plugin;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/pluginlib/src/main/java/com/lrhehe/hostplugin/pluginlib/PluginLibUtils.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.pluginlib;
2 |
3 | import android.content.Context;
4 | import android.widget.Toast;
5 |
6 | /**
7 | * @Author ray
8 | * @Date 3/31/16.
9 | */
10 | public class PluginLibUtils {
11 |
12 | public static void toast(Context context, String tip) {
13 | Toast.makeText(context, tip, Toast.LENGTH_LONG).show();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/pluginlib/src/test/java/com/lrhehe/hostplugin/pluginlib/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.pluginlib;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/lrhehe/hostplugin/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/plugin/src/androidTest/java/com/lrhehe/hostplugin/plugin/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.plugin;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/pluginlib/src/androidTest/java/com/lrhehe/hostplugin/pluginlib/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.pluginlib;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/pluginlib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lrhehe/hostplugin/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin;
2 |
3 | import android.app.Application;
4 |
5 | /**
6 | * @Author ray
7 | * @Date 3/26/16.
8 | */
9 | public class MyApplication extends Application {
10 | private static MyApplication mInstance;
11 |
12 | public static MyApplication getInstance() {
13 | return mInstance;
14 | }
15 |
16 | @Override
17 | public void onCreate() {
18 | super.onCreate();
19 | HostPlugin.init(this);
20 | mInstance = this;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/pluginlib/src/main/res/layout/activity_pluginlib.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/pluginlib/build.gradle:
--------------------------------------------------------------------------------
1 | //apply plugin: 'com.android.application'
2 | apply plugin: 'com.android.library'
3 |
4 | android {
5 | compileSdkVersion 21
6 | buildToolsVersion "23.0.1"
7 |
8 | defaultConfig {
9 | minSdkVersion 14
10 | targetSdkVersion 21
11 | versionCode 1
12 | versionName "0.1"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | }
26 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ray/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/plugin/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ray/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/pluginlib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ray/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/plugin/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
15 |
16 |
17 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/plugin/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/pluginlib/src/main/java/com/lrhehe/hostplugin/pluginlib/PluginLibActivity.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.pluginlib;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Button;
7 |
8 | /**
9 | * @Author ray
10 | * @Date 3/31/16.
11 | */
12 | public class PluginLibActivity extends Activity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_pluginlib);
18 |
19 | Button button1 = (Button) findViewById(R.id.button1);
20 | button1.setOnClickListener(new View.OnClickListener() {
21 | @Override
22 | public void onClick(View v) {
23 | PluginLibUtils.toast(PluginLibActivity.this, "changed nice nice!");
24 | }
25 | });
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/src/main/java/com/lrhehe/hostplugin/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.content.res.Resources;
6 | import android.os.Bundle;
7 | import android.view.View;
8 |
9 | public class MainActivity extends Activity {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 | setContentView(R.layout.activity_main);
15 | HostPlugin.loadPlugins();
16 | findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
17 | @Override
18 | public void onClick(View v) {
19 | try {
20 | Class clazz = Class.forName("com.lrhehe.hostplugin.plugin.MainActivity");
21 | Intent intent = new Intent(MainActivity.this, clazz);
22 | startActivity(intent);
23 | } catch (Exception e) {
24 | e.printStackTrace();
25 | }
26 | }
27 | });
28 | }
29 |
30 | @Override
31 | public Resources getResources() {
32 | return super.getResources();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/plugin/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 21
5 | buildToolsVersion "23.0.1"
6 |
7 | defaultConfig {
8 | applicationId "com.lrhehe.hostplugin.plugin"
9 | minSdkVersion 14
10 | targetSdkVersion 21
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | signingConfigs {
22 | debug {
23 | storeFile project.rootProject.file("hostplugin.jks")
24 | storePassword "hostplugin"
25 | keyAlias "hostplugin"
26 | keyPassword "hostplugin"
27 | }
28 |
29 | release {
30 | storeFile project.rootProject.file("hostplugin.jks")
31 | storePassword "hostplugin"
32 | keyAlias "hostplugin"
33 | keyPassword "hostplugin"
34 | }
35 | }
36 | }
37 |
38 | dependencies {
39 | compile fileTree(dir: 'libs', include: ['*.jar'])
40 | testCompile 'junit:junit:4.12'
41 | compile 'com.android.support:support-v4:21.0.3'
42 | provided fileTree('plibs/classes.jar')
43 | // compile project(':pluginlib')
44 | }
45 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 21
5 | buildToolsVersion "23.0.1"
6 |
7 | def versionMajor = 0
8 | def versionMinor = 1
9 | def versionPatch = 0
10 |
11 | defaultConfig {
12 | applicationId "com.lrhehe.hostplugin"
13 | minSdkVersion 14
14 | targetSdkVersion 21
15 | versionCode versionMajor * 1000000 + versionMinor * 10000 + versionPatch * 100
16 | versionName "${versionMajor}.${versionMinor}.${versionPatch}"
17 | }
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | signingConfigs {
26 | debug {
27 | storeFile project.rootProject.file("hostplugin.jks")
28 | storePassword "hostplugin"
29 | keyAlias "hostplugin"
30 | keyPassword "hostplugin"
31 | }
32 |
33 | release {
34 | storeFile project.rootProject.file("hostplugin.jks")
35 | storePassword "hostplugin"
36 | keyAlias "hostplugin"
37 | keyPassword "hostplugin"
38 | }
39 | }
40 | }
41 |
42 | dependencies {
43 | compile fileTree(dir: 'libs', include: ['*.jar'])
44 | testCompile 'junit:junit:4.12'
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AndroidHostPlugin
2 | The most simple android plugin framework that fouse on module split and dynamic update.
3 |
4 | 针对模块拆分和动态更新的 Android 插件化开发最简框架。
5 |
6 |
7 |
8 | ###针对场景
9 |
10 | 1. 模块拆分:将工程中模块(application 或者 library)打包成独立插件 (plugin),宿主(Host)启动后进行加载
11 | 2. 动态更新:可下载插件,对原插件进行动态更新(重启应用可生效)
12 |
13 | ### 原理
14 |
15 | 1. 使用 DexClassLoader 加载插件代码
16 | 2. 给每个插件建立一套 AssetManager 和 Resources,借鉴 [Dynamic-load-Apk](https://github.com/singwhatiwanna/dynamic-load-apk) 项目
17 | 3. 代理 Instrumentation(借鉴 [Small](https://github.com/wequick/Small) 项目),在启动插件 Activity 的时候设置对应的资源
18 |
19 | 具体参见 [wiki](https://github.com/lrhehe/AndroidHostPlugin/wiki/Android-%E6%8F%92%E4%BB%B6%E5%8C%96%E5%BC%80%E5%8F%91)
20 |
21 | ###优势
22 |
23 | 采用最简单的方案,引入最少的坑
24 |
25 | 1. 不用对现有代码做修改
26 | 2. 不用对资源做修改
27 | 3. 原生的 Activity 启动方式(存在局限,见局限1)
28 |
29 | ###局限
30 |
31 | 1. 插件的 Activity 和 Service 要在宿主中进行注册
32 | 2. 由于每个插件一套独立资源,宿主,插件之间不能够互相访问资源
33 |
34 |
35 |
36 | ###进度:
37 |
38 | * 0.1.0
39 |
40 | 实现了简单demo:一个宿主(host),一个插件(plugin), 一个插件库(pluginlib)
41 |
42 | plugin 依赖 pluginlib,host 启动后加载 plugin.apk 和 pluginlib.apk
43 |
44 | 从 host 可以打开 plugin 的 MainActivity
45 |
46 | 从 plugin 可以打开 pluginlib 的 PluginLibActivity
47 |
48 | 在 plugin 可以使用 pluginlib 的 PluginLibUtils
49 |
50 |
51 |
52 | ###感谢
53 |
54 | 感谢 [Piasy](https://github.com/Piasy),其对技术的追求和在 github 上的活跃很激励我,在知道我在寻找插件化方案后,向我推荐了 Small
55 |
56 | 感谢 Small 项目作者[林广亮]( https://github.com/galenlin),在我调研过程中的答疑解惑
--------------------------------------------------------------------------------
/plugin/src/main/java/com/lrhehe/hostplugin/plugin/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin.plugin;
2 |
3 | import android.content.Intent;
4 | import android.content.res.Resources;
5 | import android.os.Bundle;
6 | import android.support.v4.app.FragmentActivity;
7 | import android.view.View;
8 | import android.widget.Button;
9 |
10 | import com.lrhehe.hostplugin.pluginlib.PluginLibActivity;
11 | import com.lrhehe.hostplugin.pluginlib.PluginLibUtils;
12 |
13 | /**
14 | * @Author ray
15 | * @Date 3/31/16.
16 | */
17 | public class MainActivity extends FragmentActivity {
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_main);
23 |
24 | Button button1 = (Button) findViewById(R.id.button1);
25 | button1.setOnClickListener(new View.OnClickListener() {
26 | @Override
27 | public void onClick(View v) {
28 | PluginLibUtils.toast(MainActivity.this, "hello buddy");
29 | }
30 | });
31 |
32 | Button button2 = (Button) findViewById(R.id.button2);
33 | button2.setOnClickListener(new View.OnClickListener() {
34 | @Override
35 | public void onClick(View v) {
36 | try {
37 | Intent intent = new Intent(MainActivity.this, PluginLibActivity.class);
38 | startActivity(intent);
39 | } catch (Exception e) {
40 | e.printStackTrace();
41 | }
42 | }
43 | });
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lrhehe/hostplugin/HostPlugin.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageInfo;
7 | import android.content.pm.PackageManager;
8 | import android.content.res.AssetManager;
9 | import android.content.res.Resources;
10 | import android.util.Log;
11 |
12 | import java.io.File;
13 | import java.io.FileOutputStream;
14 | import java.io.IOException;
15 | import java.io.InputStream;
16 | import java.io.OutputStream;
17 | import java.lang.reflect.Method;
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | /**
22 | * @Author ray
23 | * @Date 3/23/16.
24 | */
25 | public class HostPlugin {
26 |
27 | private static final String TAG = "HostPlugin";
28 | private static Context sContext;
29 | private static String sPluginsDir;
30 | private static PluginLoader sPluginLoader;
31 |
32 | private static List sPluginFiles = new ArrayList<>();
33 |
34 | public static void init(Context context) {
35 | init(context, null);
36 | }
37 |
38 | public static void init(Context context, PluginLoader pluginLoader) {
39 | sContext = context;
40 | File file = sContext.getFileStreamPath("plugins");
41 | if (!file.exists() && !file.mkdir()) {
42 | Log.e(TAG, "create plugins dir fail");
43 | return;
44 | }
45 | sPluginsDir = file.getAbsolutePath();
46 | if (pluginLoader == null) {
47 | sPluginLoader = new PluginLoader(context);
48 | } else {
49 | sPluginLoader = pluginLoader;
50 | }
51 | }
52 |
53 |
54 | public static void loadPlugins() {
55 | copyPluginsToLocal();
56 |
57 | for (File plugin : sPluginFiles) {
58 | sPluginLoader.load(plugin);
59 | }
60 | }
61 |
62 | private static void copyPluginsToLocal() {
63 | AssetManager assetManager = sContext.getAssets();
64 | try {
65 | for (String pluginName : assetManager.list("plugins")) {
66 | InputStream inputStream = assetManager.open("plugins/" + pluginName);
67 | File pluginFile = new File(sPluginsDir, pluginName);
68 | if (!pluginFile.exists()) {
69 | pluginFile.createNewFile();
70 | }
71 | OutputStream outputStream = new FileOutputStream(pluginFile);
72 | byte[] buffer = new byte[1024 * 4];
73 | int n;
74 | while (-1 != (n = inputStream.read(buffer))) {
75 | outputStream.write(buffer, 0, n);
76 | }
77 | inputStream.close();
78 | outputStream.close();
79 | sPluginFiles.add(pluginFile);
80 | }
81 | } catch (IOException e) {
82 | e.printStackTrace();
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lrhehe/hostplugin/PluginLoader.java:
--------------------------------------------------------------------------------
1 | package com.lrhehe.hostplugin;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.app.Instrumentation;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.pm.ActivityInfo;
9 | import android.content.pm.PackageInfo;
10 | import android.content.pm.PackageManager;
11 | import android.content.res.AssetManager;
12 | import android.content.res.Resources;
13 | import android.util.Pair;
14 |
15 | import java.io.File;
16 | import java.lang.reflect.Field;
17 | import java.lang.reflect.Method;
18 | import java.util.HashMap;
19 | import java.util.Map;
20 |
21 | /**
22 | * @Author ray
23 | * @Date 3/31/16.
24 | */
25 | public class PluginLoader {
26 |
27 | protected static Instrumentation sHostInstrumentation;
28 |
29 | private static final String FD_STORAGE = "storage";
30 | private static final String FD_LIBRARY = "lib";
31 | private static Context mContext;
32 | private static final String FILE_DEX = "bundle.dex";
33 |
34 | /**
35 | * Class for redirect activity from Stub(AndroidManifest.xml) to Real(Plugin)
36 | */
37 | private static class InstrumentationWrapper extends Instrumentation {
38 |
39 | public InstrumentationWrapper() {}
40 |
41 | @Override
42 | public void callActivityOnCreate(Activity activity, android.os.Bundle icicle) {
43 | String activityName = activity.getClass().getName();
44 | if (mActivityResourcesMap.containsKey(activityName)) {
45 | ActivityInfo activityInfo = mActivityResourcesMap.get(activityName).first;
46 | applyActivityInfo(activity, activityInfo);
47 | }
48 | super.callActivityOnCreate(activity, icicle);
49 | }
50 |
51 | @Override
52 | public Activity newActivity(ClassLoader cl, String className, Intent intent)
53 | throws InstantiationException, IllegalAccessException, ClassNotFoundException {
54 | Activity activity = super.newActivity(cl, className, intent);
55 | String activityName = activity.getClass().getName();
56 | if (mActivityResourcesMap.containsKey(activityName)) {
57 | Resources resources = mActivityResourcesMap.get(activityName).second;
58 | ensureAddAssetPath(activity, resources);
59 | }
60 | return activity;
61 | }
62 | }
63 |
64 | public PluginLoader(Context context) {
65 | mContext = context;
66 | // Inject instrumentation
67 | if (sHostInstrumentation == null) {
68 | try {
69 | final Class> activityThreadClass = Class.forName("android.app.ActivityThread");
70 | final Method method = activityThreadClass.getMethod("currentActivityThread");
71 | Object thread = method.invoke(null, (Object[]) null);
72 | Field field = activityThreadClass.getDeclaredField("mInstrumentation");
73 | field.setAccessible(true);
74 | sHostInstrumentation = (Instrumentation) field.get(thread);
75 | Instrumentation wrapper = new InstrumentationWrapper();
76 | field.set(thread, wrapper);
77 |
78 | // if (mContext instanceof Activity) {
79 | // field = Activity.class.getDeclaredField("mInstrumentation");
80 | // field.setAccessible(true);
81 | // field.set(mContext, wrapper);
82 | // }
83 | } catch (Exception e) {
84 | e.printStackTrace();
85 | }
86 | }
87 | }
88 |
89 | private static Map> mActivityResourcesMap = new HashMap<>();
90 |
91 | public void load(File plugin) {
92 | String pluginPath = plugin.getPath();
93 | PackageManager pm = MyApplication.getInstance().getPackageManager();
94 | PackageInfo pluginInfo = pm.getPackageArchiveInfo(pluginPath,
95 | PackageManager.GET_ACTIVITIES);
96 | Resources resources = getPluginResources(pluginPath);
97 | for (ActivityInfo info : pluginInfo.activities) {
98 | mActivityResourcesMap.put(info.name, Pair.create(info, resources));
99 | }
100 |
101 | // load dex
102 | File packagePath = mContext.getFileStreamPath(FD_STORAGE);
103 | packagePath = new File(packagePath, pluginInfo.packageName);
104 | if (!packagePath.exists()) {
105 | packagePath.mkdirs();
106 | }
107 | File libDir = new File(packagePath, FD_LIBRARY);
108 | File optDexFile = new File(packagePath, FILE_DEX);
109 |
110 | ReflectAccelerator.expandDexPathList(
111 | mContext.getClassLoader(), pluginPath, libDir.getPath(), optDexFile.getPath());
112 |
113 | // Call bundle application onCreate
114 | String bundleApplicationName = pluginInfo.applicationInfo.className;
115 | if (bundleApplicationName != null) {
116 | try {
117 | Class applicationClass = Class.forName(bundleApplicationName);
118 | Application bundleApplication = Instrumentation.newApplication(
119 | applicationClass, MyApplication.getInstance());
120 | sHostInstrumentation.callApplicationOnCreate(bundleApplication);
121 | ReflectAccelerator.setResources(bundleApplication, resources);
122 | } catch (Exception e) {
123 | e.printStackTrace();
124 | }
125 | }
126 | }
127 |
128 | private Resources getPluginResources(String pluginPath) {
129 | AssetManager assetManager = ReflectAccelerator.newAssetManager();
130 | ReflectAccelerator.addAssetPath(assetManager, pluginPath);
131 | Resources superRes = mContext.getResources();
132 | return new Resources(assetManager, superRes.getDisplayMetrics(),
133 | superRes.getConfiguration());
134 | }
135 |
136 | /**
137 | * Apply plugin activity info with plugin's AndroidManifest.xml
138 | *
139 | * @param activity
140 | * @param ai
141 | */
142 | private static void applyActivityInfo(Activity activity, ActivityInfo ai) {
143 | // Apply plugin theme
144 | ReflectAccelerator.setTheme(activity, null);
145 | activity.setTheme(ai.getThemeResource());
146 | // Apply plugin softInputMode
147 | activity.getWindow().setSoftInputMode(ai.softInputMode);
148 | }
149 |
150 | private static void ensureAddAssetPath(Activity activity, Resources resources) {
151 | // Replace resources for application and activity
152 | // ReflectAccelerator.setResources(activity.getApplication(), resources);
153 | ReflectAccelerator.setResources(activity, resources);
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lrhehe/hostplugin/ReflectAccelerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-present wequick.net
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain
6 | * a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | *
16 | */
17 |
18 | // this is a copy from https://github.com/wequick/Small, thanks Small.
19 | package com.lrhehe.hostplugin;
20 |
21 | import android.app.Activity;
22 | import android.app.Application;
23 | import android.app.Instrumentation;
24 | import android.content.Context;
25 | import android.content.Intent;
26 | import android.content.pm.PackageManager;
27 | import android.content.pm.Signature;
28 | import android.content.res.AssetManager;
29 | import android.content.res.Resources;
30 | import android.os.Build;
31 | import android.os.IBinder;
32 | import android.util.DisplayMetrics;
33 | import android.view.ContextThemeWrapper;
34 |
35 | import dalvik.system.DexClassLoader;
36 | import dalvik.system.DexFile;
37 |
38 | import java.io.File;
39 | import java.lang.reflect.Array;
40 | import java.lang.reflect.Constructor;
41 | import java.lang.reflect.Field;
42 | import java.lang.reflect.Method;
43 | import java.util.zip.ZipFile;
44 |
45 | /**
46 | * This class consists exclusively of static methods that accelerate reflections.
47 | */
48 | public class ReflectAccelerator {
49 | // AssetManager.addAssetPath
50 | private static Method sAddAssetPath;
51 | // DexPathList
52 | private static Constructor sDexElementConstructor;
53 | private static Class sDexElementClass;
54 | private static Field sPathListField;
55 | private static Field sDexElementsField;
56 | // ApplicationInfo.resourceDirs
57 | private static Field sContextThemeWrapper_mTheme_field;
58 | private static Field sContextThemeWrapper_mResources_field;
59 | private static Field sContextImpl_mResources_field;
60 | private static Field sActivity_mMainThread_field;
61 | private static Method sActivityThread_currentActivityThread_method;
62 | private static Method sInstrumentation_execStartActivityV21_method;
63 | private static Method sInstrumentation_execStartActivityV20_method;
64 | // Signatures - V13
65 | private static Constructor sPackageParser_constructor;
66 | private static Method sPackageParser_parsePackage_method;
67 | private static Method sPackageParser_collectCertificates_method;
68 | private static Field sPackageParser$Package_mSignatures_field;
69 | // DexClassLoader - V13
70 | private static Field sDexClassLoader_mFiles_field;
71 | private static Field sDexClassLoader_mPaths_field;
72 | private static Field sDexClassLoader_mZips_field;
73 | private static Field sDexClassLoader_mDexs_field;
74 |
75 | private ReflectAccelerator() {/** cannot be instantiated */
76 | }
77 |
78 | // ______________________________________________________________________________________________
79 | // API
80 |
81 | public static int addAssetPath(AssetManager assets, String path) {
82 | Method sAddAssetPath = getMethod(AssetManager.class,
83 | "addAssetPath", new Class[] {
84 | String.class
85 | });
86 | if (sAddAssetPath == null)
87 | return 0;
88 | Integer ret = invoke(sAddAssetPath, assets, path);
89 | if (ret == null)
90 | return 0;
91 | return ret;
92 | }
93 |
94 | // ______________________________________________________________________________________________
95 | // Dex path list
96 |
97 | /**
98 | * Make dex element
99 | *
100 | * @see DexPathList.java
102 | * @param pkg archive android package with any file extensions
103 | * @param dexFile
104 | * @return dalvik.system.DexPathList$Element
105 | */
106 | private static Object makeDexElement(File pkg, DexFile dexFile) throws Exception {
107 | if (sDexElementClass == null) {
108 | sDexElementClass = Class.forName("dalvik.system.DexPathList$Element");
109 | }
110 | if (sDexElementConstructor == null) {
111 | sDexElementConstructor = sDexElementClass.getConstructors()[0];
112 | }
113 | Class>[] types = sDexElementConstructor.getParameterTypes();
114 | switch (types.length) {
115 | case 3:
116 | if (types[1].equals(ZipFile.class)) {
117 | // Element(File apk, ZipFile zip, DexFile dex)
118 | ZipFile zip = new ZipFile(pkg);
119 | return sDexElementConstructor.newInstance(pkg, zip, dexFile);
120 | } else {
121 | // Element(File apk, File zip, DexFile dex)
122 | return sDexElementConstructor.newInstance(pkg, pkg, dexFile);
123 | }
124 | case 4:
125 | default:
126 | // Element(File apk, boolean isDir, File zip, DexFile dex)
127 | return sDexElementConstructor.newInstance(pkg, false, pkg, dexFile);
128 | }
129 | }
130 |
131 | public static boolean expandDexPathList(ClassLoader cl, String dexPath,
132 | String libraryPath, String optDexPath) {
133 | if (Build.VERSION.SDK_INT < 14) {
134 | try {
135 | if (sDexClassLoader_mFiles_field == null) {
136 | sDexClassLoader_mFiles_field = getDeclaredField(cl.getClass(), "mFiles");
137 | sDexClassLoader_mPaths_field = getDeclaredField(cl.getClass(), "mPaths");
138 | sDexClassLoader_mZips_field = getDeclaredField(cl.getClass(), "mZips");
139 | sDexClassLoader_mDexs_field = getDeclaredField(cl.getClass(), "mDexs");
140 | }
141 | if (sDexClassLoader_mFiles_field == null
142 | || sDexClassLoader_mPaths_field == null
143 | || sDexClassLoader_mZips_field == null
144 | || sDexClassLoader_mDexs_field == null) {
145 | return false;
146 | }
147 |
148 | File pathFile = new File(dexPath);
149 | expandArray(cl, sDexClassLoader_mFiles_field, new Object[] {
150 | pathFile
151 | }, true);
152 |
153 | expandArray(cl, sDexClassLoader_mPaths_field, new Object[] {
154 | dexPath
155 | }, true);
156 |
157 | ZipFile zipFile = new ZipFile(dexPath);
158 | expandArray(cl, sDexClassLoader_mZips_field, new Object[] {
159 | zipFile
160 | }, true);
161 |
162 | DexFile dexFile = DexFile.loadDex(dexPath, optDexPath, 0);
163 | expandArray(cl, sDexClassLoader_mDexs_field, new Object[] {
164 | dexFile
165 | }, true);
166 | } catch (Exception e) {
167 | return false;
168 | }
169 | } else {
170 | try {
171 | File pkg = new File(dexPath);
172 | DexFile dexFile = DexFile.loadDex(dexPath, optDexPath, 0);
173 | Object element = makeDexElement(pkg, dexFile);
174 | fillDexPathList(cl, element);
175 | } catch (Exception e) {
176 | e.printStackTrace();
177 | return false;
178 | }
179 | }
180 | return true;
181 | }
182 |
183 | /**
184 | * Add elements to Object[] with reflection
185 | *
186 | * @see MultiDex
188 | * @param target
189 | * @param arrField
190 | * @param extraElements
191 | * @param push true=push to array head, false=append to array tail
192 | * @throws IllegalAccessException
193 | */
194 | public static void expandArray(Object target, Field arrField,
195 | Object[] extraElements, boolean push)
196 | throws IllegalAccessException {
197 | Object[] original = (Object[]) arrField.get(target);
198 | Object[] combined = (Object[]) Array.newInstance(
199 | original.getClass().getComponentType(), original.length + extraElements.length);
200 | if (push) {
201 | System.arraycopy(extraElements, 0, combined, 0, extraElements.length);
202 | System.arraycopy(original, 0, combined, extraElements.length, original.length);
203 | } else {
204 | System.arraycopy(original, 0, combined, 0, original.length);
205 | System.arraycopy(extraElements, 0, combined, original.length, extraElements.length);
206 | }
207 | arrField.set(target, combined);
208 | }
209 |
210 | public static void sliceArray(Object target, Field arrField, int deleteIndex)
211 | throws IllegalAccessException {
212 | Object[] original = (Object[]) arrField.get(target);
213 | if (original.length == 0)
214 | return;
215 |
216 | Object[] sliced = (Object[]) Array.newInstance(
217 | original.getClass().getComponentType(), original.length - 1);
218 | if (deleteIndex > 0) {
219 | // Copy left elements
220 | System.arraycopy(original, 0, sliced, 0, deleteIndex);
221 | }
222 | int rightCount = original.length - deleteIndex - 1;
223 | if (rightCount > 0) {
224 | // Copy right elements
225 | System.arraycopy(original, deleteIndex + 1, sliced, deleteIndex, rightCount);
226 | }
227 | arrField.set(target, sliced);
228 | }
229 |
230 | private static void fillDexPathList(ClassLoader cl, Object element)
231 | throws NoSuchFieldException, IllegalAccessException {
232 | if (sPathListField == null) {
233 | sPathListField = getDeclaredField(DexClassLoader.class.getSuperclass(), "pathList");
234 | }
235 | Object pathList = sPathListField.get(cl);
236 | if (sDexElementsField == null) {
237 | sDexElementsField = getDeclaredField(pathList.getClass(), "dexElements");
238 | }
239 | expandArray(pathList, sDexElementsField, new Object[] {
240 | element
241 | }, true);
242 | }
243 |
244 | public static void removeDexPathList(ClassLoader cl, int deleteIndex) {
245 | try {
246 | if (sPathListField == null) {
247 | sPathListField = getDeclaredField(DexClassLoader.class.getSuperclass(), "pathList");
248 | }
249 | Object pathList = sPathListField.get(cl);
250 | if (sDexElementsField == null) {
251 | sDexElementsField = getDeclaredField(pathList.getClass(), "dexElements");
252 | }
253 | sliceArray(pathList, sDexElementsField, deleteIndex);
254 | } catch (Exception e) {
255 | e.printStackTrace();
256 | }
257 | }
258 |
259 | public static void setTheme(Activity activity, Resources.Theme theme) {
260 | Field sContextThemeWrapper_mTheme_field = getDeclaredField(
261 | ContextThemeWrapper.class, "mTheme");
262 | if (sContextThemeWrapper_mTheme_field == null)
263 | return;
264 | setValue(sContextThemeWrapper_mTheme_field, activity, theme);
265 | }
266 |
267 | public static void setResources(Activity activity, Resources resources) {
268 | Object target = activity;
269 | Class targetClass = ContextThemeWrapper.class;
270 | if (Build.VERSION.SDK_INT <= 16) {
271 | // wu4321: Fix resource not found bug for API16-
272 | target = activity.getBaseContext();
273 | targetClass = target.getClass();
274 | }
275 | Field sContextThemeWrapper_mResources_field = getDeclaredField(targetClass, "mResources");
276 | setValue(sContextThemeWrapper_mResources_field, target, resources);
277 | }
278 |
279 | public static void setResources(Application app, Resources resources) {
280 | setResources(app.getBaseContext(), resources);
281 | }
282 |
283 | public static void setResources(Context context, Resources resources) {
284 |
285 | Field sContextImpl_mResources_field = getDeclaredField(
286 | context.getClass(), "mResources");
287 | if (sContextImpl_mResources_field == null)
288 | return;
289 | setValue(sContextImpl_mResources_field, context, resources);
290 | }
291 |
292 | public static Object getActivityThread(Context context) {
293 | if (context instanceof Activity) {
294 | if (sActivity_mMainThread_field == null) {
295 | sActivity_mMainThread_field = getDeclaredField(Activity.class, "mMainThread");
296 | }
297 | if (sActivity_mMainThread_field == null)
298 | return null;
299 | return getValue(sActivity_mMainThread_field, context);
300 | } else {
301 | if (sActivityThread_currentActivityThread_method == null) {
302 | try {
303 | Class> activityThreadClazz = Class.forName("android.app.ActivityThread");
304 | sActivityThread_currentActivityThread_method = getMethod(
305 | activityThreadClazz, "currentActivityThread", null);
306 | } catch (ClassNotFoundException e) {
307 | return null;
308 | }
309 | }
310 | return invoke(sActivityThread_currentActivityThread_method, null, (Object[]) null);
311 | }
312 | }
313 |
314 | public static AssetManager newAssetManager() {
315 | AssetManager assets;
316 | try {
317 | assets = AssetManager.class.newInstance();
318 | } catch (InstantiationException e1) {
319 | e1.printStackTrace();
320 | return null;
321 | } catch (IllegalAccessException e1) {
322 | e1.printStackTrace();
323 | return null;
324 | }
325 | return assets;
326 | }
327 |
328 | public static Instrumentation.ActivityResult execStartActivityV21(
329 | Instrumentation instrumentation,
330 | Context who, IBinder contextThread, IBinder token, Activity target,
331 | Intent intent, int requestCode, android.os.Bundle options) {
332 | if (sInstrumentation_execStartActivityV21_method == null) {
333 | Class[] types = new Class[] {
334 | Context.class, IBinder.class, IBinder.class,
335 | Activity.class, Intent.class, int.class, android.os.Bundle.class
336 | };
337 | sInstrumentation_execStartActivityV21_method = getMethod(Instrumentation.class,
338 | "execStartActivity", types);
339 | }
340 | if (sInstrumentation_execStartActivityV21_method == null)
341 | return null;
342 | return invoke(sInstrumentation_execStartActivityV21_method, instrumentation,
343 | who, contextThread, token, target, intent, requestCode, options);
344 | }
345 |
346 | public static Instrumentation.ActivityResult execStartActivityV20(
347 | Instrumentation instrumentation,
348 | Context who, IBinder contextThread, IBinder token, Activity target,
349 | Intent intent, int requestCode) {
350 | if (sInstrumentation_execStartActivityV20_method == null) {
351 | Class[] types = new Class[] {
352 | Context.class, IBinder.class, IBinder.class,
353 | Activity.class, Intent.class, int.class
354 | };
355 | sInstrumentation_execStartActivityV20_method = getMethod(Instrumentation.class,
356 | "execStartActivity", types);
357 | }
358 | if (sInstrumentation_execStartActivityV20_method == null)
359 | return null;
360 | return invoke(sInstrumentation_execStartActivityV20_method, instrumentation,
361 | who, contextThread, token, target, intent, requestCode);
362 | }
363 |
364 | /**
365 | * @see PackageParser.java
367 | */
368 | public static Signature[] getSignaturesV13(File plugin) {
369 | try {
370 | if (sPackageParser_constructor == null) {
371 | Class clazz = Class.forName("android.content.pm.PackageParser");
372 | sPackageParser_constructor = clazz.getConstructors()[0];
373 | if (sPackageParser_constructor == null)
374 | return null;
375 |
376 | sPackageParser_parsePackage_method = getDeclaredMethod(clazz, "parsePackage",
377 | new Class[] {
378 | File.class, String.class, DisplayMetrics.class, Integer.TYPE
379 | });
380 | if (sPackageParser_parsePackage_method == null)
381 | return null;
382 |
383 | Class pkgClazz = sPackageParser_parsePackage_method.getReturnType();
384 | sPackageParser_collectCertificates_method = getDeclaredMethod(clazz,
385 | "collectCertificates",
386 | new Class[] {
387 | pkgClazz, Integer.TYPE
388 | });
389 | if (sPackageParser_collectCertificates_method == null)
390 | return null;
391 |
392 | sPackageParser$Package_mSignatures_field = getDeclaredField(pkgClazz, "mSignatures");
393 | if (sPackageParser$Package_mSignatures_field == null)
394 | return null;
395 | }
396 |
397 | String path = plugin.getPath();
398 | Object parser = sPackageParser_constructor.newInstance(path);
399 | DisplayMetrics metrics = new DisplayMetrics();
400 | metrics.setToDefaults();
401 | Object pkg = sPackageParser_parsePackage_method.invoke(parser,
402 | plugin, path, metrics, PackageManager.GET_SIGNATURES);
403 | sPackageParser_collectCertificates_method.invoke(parser,
404 | pkg, PackageManager.GET_SIGNATURES);
405 | return getValue(sPackageParser$Package_mSignatures_field, pkg);
406 | } catch (Exception e) {
407 | e.printStackTrace();
408 | }
409 | return null;
410 | }
411 |
412 | // ______________________________________________________________________________________________
413 | // Private
414 |
415 | private static Method getMethod(Class cls, String methodName, Class[] types) {
416 | try {
417 | Method method = cls.getMethod(methodName, types);
418 | method.setAccessible(true);
419 | return method;
420 | } catch (NoSuchMethodException e) {
421 | return null;
422 | }
423 | }
424 |
425 | private static Method getDeclaredMethod(Class cls, String methodName, Class[] types) {
426 | try {
427 | Method method = cls.getDeclaredMethod(methodName, types);
428 | method.setAccessible(true);
429 | return method;
430 | } catch (NoSuchMethodException e) {
431 | return null;
432 | }
433 | }
434 |
435 | private static Field getDeclaredField(Class cls, String fieldName) {
436 | try {
437 | Field field = cls.getDeclaredField(fieldName);
438 | field.setAccessible(true);
439 | return field;
440 | } catch (NoSuchFieldException e) {
441 | return null;
442 | }
443 | }
444 |
445 | private static T invoke(Method method, Object target, Object... args) {
446 | try {
447 | return (T) method.invoke(target, args);
448 | } catch (Exception e) {
449 | // Ignored
450 | e.printStackTrace();
451 | return null;
452 | }
453 | }
454 |
455 | private static T getValue(Field field, Object target) {
456 | try {
457 | return (T) field.get(target);
458 | } catch (IllegalAccessException e) {
459 | e.printStackTrace();
460 | return null;
461 | }
462 | }
463 |
464 | private static void setValue(Field field, Object target, Object value) {
465 | try {
466 | field.set(target, value);
467 | } catch (IllegalAccessException e) {
468 | // Ignored
469 | e.printStackTrace();
470 | }
471 | }
472 | }
473 |
--------------------------------------------------------------------------------