├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ ├── dimens.xml
│ │ │ └── styles.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
│ │ └── testapk-with-res.apk
│ │ ├── kotlin
│ │ └── com
│ │ │ └── fashare
│ │ │ └── app
│ │ │ ├── App.kt
│ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
├── build.gradle
└── proguard-rules.pro
├── dl
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ └── strings.xml
│ │ └── layout
│ │ │ └── activity_proxy.xml
│ │ ├── AndroidManifest.xml
│ │ └── kotlin
│ │ └── com
│ │ └── fashare
│ │ └── dl
│ │ ├── DL.kt
│ │ └── InstrumentationProxy.kt
├── build.gradle
└── proguard-rules.pro
├── testapk
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable
│ │ │ │ ├── photo.jpg
│ │ │ │ └── ic_launcher.png
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── fashare
│ │ │ │ └── testapk
│ │ │ │ ├── Test.java
│ │ │ │ └── PluginActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── fashare
│ │ │ └── testapk
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── fashare
│ │ └── testapk
│ │ └── ExampleInstrumentedTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── README.md
├── gradlew.bat
├── ext.gradle
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/dl/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/testapk/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':dl', ':testapk'
2 |
--------------------------------------------------------------------------------
/dl/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DL
3 |
4 |
--------------------------------------------------------------------------------
/testapk/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DL-Learning
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/dl/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | dependencies {
4 | compile joor
5 | compile support.appcompat
6 | }
7 |
--------------------------------------------------------------------------------
/testapk/src/main/res/drawable/photo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/testapk/src/main/res/drawable/photo.jpg
--------------------------------------------------------------------------------
/app/src/main/assets/testapk-with-res.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/app/src/main/assets/testapk-with-res.apk
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/testapk/src/main/res/drawable/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fashare2015/Dynamic-Load-Learning/HEAD/testapk/src/main/res/drawable/ic_launcher.png
--------------------------------------------------------------------------------
/testapk/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | testapk
3 | MainActivity
4 | I\'m plugin Activity!
5 |
6 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | defaultConfig {
5 | applicationId "com.fashare.app"
6 | }
7 | }
8 |
9 | dependencies {
10 | compile project(":dl")
11 | compile support.appcompat
12 | }
13 |
--------------------------------------------------------------------------------
/testapk/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | defaultConfig {
5 | applicationId "com.fashare.testapk"
6 | }
7 | }
8 |
9 | dependencies {
10 | compile support.appcompat
11 | compile support.design
12 | }
13 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Feb 09 11:21:11 CST 2018
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-3.5-all.zip
7 |
--------------------------------------------------------------------------------
/testapk/src/main/java/com/fashare/testapk/Test.java:
--------------------------------------------------------------------------------
1 | package com.fashare.testapk;
2 |
3 | import android.content.Context;
4 | import android.widget.Toast;
5 |
6 | public class Test {
7 | public static void sayHello(Context context){
8 | Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/dl/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/testapk/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/testapk/src/test/java/com/fashare/testapk/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.fashare.testapk;
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() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/dl/src/main/res/layout/activity_proxy.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/fashare/app/App.kt:
--------------------------------------------------------------------------------
1 | package com.fashare.app
2 |
3 | import android.app.Application
4 | import android.net.Uri
5 | import android.os.Environment
6 | import com.fashare.dl.DL
7 | import java.io.File
8 |
9 | class App: Application(){
10 | override fun onCreate() {
11 | super.onCreate()
12 |
13 | DL.replaceInstrumentation()
14 | val pluginFile = File(Environment.getExternalStorageDirectory(), "testapk-with-res.apk")
15 | assets.open("testapk-with-res.apk").copyTo(pluginFile.outputStream())
16 | DL.loadApk(this, Uri.fromFile(pluginFile))
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/testapk/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/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/jinliangshan/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 |
--------------------------------------------------------------------------------
/dl/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 |
--------------------------------------------------------------------------------
/testapk/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
17 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/testapk/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/testapk/src/androidTest/java/com/fashare/testapk/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.fashare.testapk;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.fashare.testapk.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Tue Oct 24 13:23:37 CST 2017
16 | systemProp.http.proxyHost=127.0.0.1
17 | org.gradle.jvmargs=-Xmx1536m
18 | systemProp.http.proxyPort=80
19 |
--------------------------------------------------------------------------------
/testapk/src/main/java/com/fashare/testapk/PluginActivity.kt:
--------------------------------------------------------------------------------
1 | package com.fashare.testapk
2 |
3 | import android.app.Activity
4 | import android.os.Bundle
5 | import android.util.Log
6 | import android.widget.Toast
7 |
8 | class PluginActivity : Activity() {
9 | val TAG = "PluginActivity"
10 |
11 | override fun onCreate(savedInstanceState: Bundle?) {
12 | super.onCreate(savedInstanceState)
13 | Toast.makeText(this, resources.getString(R.string.plugin_activity), Toast.LENGTH_LONG).show()
14 | setContentView(R.layout.activity_main)
15 |
16 | Log.d(TAG, "onCreate")
17 | }
18 |
19 | override fun onStart() {
20 | super.onStart()
21 | Log.d(TAG, "onStart")
22 | }
23 |
24 | override fun onResume() {
25 | super.onResume()
26 | Log.d(TAG, "onResume")
27 | }
28 |
29 | override fun onPause() {
30 | super.onPause()
31 | Log.d(TAG, "onPause")
32 | }
33 |
34 | override fun onStop() {
35 | super.onStop()
36 | Log.d(TAG, "onStop")
37 | }
38 |
39 | override fun onDestroy() {
40 | super.onDestroy()
41 | Log.d(TAG, "onDestroy")
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 1 前言
2 | 插件化算是比较复杂的一个话题。刚一接触的时候,我是一脸懵逼的,网上看了很多博客,一直是似懂非懂,不得其要领。期间也尝试看了`Small`,也是知其然不知其所以然。
3 |
4 | 就此搁置一段时间,直到真正拿出勇气,尝试自己实现插件化,成功加载了四大组件之一`Activity`。这才明白它的背后究竟做了什么,以及为什么这么做。
5 |
6 | 希望借着这篇文章,谈谈自己的理解。也希望通过我的小 Demo,能帮大家更轻松的理解诸如`Small`、`VirtualApk`、`Atlas`之类的大型框架。如有纰漏,请留言指出。
7 |
8 | # 2 效果预览
9 | `主apk[com.fashare.app.MainActivity]`唤起`sd卡`上的`插件apk[com.fashare.testapk.PluginActivity]` :
10 |
11 | 
12 |
13 | # 3 原理与实现
14 | 详见我的博客:
15 | [插件化理解与实现 —— 加载 Activity「类加载篇」](https://fashare2015.github.io/2018/01/24/dynamic-load-learning-load-activity/)
16 |
17 | # 4 感谢
18 | [《Android插件化技术——原理篇》—— 腾讯Bugly](https://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653579547&idx=1&sn=9f782f6c91c20fd0b17a6c3762b6e06a&chksm=84b3bb1cb3c4320ad660e3a4a274aa2e433bf0401389f38be337d01d2ba604714303e169d48a&mpshare=1&scene=23&srcid=0111lAPa4UGPssFMoc05pgLP#rd)
19 |
20 | [Android 插件化原理解析——插件加载机制 —— weishu](http://weishu.me/2016/04/05/understand-plugin-framework-classloader/)
21 |
22 | [8个类搞定插件化——Activity —— 开源实验室](https://kymjs.com/code/2016/05/15/01/)
23 |
24 | [插件化框架 Small](https://github.com/wequick/Small)
25 |
26 | [插件化框架 VirtualAPK —— 滴滴](https://github.com/didi/VirtualAPK)
27 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/fashare/app/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.fashare.app
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import android.support.v7.app.AppCompatActivity
6 | import com.fashare.dl.DL
7 | import com.fashare.dl.loge
8 | import com.fashare.dl.toast
9 | import org.joor.Reflect
10 |
11 | class MainActivity : AppCompatActivity() {
12 |
13 | override fun onCreate(savedInstanceState: Bundle?) {
14 | super.onCreate(savedInstanceState)
15 | setContentView(R.layout.activity_main)
16 |
17 | findViewById(R.id.btn_load_static_method).setOnClickListener {
18 | try {
19 | DL.dexClassLoader.loadClass("com.fashare.testapk.Test").apply {
20 | Reflect.on(this).call("sayHello", it.context)
21 | }
22 | }catch (e: Exception){
23 | loge(e)
24 | }
25 | }
26 |
27 | findViewById(R.id.btn_goto_test_apk).setOnClickListener {
28 | try {
29 | DL.dexClassLoader.loadClass("com.fashare.testapk.PluginActivity").apply {
30 |
31 | startActivity(Intent(this@MainActivity, this))
32 | }
33 | }catch (e: Exception){
34 | toast("加载失败")
35 | loge(e)
36 | }
37 | }
38 | }
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/dl/src/main/kotlin/com/fashare/dl/DL.kt:
--------------------------------------------------------------------------------
1 | package com.fashare.dl
2 |
3 | import android.app.Instrumentation
4 | import android.content.Context
5 | import android.net.Uri
6 | import android.util.Log
7 | import android.widget.Toast
8 | import dalvik.system.DexClassLoader
9 | import org.joor.Reflect
10 |
11 | /**
12 | *
13 | * author : jinliangshan
14 | * e-mail : jinliangshan@chexiang.com
15 | * desc :
16 | *
17 | */
18 | fun Context.toast(msg: String?){
19 | Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
20 | }
21 |
22 | fun Any.logd(msg: Any?){
23 | Log.d(this.javaClass.simpleName, msg.toString())
24 | }
25 |
26 | fun Any.loge(msg: Any?){
27 | Log.e(this.javaClass.simpleName, msg.toString())
28 | }
29 |
30 |
31 | /**
32 | * 动态加载(门面类)
33 | */
34 | object DL {
35 | lateinit var dexClassLoader: DexClassLoader
36 |
37 | /**
38 | * 加载 sdcard 上的 未安装的 apk
39 | */
40 | fun loadApk(context: Context, uri: Uri) {
41 | dexClassLoader = DexClassLoader(
42 | uri.path,
43 | context.cacheDir.path,
44 | null,
45 | context.classLoader)
46 | }
47 |
48 | /**
49 | * 替换 ActivityThread.mInstrumentation
50 | */
51 | fun replaceInstrumentation() {
52 | Reflect.on("android.app.ActivityThread")
53 | .call("currentActivityThread").apply {
54 |
55 | val base = get("mInstrumentation")
56 | val instrumentation = InstrumentationProxy(base)
57 | set("mInstrumentation", instrumentation)
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/ext.gradle:
--------------------------------------------------------------------------------
1 | // external 全局变量
2 | println("${project.name}: apply ext")
3 |
4 | // 以下写法为闭包, 可能不会立即赋值
5 | project.ext{
6 | println("${project.name}: init ext")
7 |
8 | def androidPluginVersion = "2.2.0"
9 | _android = [
10 | plugin: "com.android.tools.build:gradle:$androidPluginVersion",
11 |
12 | compileSdkVersion : 26,
13 | buildToolsVersion : '26.0.0',
14 |
15 | minSdkVersion : 15,
16 | targetSdkVersion : 24,
17 | versionCode : 1,
18 | versionName : "1.0"
19 | ]
20 |
21 | // kotlin
22 | def kotlinVersion = "1.2.0"
23 | _kotlin = [
24 | // plugins
25 | kotlin_gradle_plugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion",
26 | kotlin_android_extensions: "org.jetbrains.kotlin:kotlin-android-extensions:$kotlinVersion",
27 |
28 | // libs
29 | kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
30 | ]
31 |
32 | // UI
33 | def supportLibraryVersion = "25.3.1"
34 | support = [
35 | support_v4: "com.android.support:support-v4:$supportLibraryVersion",
36 | appcompat: "com.android.support:appcompat-v7:$supportLibraryVersion",
37 | recyclerview: "com.android.support:recyclerview-v7:$supportLibraryVersion",
38 | design: "com.android.support:design:$supportLibraryVersion",
39 | cardview: "com.android.support:cardview-v7:$supportLibraryVersion"
40 | ]
41 | springview = "com.liaoinstan.springview:library:1.3.0"
42 | swipebackhelper = "com.jude:swipebackhelper:3.1.2"
43 |
44 | databindingCompiler = "com.android.databinding:compiler:$androidPluginVersion"
45 |
46 | // rx
47 | def rxjavaVersion = "2.1.0"
48 | def rxandroidVersion = "2.0.1"
49 | rx = [
50 | rxjava2: "io.reactivex.rxjava2:rxjava:$rxjavaVersion",
51 | rxandroid2: "io.reactivex.rxjava2:rxandroid:$rxandroidVersion",
52 | rxlifecycle: "com.trello.rxlifecycle2:rxlifecycle:$rxjavaVersion",
53 | rxlifecycle_android: "com.trello.rxlifecycle2:rxlifecycle-android:$rxjavaVersion",
54 | rxlifecycle_components: "com.trello.rxlifecycle2:rxlifecycle-components:$rxjavaVersion",
55 |
56 | rxpermissions: 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar'
57 | ]
58 |
59 | // net
60 | def okhttp3Version = "3.8.0"
61 | okhttp3 = [
62 | logging_interceptor: "com.squareup.okhttp3:logging-interceptor:$okhttp3Version",
63 | ]
64 | def retrofitVersion = "2.3.0"
65 | retrofit2 = [
66 | retrofit: "com.squareup.retrofit2:retrofit:$retrofitVersion",
67 | adapter_rxjava2: "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion",
68 | converter_gson: "com.squareup.retrofit2:converter-gson:$retrofitVersion"
69 | ]
70 | gson = "com.google.code.gson:gson:2.7"
71 | klog = "com.github.zhaokaiqiang.klog:library:1.6.0"
72 |
73 | def glideVersion = "3.7.0"
74 | glide = [
75 | glide: "com.github.bumptech.glide:glide:$glideVersion",
76 | glide_transformations: "jp.wasabeef:glide-transformations:2.0.1",
77 | okhttp3_integration: "com.github.bumptech.glide:okhttp3-integration:1.4.0@aar"
78 | ]
79 |
80 | // other
81 | utilcode = "com.blankj:utilcode:1.9.0"
82 | joor = 'org.jooq:joor:0.9.5'
83 | }
84 |
--------------------------------------------------------------------------------
/dl/src/main/kotlin/com/fashare/dl/InstrumentationProxy.kt:
--------------------------------------------------------------------------------
1 | package com.fashare.dl
2 |
3 | import android.app.Activity
4 | import android.app.Instrumentation
5 | import android.content.ComponentName
6 | import android.content.Context
7 | import android.content.Intent
8 | import android.content.res.AssetManager
9 | import android.content.res.Resources
10 | import android.os.Build
11 | import android.os.Bundle
12 | import android.os.Environment
13 | import android.os.IBinder
14 | import android.support.v7.app.AppCompatActivity
15 | import org.joor.Reflect
16 | import java.io.File
17 |
18 | /**
19 | * 占坑用 Activity
20 | */
21 | internal class StubActivity : AppCompatActivity()
22 |
23 |
24 | /**
25 | * Intent 代理类,替换 component.className 为 StubActivity
26 | */
27 | internal class IntentProxy(var base: Intent?) : Intent() {
28 | init {
29 | component = ComponentName(base?.component?.packageName,
30 | StubActivity::class.java.name)
31 | }
32 | }
33 |
34 | /**
35 | * 插件专用的 Resources. 从插件apk中读取资源, 不再从宿主apk读取.
36 | */
37 | internal class PluginResources(hostResources: Resources?)
38 | : Resources(AssetManager::class.java.newInstance().apply {
39 | val pluginFile = File(Environment.getExternalStorageDirectory(), "testapk-with-res.apk")
40 | Reflect.on(this).call("addAssetPath", pluginFile.path)
41 | }, hostResources?.displayMetrics, hostResources?.configuration)
42 |
43 | /**
44 | * Instrumentation 代理类
45 | */
46 | internal class InstrumentationProxy(val base: Instrumentation) : Instrumentation() {
47 | var mIntentProxy: IntentProxy? = null
48 |
49 | /**
50 | * new 之前,取出原始的 intent 来 new Activity实例
51 | */
52 | override fun newActivity(cl: ClassLoader?, className: String?, intent: Intent?): Activity? {
53 | logd("newActivity short: " + className)
54 | return (mIntentProxy?.base ?: intent)?.let {
55 | mIntentProxy = null
56 | base.newActivity(DL.dexClassLoader, it.component?.className, it)
57 | }
58 | }
59 |
60 | /**
61 | * start 之前,替换 Intent 为 已注册的 StubActivity, 以绕过 系统对 manifest 的检查
62 | *
63 | * 注: 这里其实也是 override, 只是 super.execStartActivity(...) 为 @hide, 所以看起来比较奇怪.
64 | */
65 | fun execStartActivity(
66 | who: Context?, contextThread: IBinder?, token: IBinder?, target: Activity?,
67 | intent: Intent?, requestCode: Int, options: Bundle?): ActivityResult? {
68 | logd("execStartActivity long: " + intent?.component)
69 |
70 | mIntentProxy = IntentProxy(intent)
71 |
72 | return try {
73 | Reflect.on(base).call("execStartActivity",
74 | who, contextThread, token, target,
75 | mIntentProxy, requestCode, options).get()
76 | } catch (e: Exception) {
77 | loge("execStartActivity long: " + e.toString())
78 | null
79 | }
80 | }
81 |
82 | /**
83 | * 这里主要处理资源的加载, 包括图片等资源文件以及 R.java
84 | */
85 | override fun callActivityOnCreate(activity: Activity?, icicle: Bundle?) {
86 | if (!isFromPlugin(activity)) {
87 | base.callActivityOnCreate(activity, icicle)
88 | return
89 | }
90 | val pluginRes = PluginResources(activity?.resources)
91 | // hack 各处的 mClassLoader, 以便从插件读 R.java . 这步不做的话,默认从宿主读取,会找不到 R.xxx.xxx .
92 | try {
93 | // 替换 ContextImpl.getClassLoader() 为 DL.dexClassLoader
94 | Reflect.on(activity?.baseContext)
95 | .field("mPackageInfo") // get LoadedApk
96 | .set("mClassLoader", DL.dexClassLoader)
97 |
98 | // 替换 PluginResources.mClassLoader 为 DL.dexClassLoader
99 | Reflect.on(pluginRes).set("mClassLoader", DL.dexClassLoader)
100 |
101 | } catch (e: Exception) {
102 | e.printStackTrace()
103 | }
104 |
105 | val pluginTheme = pluginRes.newTheme().apply {
106 | val pluginThemeId = Reflect.on(Resources::class.java)
107 | .call("selectDefaultTheme", 0, Build.VERSION.SDK_INT)
108 | .get()
109 | applyStyle(pluginThemeId, false)
110 | }
111 |
112 | // hack 各处的 mResources, mTheme, 以便从插件读资源文件
113 | try {
114 | // 替换 ContextImpl.mResources, mTheme
115 | Reflect.on(activity?.baseContext)
116 | .set("mResources", pluginRes)
117 | .set("mTheme", pluginTheme)
118 | // 替换 ContextThemeWrapper.mResources, mTheme
119 | Reflect.on(activity)
120 | .set("mResources", pluginRes)
121 | .set("mTheme", pluginTheme)
122 |
123 | } catch (e: Exception) {
124 | e.printStackTrace()
125 | }
126 |
127 | // Activity 的 ActionBar 会使用宿主 Application 的 icon, 即宿主 apk 的 ic_launcher,
128 | // 由于插件不能访问宿主的资源, 会导致找不到 ic_launcher, 此时暂时设为 0, hack 掉.
129 | // 以后加上插件自己的 Application 时, 可以移除这部分 hack.
130 | try {
131 | Reflect.on(activity).field("mActivityInfo")
132 | .field("applicationInfo")
133 | .set("icon", 0)
134 |
135 | } catch (e: Exception) {
136 | e.printStackTrace()
137 | }
138 |
139 | base.callActivityOnCreate(activity, icicle)
140 | }
141 |
142 | /**
143 | * 是插件 apk 中的 Activity
144 | */
145 | private fun isFromPlugin(activity: Activity?): Boolean {
146 | return activity?.javaClass?.canonicalName?.startsWith("com.fashare.testapk") ?: false
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------