├── PluginApplication
├── app
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ ├── values
│ │ │ │ │ ├── strings.xml
│ │ │ │ │ ├── colors.xml
│ │ │ │ │ └── styles.xml
│ │ │ │ ├── mipmap-hdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-mdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-anydpi-v26
│ │ │ │ │ ├── ic_launcher.xml
│ │ │ │ │ └── ic_launcher_round.xml
│ │ │ │ ├── layout
│ │ │ │ │ └── activity_main.xml
│ │ │ │ ├── drawable-v24
│ │ │ │ │ └── ic_launcher_foreground.xml
│ │ │ │ └── drawable
│ │ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── ctrip
│ │ │ │ └── pluginapplication
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── ProxyActivity.java
│ │ │ │ └── PluginManager.java
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── ctrip
│ │ │ │ └── pluginapplication
│ │ │ │ └── ExampleUnitTest.kt
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── ctrip
│ │ │ └── pluginapplication
│ │ │ └── ExampleInstrumentedTest.kt
│ ├── proguard-rules.pro
│ └── build.gradle
├── standard
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ └── values
│ │ │ │ │ └── strings.xml
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── ctrip
│ │ │ │ └── standard
│ │ │ │ └── AppInterface.java
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── ctrip
│ │ │ │ └── standard
│ │ │ │ └── ExampleUnitTest.java
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── ctrip
│ │ │ └── standard
│ │ │ └── ExampleInstrumentedTest.java
│ ├── proguard-rules.pro
│ └── build.gradle
├── thirdpartyapplication
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ ├── values
│ │ │ │ │ ├── strings.xml
│ │ │ │ │ ├── colors.xml
│ │ │ │ │ └── styles.xml
│ │ │ │ ├── drawable
│ │ │ │ │ ├── img.jpeg
│ │ │ │ │ ├── img2.jpeg
│ │ │ │ │ └── ic_launcher_background.xml
│ │ │ │ ├── mipmap-hdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-mdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-anydpi-v26
│ │ │ │ │ ├── ic_launcher.xml
│ │ │ │ │ └── ic_launcher_round.xml
│ │ │ │ ├── layout
│ │ │ │ │ └── activity_main.xml
│ │ │ │ └── drawable-v24
│ │ │ │ │ └── ic_launcher_foreground.xml
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ │ └── ctrip
│ │ │ │ │ └── thirdpartyapplication
│ │ │ │ │ ├── MainActivity.kt
│ │ │ │ │ └── BaseActivity.java
│ │ │ └── AndroidManifest.xml
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── ctrip
│ │ │ │ └── thirdpartyapplication
│ │ │ │ └── ExampleUnitTest.kt
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── ctrip
│ │ │ └── thirdpartyapplication
│ │ │ └── ExampleInstrumentedTest.kt
│ ├── proguard-rules.pro
│ └── build.gradle
├── settings.gradle
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── .gitignore
├── .idea
│ ├── encodings.xml
│ ├── vcs.xml
│ ├── runConfigurations.xml
│ ├── modules.xml
│ ├── gradle.xml
│ └── misc.xml
├── build.gradle
├── gradle.properties
├── gradlew.bat
└── gradlew
├── .DS_Store
└── README.md
/PluginApplication/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/PluginApplication/standard/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/.DS_Store
--------------------------------------------------------------------------------
/PluginApplication/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':standard', ':app', ':thirdpartyapplication'
2 |
--------------------------------------------------------------------------------
/PluginApplication/standard/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Standard
3 |
4 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PluginApplication
3 |
4 |
--------------------------------------------------------------------------------
/PluginApplication/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/PluginApplication/standard/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ThirdPartyApplication
3 |
4 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/drawable/img.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/drawable/img.jpeg
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/drawable/img2.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/drawable/img2.jpeg
--------------------------------------------------------------------------------
/PluginApplication/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/PluginApplication/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/colinNaive/PluginApplication/HEAD/PluginApplication/thirdpartyapplication/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/PluginApplication/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Mar 03 16:48:36 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-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/test/java/com/ctrip/pluginapplication/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.ctrip.pluginapplication
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/test/java/com/ctrip/thirdpartyapplication/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.ctrip.thirdpartyapplication
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/PluginApplication/standard/src/test/java/com/ctrip/standard/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.standard;
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 | }
--------------------------------------------------------------------------------
/PluginApplication/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/PluginApplication/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/PluginApplication/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.2.21'
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.0.1'
11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/PluginApplication/standard/src/main/java/com/ctrip/standard/AppInterface.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.standard;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 |
7 | /**
8 | * @author Zhenhua on 2018/3/7.
9 | * @email zhshan@ctrip.com ^.^
10 | */
11 |
12 | public interface AppInterface {
13 | void setContentView(int layoutResID);
14 |
15 | T findViewById(int id);
16 |
17 | void onCreate(Bundle savedInstanceState);
18 |
19 | void onStart();
20 |
21 | void onResume();
22 |
23 | void onDestroy();
24 |
25 | void onPause();
26 |
27 | void onSaveInstanceState(Bundle outState);
28 |
29 | /**
30 | * 需要宿主app注入给插件app上下文
31 | */
32 | void attach(Activity activity);
33 | }
34 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/java/com/ctrip/thirdpartyapplication/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.ctrip.thirdpartyapplication
2 |
3 | import android.os.Bundle
4 | import android.widget.ImageView
5 | import android.widget.Toast
6 |
7 | class MainActivity : BaseActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 | setContentView(R.layout.activity_main)
12 | /**
13 | * 这里不能直接使用findViewById,因为该方法需要一个上下文,
14 | * 而thirdPartyApp是插件,是没有被安装的,是没有上下文的
15 | * 所以,需要重新findViewById,让宿主app来实现
16 | */
17 | var img = findViewById(R.id.img)
18 | img.setOnClickListener({
19 | Toast.makeText(that, "点击啦", Toast.LENGTH_SHORT).show()
20 | })
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/PluginApplication/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/androidTest/java/com/ctrip/pluginapplication/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.ctrip.pluginapplication
2 |
3 | import android.support.test.InstrumentationRegistry
4 | import android.support.test.runner.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getTargetContext()
22 | assertEquals("com.ctrip.pluginapplication", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/PluginApplication/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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 | android.enableAapt2=false
19 |
--------------------------------------------------------------------------------
/PluginApplication/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/PluginApplication/standard/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 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/androidTest/java/com/ctrip/thirdpartyapplication/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.ctrip.thirdpartyapplication
2 |
3 | import android.support.test.InstrumentationRegistry
4 | import android.support.test.runner.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getTargetContext()
22 | assertEquals("com.ctrip.thirdpartyapplication", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/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 |
--------------------------------------------------------------------------------
/PluginApplication/standard/src/androidTest/java/com/ctrip/standard/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.standard;
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.ctrip.standard.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/PluginApplication/standard/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 15
10 | targetSdkVersion 26
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(include: ['*.jar'], dir: 'libs')
29 | implementation 'com.android.support:appcompat-v7:26.1.0'
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
32 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
33 | }
34 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/PluginApplication/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 |
5 | apply plugin: 'kotlin-android-extensions'
6 |
7 | android {
8 | compileSdkVersion 26
9 | defaultConfig {
10 | applicationId "com.ctrip.pluginapplication"
11 | minSdkVersion 15
12 | targetSdkVersion 26
13 | versionCode 1
14 | versionName "1.0"
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation project(':standard')
27 | implementation fileTree(include: ['*.jar'], dir: 'libs')
28 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
29 | implementation 'com.android.support:appcompat-v7:26.1.0'
30 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
31 | testImplementation 'junit:junit:4.12'
32 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
34 | }
35 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 | android {
5 | compileSdkVersion 26
6 |
7 |
8 |
9 | defaultConfig {
10 | applicationId "com.ctrip.thirdpartyapplication"
11 | minSdkVersion 15
12 | targetSdkVersion 26
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
17 |
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 |
27 | }
28 |
29 | dependencies {
30 | implementation project(':standard')
31 | implementation fileTree(include: ['*.jar'], dir: 'libs')
32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
33 | implementation 'com.android.support:appcompat-v7:26.1.0'
34 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
35 | testImplementation 'junit:junit:4.12'
36 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
37 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
38 | }
39 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/java/com/ctrip/pluginapplication/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.pluginapplication;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.os.Environment;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 |
10 | import java.io.File;
11 |
12 | /**
13 | * @author Zhenhua on 2018/3/7.
14 | * @email zhshan@ctrip.com ^.^
15 | */
16 |
17 | public class MainActivity extends AppCompatActivity {
18 | @Override
19 | protected void onCreate(@Nullable Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 | findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
23 | @Override
24 | public void onClick(View view) {
25 | click();
26 | }
27 | });
28 | findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
29 | @Override
30 | public void onClick(View view) {
31 | load();
32 | }
33 | });
34 | PluginManager.getInstance().setContext(this);
35 | }
36 |
37 | //事件绑定load
38 | private void load() {
39 | /**
40 | * 事先放置到SD卡根目录的plugin.apk
41 | * 现实场景中是有服务端下发
42 | */
43 | File file = new File(Environment.getExternalStorageDirectory().getPath() + "/plugin_test", "plugin.apk");
44 | PluginManager.getInstance().loadPath(file.getAbsoluteFile().getPath());
45 | }
46 |
47 | //事件绑定click
48 | private void click() {
49 | /**
50 | * 点击跳往插件app的activity,一律跳转到PRoxyActivity
51 | */
52 | Intent intent = new Intent(this, ProxyActivity.class);
53 | intent.putExtra("className", PluginManager.getInstance().getEntryName());
54 | startActivity(intent);
55 |
56 | }
57 | }
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/PluginApplication/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 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/java/com/ctrip/pluginapplication/ProxyActivity.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.pluginapplication;
2 |
3 | import android.app.Activity;
4 | import android.content.res.Resources;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.AppCompatActivity;
8 |
9 | import com.ctrip.standard.AppInterface;
10 |
11 | import java.lang.reflect.Constructor;
12 | import java.lang.reflect.InvocationTargetException;
13 |
14 | /**
15 | * @author Zhenhua on 2018/3/7.
16 | * @email zhshan@ctrip.com ^.^
17 | */
18 |
19 | public class ProxyActivity extends Activity {
20 | /**
21 | * 要跳转的activity的name
22 | */
23 | private String className = "";
24 | private AppInterface appInterface = null;
25 |
26 | @Override
27 | protected void onCreate(@Nullable Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | /**
30 | * step1:得到插件app的activity的className
31 | */
32 | className = getIntent().getStringExtra("className");
33 | /**
34 | * step2:通过反射拿到class,
35 | * 但不能用以下方式
36 | * classLoader.loadClass(className)
37 | * Class.forName(className)
38 | * 因为插件app没有被安装!
39 | * 这里我们调用我们重写过多classLoader
40 | */
41 | try {
42 | Class activityClass = getClassLoader().loadClass(className);
43 | Constructor constructor = activityClass.getConstructor();
44 | Object instance = constructor.newInstance();
45 |
46 | appInterface = (AppInterface) instance;
47 | appInterface.attach(this);
48 | Bundle bundle = new Bundle();
49 | appInterface.onCreate(bundle);
50 | } catch (ClassNotFoundException e) {
51 | e.printStackTrace();
52 | } catch (NoSuchMethodException e) {
53 | e.printStackTrace();
54 | } catch (InstantiationException e) {
55 | e.printStackTrace();
56 | } catch (IllegalAccessException e) {
57 | e.printStackTrace();
58 | } catch (InvocationTargetException e) {
59 | e.printStackTrace();
60 | }
61 | }
62 |
63 | @Override
64 | protected void onStart() {
65 | super.onStart();
66 | appInterface.onStart();
67 | }
68 |
69 |
70 | @Override
71 | protected void onResume() {
72 | super.onResume();
73 | appInterface.onResume();
74 | }
75 |
76 | @Override
77 | protected void onDestroy() {
78 | super.onDestroy();
79 | appInterface.onDestroy();
80 | }
81 |
82 | @Override
83 | public ClassLoader getClassLoader() {
84 | //不用系统的ClassLoader,用dexClassLoader加载
85 | return PluginManager.getInstance().getDexClassLoader();
86 | }
87 |
88 | @Override
89 | public Resources getResources() {
90 | //不用系统的resources,自己实现一个resources
91 | return PluginManager.getInstance().getResources();
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/PluginApplication/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | Android > Lint > Correctness
36 |
37 |
38 | Android > Lint > Performance
39 |
40 |
41 | Data flow issuesJava
42 |
43 |
44 | Java
45 |
46 |
47 |
48 |
49 | Android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/java/com/ctrip/pluginapplication/PluginManager.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.pluginapplication;
2 |
3 | import android.content.Context;
4 | import android.content.pm.PackageInfo;
5 | import android.content.pm.PackageManager;
6 | import android.content.res.AssetManager;
7 | import android.content.res.Resources;
8 |
9 | import java.io.File;
10 | import java.lang.reflect.Method;
11 |
12 | import dalvik.system.DexClassLoader;
13 |
14 | /**
15 | * @author Zhenhua on 2018/3/7.
16 | * @email zhshan@ctrip.com ^.^
17 | */
18 |
19 | public class PluginManager {
20 | //-------1:构建单例类start--------
21 | private static PluginManager instance = new PluginManager();
22 |
23 | public static PluginManager getInstance() {
24 | return instance;
25 | }
26 | //-------1:构建单例类end--------
27 |
28 | private Context context;
29 |
30 | public void setContext(Context context) {
31 | this.context = context.getApplicationContext();
32 | }
33 |
34 | public void loadPath(String path) {
35 | setEntryName(path);
36 | setClassLoader(path);
37 | setResources(path);
38 | }
39 |
40 | //-------2:获取插件app入口activity name start--------
41 | private void setEntryName(String path) {
42 | //得到packageManager来获取包信息
43 | PackageManager packageManager = context.getPackageManager();
44 | //参数一是apk的路径,参数二是希望得到的内容
45 | PackageInfo packageInfo = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
46 | //得到插件app的入口activity名称
47 | entryName = packageInfo.activities[0].name;
48 | }
49 |
50 | private String entryName;
51 |
52 | public String getEntryName() {
53 | return entryName;
54 | }
55 | //-------2:获取插件app入口activity name end--------
56 |
57 |
58 | //-------3:构造classLoader start-------------
59 | private DexClassLoader dexClassLoader;
60 |
61 | private void setClassLoader(String path) {
62 | //dex的缓存路径
63 | File dexOutFile = context.getDir("dex", Context.MODE_PRIVATE);
64 | dexClassLoader = new DexClassLoader(path, dexOutFile.getAbsoluteFile().getAbsolutePath(), null, context.getClassLoader());
65 | }
66 |
67 | public DexClassLoader getDexClassLoader() {
68 | return dexClassLoader;
69 | }
70 | //-------3:构造classLoader end-------------
71 |
72 | //-------4:构造resources start--------
73 | private Resources resources;
74 |
75 | public Resources getResources() {
76 | return resources;
77 | }
78 |
79 | public void setResources(String path) {
80 | //由于构建resources必须要传入AssetManager,这里先构建一个AssetManager
81 | try {
82 | AssetManager assetManager = AssetManager.class.newInstance();
83 | Method addAssetPath = AssetManager.class.getMethod("addAssetPath", String.class);
84 | addAssetPath.invoke(assetManager, path);
85 |
86 | resources = new Resources(assetManager, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
87 | } catch (Exception e) {
88 | }
89 | }
90 | //-------4:构造resources end--------
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/java/com/ctrip/thirdpartyapplication/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.ctrip.thirdpartyapplication;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.app.Activity;
5 | import android.os.Bundle;
6 | import android.support.annotation.NonNull;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.Window;
10 | import android.view.WindowManager;
11 |
12 | import com.ctrip.standard.AppInterface;
13 |
14 | import org.jetbrains.annotations.NotNull;
15 | import org.jetbrains.annotations.Nullable;
16 |
17 | /**
18 | * @author Zhenhua on 2018/3/3.
19 | * @email zhshan@ctrip.com ^.^
20 | */
21 |
22 | public class BaseActivity extends Activity implements AppInterface {
23 |
24 | protected Activity that;
25 |
26 | @Override
27 | public void attach(@NotNull Activity activity) {
28 | //上下文注入进来了
29 | this.that = activity;
30 | }
31 |
32 | /**
33 | * super.setContentView(layoutResID)最终调用的是系统给我们注入的上下文
34 | */
35 | @Override
36 | public void setContentView(int layoutResID) {
37 | if (that == null) {
38 | super.setContentView(layoutResID);
39 | } else {
40 | that.setContentView(layoutResID);
41 | }
42 | }
43 |
44 | @Override
45 | public T findViewById(int id) {
46 | if (that == null) {
47 | return super.findViewById(id);
48 | } else {
49 | return that.findViewById(id);
50 | }
51 |
52 | }
53 |
54 | @Override
55 | public void onSaveInstanceState(Bundle outState) {
56 |
57 | }
58 |
59 | @Override
60 | public ClassLoader getClassLoader() {
61 | if (that == null) {
62 | return super.getClassLoader();
63 | } else {
64 | return that.getClassLoader();
65 | }
66 | }
67 |
68 | @NonNull
69 | @Override
70 | public LayoutInflater getLayoutInflater() {
71 | if (that == null) {
72 | return super.getLayoutInflater();
73 | } else {
74 | return that.getLayoutInflater();
75 | }
76 | }
77 |
78 |
79 | @Override
80 | public WindowManager getWindowManager() {
81 | if (that == null) {
82 | return super.getWindowManager();
83 | } else {
84 | return that.getWindowManager();
85 | }
86 | }
87 |
88 | @Override
89 | public Window getWindow() {
90 | if (that == null) {
91 | return super.getWindow();
92 | } else {
93 | return that.getWindow();
94 | }
95 | }
96 |
97 | public void onCreate(@Nullable Bundle savedInstanceState) {
98 | if (that == null) {
99 | super.onCreate(savedInstanceState);
100 | } else {
101 | // that.onCreate(savedInstanceState);
102 | }
103 | }
104 |
105 | public void onStart() {
106 | if (that == null) {
107 | super.onStart();
108 | } else {
109 | // that.onStart();
110 | }
111 | }
112 |
113 | public void onDestroy() {
114 | if (that == null) {
115 | super.onDestroy();
116 | } else {
117 | // that.onDestroy();
118 | }
119 | }
120 |
121 | public void onPause() {
122 | if (that == null) {
123 | super.onPause();
124 | } else {
125 | // that.onPause();
126 | }
127 | }
128 |
129 | public void onResume() {
130 | if (that == null) {
131 | super.onResume();
132 | } else {
133 | // that.onResume();
134 | }
135 | }
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/PluginApplication/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 |
--------------------------------------------------------------------------------
/PluginApplication/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/PluginApplication/thirdpartyapplication/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ###背景
2 | Android插件化作为每个合格的Android程序员都必须会的技术,被各大厂广泛使用。随着各大厂对移动互联网的垄断,我们渐渐发现app集成的功能越来越多。比如如下几个app(携程、淘宝、支付宝):
3 |
4 | 可以看到每一个app都被集成了无数的功能入口,就拿淘宝来说,“天猫”、“外卖”、“飞猪”、“拍卖”,这任何一个入口都其实是一个app,只不过被集成到“淘宝”这个入口里了。如果没有插件化技术,很难想象淘宝app的size会有多大。很可能有几个GB!!
5 |
6 | 再来看看支付宝,可以发现支付宝中提供了很多第三方app的入口,而点击这些入口跳转的也都是native页面。应用市场上的支付宝app一共只有二三十MB,而如果这些app都集成到支付宝中,那支付宝的size就不是二三十MB了,那就是二三十GB了!!
7 |
8 | 本篇blog的主题是介绍Android插件化技术,并且会提供一个仿支付宝插件化技术的demo,告诉你**支付宝是如何把一个第三方app作为插件集成到自己的app里的**。
9 |
10 | ###插件化好处
11 | 1. 宿主和插件分开编译
12 | 编译时只需要编译宿主app,插件app是在编译好后下发到宿主app里的。
13 | 2. 并发开发
14 | 宿主app什么时候发布版本跟插件app什么时候开发完没有关系,宿主app只要开发完并且为插件app提供一个入口就可以了。
15 | 3. 动态更新插件
16 | 插件app在开发完后下发到宿主app里,点击相应的入口就可以跳转到最新版的插件app了。
17 | 4. 按需下载模块
18 | 5. 解决方法数或变量数爆棚
19 |
20 | ###随便一个app都能集成到支付宝吗?
21 | 答案是:不能!
22 | 我们来思考,支付宝要跳转到一个插件的Activity,而**插件是没有被安装的,它没有上下文,也就没有生命周期,那么插件Activity的生命周期就要由宿主app来控制**。为此,我们需要建立一套**标准**。
23 | 话不多说,我们直接开始撸代码。
24 |
25 | ###插件app的activity没有在宿主app中注册,该怎么办?
26 | 插桩,一个空的Activity,专门用来加载插件app中的activity,这个Activity叫ProxyActivity,后面我会具体去讲这个空Activity该如何实现。我们只需要在宿主app里注册这个Activity就可以了。
27 |
28 |
29 | ###加载插件app中的Activity
30 | 1. 实际场景中插件apk肯定是由服务端下发后,保存到SD卡的某个文件夹下。这里将编译好的插件apk放到手机外置SD卡的根目录中,我们来演示宿主app如何去加载插件app中的Activity。
31 | 
32 |
33 | 2. 加载该Activity的**类**
34 | 接下来我们来看下FluginManager的loadPath方法如何实现。如果要实现这个功能,首先想到的肯定是用反射。
35 |
36 | 可是你别忘记了,插件app根本就没有安装,这里是无法找到这个Class的。我们需要DexClassLoader来完成Activity类的加载。
37 | PluginManager的getDexClassLoader的实现如下:
38 | 
39 |
40 | 3. 加载该Activity的**资源文件**
41 | 讲完了如何加载Activity,我们来讲下如何加载Activity中用到的资源文件。我们在日常开发中需要资源文件时,我们是通过getResources()来获取。例如加载一个图片:
42 |
43 | ```
44 | getResources().getDrawable()
45 | ```
46 | 可现在我们需要获取的是另外一个app的资源,所以这里就需要自己实现一个getResources()方法。
47 | PluginManager的getResources方法实现如下:
48 | 
49 |
50 | 至此,一个插件app的activity加载功能就实现完成了,下面我们来看如何跳转。
51 |
52 | ###跳转到插件app中的Activity
53 | 
54 | 由于我们需要读取SD卡中的插件apk,这里别忘记加上SD卡的读写权限
55 | 
56 | 这就可以实现跳转了。下面我们来看下效果
57 |
58 | 奇怪,为什么我们跳转到插件app的activity是空白的?我们来看下插件app的activity应该长什么样子。
59 |
60 | 当然这里我只在插件app的主activity里放了一张图片,并没有写复杂的布局。可是为什么我们跳过来的是空白页呢?我们再看下ProxyActivity的代码:
61 | ```
62 | package com.ctrip.pluginapplication
63 |
64 | import android.content.res.Resources
65 | import android.os.Bundle
66 | import android.support.v7.app.AppCompatActivity
67 |
68 | /**
69 | * 壳!专门用来加载插件Activity
70 | * @author Zhenhua on 2018/3/3.
71 | * @email zhshan@ctrip.com ^.^
72 | */
73 | class ProxyActivity : AppCompatActivity() {
74 |
75 | /**
76 | * 要跳转的activity的name
77 | */
78 | private var className = ""
79 |
80 | override fun onCreate(savedInstanceState: Bundle?) {
81 | super.onCreate(savedInstanceState)
82 | /**
83 | * step1:得到插件app的activity的className
84 | */
85 | className = intent.getStringExtra("className")
86 | /**
87 | * step2:通过反射拿到class,
88 | * 但不能用以下方式,因为插件app没有被安装!
89 | */
90 | // classLoader.loadClass(className)
91 | // Class.forName(className)
92 |
93 |
94 | }
95 |
96 | override fun getClassLoader(): ClassLoader {
97 | //不用系统的ClassLoader,用dexClassLoader加载
98 | return PluginManager.getInstance().getDexClassLoader() as? ClassLoader
99 | ?: super.getClassLoader()
100 | }
101 |
102 | override fun getResources(): Resources {
103 | //不用系统的resources,自己实现一个resources
104 | return PluginManager.getInstance().getResources() ?: super.getResources()
105 | }
106 | }
107 | ```
108 | 我们发现,我们这里还没有在ProxyActivity里写逻辑啊,我们只是得到了插件app的主activity的name,这时activity还没有生命周期。?我们接着来实现。我们需要让ProxyActivity控制插件app的activity的生命周期,所以我们需要得到插件app的activity的实例,然后去控制其生命周期:
109 | ```
110 | package com.ctrip.pluginapplication
111 |
112 | import android.content.res.Resources
113 | import android.os.Bundle
114 | import android.support.v7.app.AppCompatActivity
115 | import com.ctrip.standard.AppInterface
116 |
117 | /**
118 | * 壳!专门用来加载插件Activity
119 | * @author Zhenhua on 2018/3/3.
120 | * @email zhshan@ctrip.com ^.^
121 | */
122 | class ProxyActivity : AppCompatActivity() {
123 |
124 | /**
125 | * 要跳转的activity的name
126 | */
127 | private var className = ""
128 | private var appInterface: AppInterface? = null
129 |
130 | override fun onCreate(savedInstanceState: Bundle?) {
131 | super.onCreate(savedInstanceState)
132 | /**
133 | * step1:得到插件app的activity的className
134 | */
135 | className = intent.getStringExtra("className")
136 | /**
137 | * step2:通过反射拿到class,
138 | * 但不能用以下方式
139 | * classLoader.loadClass(className)
140 | * Class.forName(className)
141 | * 因为插件app没有被安装!
142 | * 这里我们调用我们重写过多classLoader
143 | */
144 | var activityClass = classLoader.loadClass(className)
145 | var constructor = activityClass.getConstructor()
146 | var instance = constructor.newInstance()
147 |
148 | appInterface = instance as?AppInterface
149 | appInterface?.attach(this)
150 | var bundle = Bundle()
151 | appInterface?.onCreate(bundle)
152 |
153 | }
154 |
155 | override fun onStart() {
156 | super.onStart()
157 | appInterface?.onStart()
158 | }
159 |
160 | override fun onResume() {
161 | super.onResume()
162 | appInterface?.onResume()
163 | }
164 |
165 | override fun onDestroy() {
166 | super.onDestroy()
167 | appInterface?.onDestroy()
168 | }
169 |
170 | override fun getClassLoader(): ClassLoader {
171 | //不用系统的ClassLoader,用dexClassLoader加载
172 | return PluginManager.getInstance().getDexClassLoader() as? ClassLoader
173 | ?: super.getClassLoader()
174 | }
175 |
176 | override fun getResources(): Resources {
177 | //不用系统的resources,自己实现一个resources
178 | return PluginManager.getInstance().getResources() ?: super.getResources()
179 | }
180 | }
181 | ```
182 | 这时我们就可以成功跳转了。ok,插件化实现完成。我们来看下效果。
183 |
184 | 这里**[附上demo(点击下载)](https://github.com/colinNaive/PluginApplication)**,如有任何疑问可留言提问,博主每天都会查看。
185 |
186 | ~~~~~~~~华丽丽的分割线:在插件app中实现更多功能~~~~~~~
187 | 之前我们的插件app的activity其实就只是加载了一个imageView,我们现在来实现这样一个功能:“点击ImageView,弹出一个toast”。
188 | 代码如下:
189 | 
190 | 我们来看下效果。。
191 | 然而,点击竟然crash了。我们来贴下错误日志:
192 | 
193 | 原来我们在插件Activity中不能用自己的上下文,我们应该用that!!
194 |
195 | 代码已经[**更新到github**](https://github.com/colinNaive/PluginApplication)上,欢迎下载体验。
--------------------------------------------------------------------------------