{"id":1834,"date":"2023-03-29T21:44:55","date_gmt":"2023-03-29T13:44:55","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1834"},"modified":"2023-04-22T09:18:11","modified_gmt":"2023-04-22T01:18:11","slug":"android-plugin-launching-activity","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/29\/android-plugin-launching-activity\/","title":{"rendered":"Android\u63d2\u4ef6\u5316\u542f\u52a8Activity"},"content":{"rendered":"<p>\u652f\u4ed8\u5b9dAPP\u672c\u8eab\u66f4\u50cf\u662f\u4e00\u4e2a&quot;\u7a7a\u58f3&quot;\uff0c\u91cc\u9762\u53ef\u4ee5\u642d\u8f7d\u5f88\u591a\u5c0f\u529f\u80fd\uff0c\u8fd9\u4e9b\u5c0f\u529f\u80fd\u90fd\u662f\u4ee5&quot;\u63d2\u4ef6&quot;\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u652f\u6301\u5c0f\u529f\u80fd\u7684\u7075\u6d3b\u914d\u7f6e\uff0c\u7528\u6237\u4e0d\u60f3\u8981\u67d0\u4e2a\u529f\u80fd\uff0c\u53ef\u4ee5\u4e0d\u663e\u793a\u51fa\u6765\u3002\u63d2\u4ef6\u5316\u5f00\u53d1\u662f\u5f53\u4e0b\u5927\u578bAPP\u5fc5\u5907\u7684\u4e00\u9879\u6280\u672f\u3002<\/p>\n<h2>\u63d2\u4ef6\u5316\u5f00\u53d1\u7684\u6838\u5fc3\u96be\u70b9<\/h2>\n<p><!-- more --><\/p>\n<p>\u6839\u636e\u5f15\u5b50\u4e2d\u6240\u8bf4\uff0c\u652f\u4ed8\u5b9d\u4e2d\u5404\u79cd\u5404\u6837\u7684\u529f\u80fd\uff0c\u90fd\u662f\u63d2\u4ef6\u5f62\u5f0f\u5b58\u5728\u7684\uff0c\u90a3\u4e48\u5177\u4f53\u662f\u5982\u4f55\u5b58\u5728\uff1f<br \/>\n\u6211\u4eec\u6240\u8bf4\u7684\u63d2\u4ef6\uff0c\u5176\u5b9e\u662fapk\u6587\u4ef6\uff0c\u5373<code>xxx.apk<\/code><br \/>\n\u63d2\u4ef6\u5316\u5f00\u53d1\u7684\u5957\u8def: \u5916\u58f3<code>app module<\/code> + \u591a\u4e2a\u63d2\u4ef6<code>plugin module<\/code> + \u63d2\u4ef6\u6846\u67b6\u5c42<code>library module<\/code><\/p>\n<ul>\n<li>\u5916\u58f3app \u8d1f\u8d23\u6574\u4e2aapp\u7684\u5916\u90e8\u67b6\u6784\uff0c\u5e76\u4e14\u7ed9\u63d2\u4ef6\u63d0\u4f9b\u5165\u53e3\u7ec4\u4ef6\uff08\u6bd4\u5982\uff0c\u7528\u4e00\u4e2abutton\u4f5c\u4e3a&quot;\u4f59\u989d\u5b9d&quot;\u7684\u5165\u53e3\uff0c\u70b9\u51fbbutton\uff0c\u8fdb\u5165&quot;\u4f59\u989d\u5b9d&quot;\uff09<\/li>\n<li>\u591a\u4e2a\u63d2\u4ef6<code>plugin module<\/code>\uff0c\u8d1f\u8d23\u5206\u5f00\u5f00\u53d1\u5404\u4e2a\u529f\u80fd\u3002\u4e25\u683c\u6765\u8bf4\uff0c\u6bcf\u4e2a\u529f\u80fd\u5fc5\u987b\u53ef\u4ee5\u5355\u72ec\u8fd0\u884c\uff0c\u4e5f\u5fc5\u987b\u652f\u6301\u96c6\u6210\u5230\u5916\u58f3app\u65f6\u8fd0\u884c<\/li>\n<li>\u63d2\u4ef6\u6846\u67b6\u5c42<code>library module<\/code>\uff0c\u6240\u6709\u63d2\u4ef6\u5316\u7684\u6838\u5fc3\u4ee3\u7801\uff0c\u90fd\u96c6\u4e2d\u5230\u8fd9\u91cc\u3002\u5e76\u4e14\u8fd9\u4e2alibrary\u8981\u540c\u65f6\u88ab\u5916\u58f3app\u548c\u63d2\u4ef6module\u5f15\u7528<\/li>\n<\/ul>\n<h2>\u63d2\u4ef6\u5316\u6240\u9700\u7684\u6280\u672f\u7406\u8bba\u57fa\u7840<\/h2>\n<p>\u5b66\u4e60\u63d2\u4ef6\u5316\u5f00\u53d1\uff0c\u9996\u5148\u8981\u4e86\u89e3<\/p>\n<h3>Activity\u662f\u5982\u4f55\u542f\u52a8\u7684<\/h3>\n<p>\u5728Activity\u91cc\uff0c\u5f00\u542f\u53e6\u4e00\u4e2aActivity\uff0c\u4f7f\u7528<code>startActivity<\/code>\u5373\u53ef\uff0c\u4f46\u662f<code>startActivity<\/code>\u4e4b\u540e\uff0c\u7cfb\u7edf\u505a\u4e86\u4ec0\u4e48\uff1f<\/p>\n<p>\u5f00\u59cb\u8ffd\u8e2a\u6e90\u7801\uff08\u6e90\u7801\u8ffd\u8e2a\u57fa\u4e8eSDK 28 &#8211; 9.0\uff09\uff1a<\/p>\n<pre><code class=\"language-java\">public class MainActivity extends AppCompatActivity() {\n    private void xxxx() {\n        Intent i = new Intent(this, XXXActivity.class);\n        startActivity(i);\n    }\n}<\/code><\/pre>\n<p>\u90a3\u4e48\uff0c<code>startActivity<\/code>\u5230\u5e95\u505a\u4e86\u4ec0\u4e48\uff0c\u70b9\u8fdb\u53bb\u770b\u627e\u5230\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">@Override\npublic void startActivity(Intent intent, @Nullable Bundle options) {\n    if (options != null) {\n        startActivityForResult(intent, -1, options);\n    } else {\n        \/\/ Note we want to go through this call for compatibility with\n        \/\/ applications that may have overridden the method.\n        startActivityForResult(intent, -1);\n    }\n}<\/code><\/pre>\n<p>\u7ee7\u7eed\uff0c\u8ffd\u8e2a\u8fd9\u4e24\u4e2a<code>startActivityForResult<\/code>\uff0c\u76f4\u63a5\u5230\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,\n        @Nullable Bundle options) {\n    if (mParent == null) {\n        options = transferSpringboardActivityOptions(options);\n        Instrumentation.ActivityResult ar =\n            mInstrumentation.execStartActivity(\n                this, mMainThread.getApplicationThread(), mToken, this,\n                intent, requestCode, options); \/\/\u6ce8\u610f\u770b\u8fd9\u91cc\uff0cmInstrumentation.execStartActivity\n        if (ar != null) {\n            mMainThread.sendActivityResult(\n                mToken, mEmbeddedID, requestCode, ar.getResultCode(),\n                ar.getResultData());\n        }\n        if (requestCode &gt;= 0) {\n           \u00b7\u00b7\u00b7\n            mStartedActivity = true;\n        }\n\n        cancelInputsAndStartExitTransition(options);\n        \/\/ TODO Consider clearing\/flushing other event sources and events for child windows.\n    } else {\n        if (options != null) {\n            mParent.startActivityFromChild(this, intent, requestCode, options);\n        } else {\n            \/\/ Note we want to go through this method for compatibility with\n            \/\/ existing applications that may have overridden it.\n            mParent.startActivityFromChild(this, intent, requestCode);\n        }\n    }\n}<\/code><\/pre>\n<p><code>mInstrumentation.execStartActivity<\/code>\u5728\u8fd9\u91cc\u88ab\u6267\u884c\uff0c\u7136\u800c\u53e6\u4e00\u4e2a\u5206\u652f<code>mParent<\/code>\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u4f1a\u6267\u884c<code>mParent.startActivityFromChild<\/code><\/p>\n<pre><code class=\"language-java\">public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,\n        int requestCode, @Nullable Bundle options) {\n    options = transferSpringboardActivityOptions(options);\n    Instrumentation.ActivityResult ar =\n        mInstrumentation.execStartActivity(\n            this, mMainThread.getApplicationThread(), mToken, child,\n            intent, requestCode, options); \/\/\u7136\u800c\uff0c\u8fd9\u91cc\u8fd8\u662f\u6267\u884c\u4e86mInstrumentation.execStartActivity\n    if (ar != null) {\n        mMainThread.sendActivityResult(\n            mToken, child.mEmbeddedID, requestCode,\n            ar.getResultCode(), ar.getResultData());\n    }\n    cancelInputsAndStartExitTransition(options);\n}<\/code><\/pre>\n<p>\u7136\u800c\uff0c\u8fd9\u91cc\u8fd8\u662f\u6267\u884c\u4e86<code>mInstrumentation.execStartActivity<\/code>\uff0c\u7efc\u4e0a\u6240\u8ff0\uff0c<code>startActivity<\/code>\u6700\u7ec8\u90fd\u4f1a\u6267\u884c\u5230<code>mInstrumentation.execStartActivity<\/code>\uff0c\u90a3\u4e48\u7ee7\u7eed\u8ddf\u8e2a\u8fd9\u4e2a<code>execStartActivity<\/code><\/p>\n<pre><code class=\"language-java\">public ActivityResult execStartActivity(\n        Context who, IBinder contextThread, IBinder token, Activity target,\n        Intent intent, int requestCode, Bundle options) {\n        ...\u7701\u7565\u4e00\u5927\u6bb5...\n    try {\n        intent.migrateExtraStreamToClipData();\n        intent.prepareToLeaveProcess(who);\n        int result = ActivityManager.getService() \/\/\u8fd9\u4e2a\u4e0d\u5c31\u662f\u5927\u540d\u9f0e\u9f0e\u7684AMS\u4e48\n            .startActivity(whoThread, who.getBasePackageName(), intent,\n                    intent.resolveTypeIfNeeded(who.getContentResolver()),\n                    token, target != null ? target.mEmbeddedID : null,\n                    requestCode, 0, null, options);\n        checkStartActivityResult(result, intent);\n    } catch (RemoteException e) {\n        throw new RuntimeException(&quot;Failure from system&quot;, e);\n    }\n    return null;\n}<\/code><\/pre>\n<p>\u6ce8\u610f\u8fd9\u91cc\u6709\u4e00\u4e2a<code>ActivityManager.getService()<\/code>\uff0c\u5176\u5b9e\u5c31\u662f\u5b89\u5353\u91cc\u5927\u540d\u9f0e\u9f0e\u7684AMS\uff08<code>ActivityManagerService<\/code>\uff09\uff0c\u8d1f\u8d23\u5bf9 Android\u56db\u5927\u7ec4\u4ef6\uff08Activity\uff0cService\uff0cBroadcast\uff0cContentProvider\uff09\u7684\u7ba1\u7406\uff0c\u5305\u62ec\u542f\u52a8\uff0c\u751f\u547d\u5468\u671f\u7ba1\u7406\u7b49<\/p>\n<p>Activity\u91cc\u9762startActivity\u7684\u8ffd\u8e2a\u5c31\u5230\u8fd9\u91cc\u3002<\/p>\n<p>PS\uff1a\u5176\u5b9e\uff0cActivity\u4e0d\u53ea\u662f\u53ef\u4ee5\u5728Activity\u91cc\u542f\u52a8\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>getApplicationContext().startActivity()<\/code>\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u53bb\u8ffd\u8e2a\u4e00\u4e0b\uff0c\u6700\u7ec8\u7ed3\u8bba\u8fd8\u662f\u4e00\u6837\uff0c\u90fd\u4f1a\u6267\u884cAMS\u7684<code>startActivity<\/code><\/p>\n<p>\u7ed3\u8bba\uff1a\u6211\u4eec\u901a\u5e38\u5728\u81ea\u5df1\u7684Activity\u91cc\u8c03\u7528<code>startActivity<\/code>\u4e4b\u540e\uff0c\u6700\u7ec8\u4f1a\u6267\u884cAMS\u7684<code>startActivity<\/code>\uff0c\u4ece\u800c\u8ba9\u542f\u52a8\u7684\u90a3\u4e2aActivity\u5177\u6709\u751f\u547d\u5468\u671f\u3002\u90a3\u4e48\u5982\u679c\u53ea\u662f<code>new<\/code>\u4e00\u4e2aActivity\u5b9e\u4f8b\uff0c\u5b83\u4f1a\u4e0d\u4f1a\u5177\u6709\u751f\u547d\u5468\u671f\u5462\uff1f\u663e\u800c\u6613\u89c1\u4e86<\/p>\n<h3>apk\u5305\uff08\u5176\u5b9e\u662f\u538b\u7f29\u5305\uff09\u91cc\u7684\u5404\u4e2a\u6587\u4ef6\u5404\u81ea\u7684\u4f5c\u7528<\/h3>\n<p>\u5728Android Studio\u91cc\uff0c\u8fd0\u884capp\uff0c\u6216\u8005<code>gradle<\/code>\u6267\u884c<code>assemble<\/code>\u547d\u4ee4\u53ef\u4ee5\u751f\u6210apk\u6587\u4ef6\uff0c\u90a3\u4e48\u6211\u4eec\u89e3\u538bapk\u6587\u4ef6\u4e4b\u540e\uff0c\u5b83\u91cc\u9762\u7684\u5404\u79cd\u5185\u90e8\u6587\u4ef6\uff0c\u5404\u81ea\u90fd\u8d77\u5230\u4e86\u4ec0\u4e48\u4f5c\u7528\u5462\uff1f<\/p>\n<h4>classes.dex<\/h4>\n<p><code>classes.dex<\/code>\u6587\u4ef6\uff0c\u5de5\u7a0b\u91cc\u9762\u7684Java\u6e90\u7801\u7f16\u8bd1\u6253\u5305\u800c\u6210\uff0c\u5305\u542b\u4e86\u8fd9\u4e2aapk\u7684\u6240\u6709Java\u7c7b\uff0c\u6211\u4eec\u62ff\u5230\u8fd9\u4e2adex\u6587\u4ef6\uff0c\u5c31\u6709\u80fd\u529b\u53cd\u5c04\u521b\u5efa\u5176\u4e2d\u7684\u7c7b\u5bf9\u8c61<\/p>\n<h4>res\u76ee\u5f55<\/h4>\n<p>\u6240\u6709\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u5916\u58f3app\u901a\u8fc7\u8d44\u6e90\u5305\uff0c\u53ef\u4ee5\u62ff\u5230\u5305\u91cc\u9762\u7684\u4efb\u610f\u8d44\u6e90\uff0c\u5f53\u7136\u524d\u63d0\u662f\u5bbf\u4e3b\u8981\u521b\u5efa\u5bf9\u5e94\u8d44\u6e90\u5305\u7684Resources\u5bf9\u8c61<\/p>\n<h4>resources.arsc<\/h4>\n<p>res\u4e0b\u6240\u6709\u8d44\u6e90\u7684\u6620\u5c04<\/p>\n<h4>META-INF<\/h4>\n<p>app\u7b7e\u540d\u7684\u4e00\u4e9b\u4e1c\u897f<\/p>\n<h4>AndroidManifest.xml<\/h4>\n<p>\u6e05\u5355\u6587\u4ef6<\/p>\n<h2>\u6838\u5fc3\u96be\u70b9\u7684\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u5916\u58f3app\uff0c\u4f5c\u4e3a\u4e00\u4e2a&quot;\u5bbf\u4e3b&quot;\u3002\u63d2\u4ef6apk\u4e2d\u7684\u6240\u6709\u4e1c\u897f\uff0c\u65e0\u8bba\u662f<code>classes.dex<\/code>\u91cc\u7684\u7c7b\uff0c\u8fd8\u662fres\u8d44\u6e90\uff0c\u90fd\u662f&quot;\u5bbf\u4e3b&quot;\u4e4b\u5916\u7684\u4e1c\u897f\uff0c\u90a3\u4e48\u5bbf\u4e3b\u8981\u60f3\u4f7f\u7528\u81ea\u5df1\u8eab\u5916\u7684\u7c7b\u548c\u8d44\u6e90\uff0c\u9700\u8981\u89e3\u51b33\u4e2a\u95ee\u9898\uff1a<\/p>\n<h3>\u53d6\u5f97\u63d2\u4ef6\u4e2d\u7684Activity\u7684Class<\/h3>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528<code>DexClassLoader<\/code>\u5b83\u662f\u4e13\u95e8\u52a0\u8f7d\u5916\u90e8apk\u7684\u7c7b\u52a0\u8f7d\u5668<\/p>\n<h3>\u53d6\u5f97\u63d2\u4ef6\u4e2d\u7684\u8d44\u6e90<\/h3>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528hook\u6280\u672f\uff0c\u521b\u5efa\u53ea\u5c5e\u4e8e\u5916\u90e8\u63d2\u4ef6\u7684Resouces\u8d44\u6e90\u7ba1\u7406\u5668<\/p>\n<h3>\u4ee3\u7406Activity\u751f\u547d\u5468\u671f<\/h3>\n<p>\u53cd\u5c04\u521b\u5efa\u4e86\u63d2\u4ef6\u4e2d\u7684Activity\u5bf9\u8c61\uff0c\u4f46\u662f\u5b83\u662f\u6ca1\u6709\u751f\u547d\u5468\u671f\u7684\uff0c\u4e0d\u80fd\u50cf\u4f7f\u7528\u5bbf\u4e3b\u81ea\u8eab\u7684Activity\u4e00\u6837\u62e5\u6709\u5b8c\u6574\u7684\u751f\u547d\u5468\u671f<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528\u4ee3\u7406Activity\u4f5c\u4e3a\u771f\u6b63\u63d2\u4ef6Activity\u7684&quot;\u5080\u5121&quot;<\/p>\n<h2>\u6838\u5fc3\u4ee3\u7801\u7ed3\u6784<\/h2>\n<h3>app module<\/h3>\n<p>\u5916\u58f3app\u5f88\u7b80\u5355\uff0c\u552f\u4e00\u8981\u8bf4\u660e\u7684\u5c31\u662f\u63d2\u4ef6apk\uff0c\u653e\u7f6e\u5728<code>src\/main\/assets<\/code>\u76ee\u5f55\uff0c\u53ea\u662f\u4e3a\u4e86\u6f14\u793ademo\u65b9\u4fbf<\/p>\n<p><code>MyApp.java<\/code>\uff0c\u53ea\u505a\u4e86\u4e00\u4ef6\u4e8b\uff0c<code>PluginManager.getInstance().init(this);<\/code>\uff0c\u5bf9<code>PluginManager<\/code>\u8fdb\u884c\u521d\u59cb\u5316\u5e76\u4e14\u8d4b\u4e88\u4e0a\u4e0b\u6587<\/p>\n<p><code>MainActivity.java<\/code>\u53ea\u505a\u4e86\u4e24\u4ef6\u4e8b\uff1a<\/p>\n<p>\uff081\uff09\u5c06<code>assets<\/code>\u91cc\u9762\u7684apk\u6587\u4ef6\uff0c\u901a\u8fc7\u5de5\u5177\u7c7b<code>AssetUtil<\/code>\u7684<code>copyAssetToCache<\/code>\u65b9\u6cd5\uff0c\u62f7\u8d1d\u5230\u4e86app\u7684\u7f13\u5b58\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u4f7f\u7528<code>PluginManager<\/code>\u53bb\u52a0\u8f7d\u8fd9\u4e2aapk:<\/p>\n<pre><code class=\"language-java\">String path = AssetUtil.copyAssetToCache(MainActivity.this, &quot;plugin_module-debug.apk&quot;);\nPluginManager.getInstance().loadPluginApk(path);<\/code><\/pre>\n<p>\uff082\uff09\u8df3\u8f6c\u5230\u4ee3\u7406Activity\uff0c\u5e76\u4e14\u4f20\u5165\u771f\u6b63\u8981\u8df3\u7684\u76ee\u6807Activity\u7684name.<\/p>\n<pre><code class=\"language-java\">\/\/ \u5148\u8df3\u5230\u4ee3\u7406Activity\uff0c\u7531\u4ee3\u7406Activity\u5c55\u793a\u771f\u6b63\u7684Activity\u5185\u5bb9\nIntent intent = new Intent(MainActivity.this, ProxyActivity.class);\nintent.putExtra(PluginApkConst.TAG_CLASS_NAME, \n        PluginManager.getInstance().getPackageInfo().activities[0].name);\nstartActivity(intent);<\/code><\/pre>\n<h3>plugin module<\/h3>\n<p>\u63d2\u4ef6module\u5341\u5206\u7b80\u5355\uff0c\u5b83\u7684\u4f5c\u7528\uff0c\u5c31\u662f\u751f\u6210\u63d2\u4ef6apk\uff0c\u5bf9\u5b83\u8fdb\u884c\u7f16\u8bd1\u6253\u5305\uff0c\u53d6\u5f97apk\u6587\u4ef6\u5373\u53ef\u3002<\/p>\n<p>\u4e24\u4e2a\u91cd\u70b9\uff1a<\/p>\n<ul>\n<li>\u63d2\u4ef6\u4e2d\u7684\u6240\u6709Activity\uff0c\u5fc5\u987b\u90fd\u96c6\u6210\u6765\u81ea<code>plugin_lib<\/code>\u7684<code>PluginBaseActivity<\/code>\uff0c\u53ea\u6709\u7ee7\u627f\u4e86\uff0c\u624d\u5177\u6709\u63d2\u4ef6\u5316\u7279\u5f81\uff0c\u80fd\u591f\u88ab\u5916\u58f3app\u6267\u884c<code>startActivity<\/code>\u6210\u529f\u8df3\u8f6c<\/li>\n<li>\u63d2\u4ef6\u5185\u90e8\u7684Activity\u8df3\u8f6c\uff0c\u4e0a\u4e0b\u6587\uff0c\u5fc5\u987b\u4f7f\u7528<code>PluginBaseActivity<\/code>\u7684<code>proxy<\/code>\u53d8\u91cf<\/li>\n<\/ul>\n<pre><code>\/**\n * \u63d2\u4ef6\u7684\u7b2c\u4e00\u4e2aActivity\n *\/\npublic class MainActivity extends PluginBaseActivity {\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                Intent intent = new Intent(proxy, Main2Activity.class);\n                startActivity(intent);\n            }\n        });\n    }\n}<\/code><\/pre>\n<pre><code>\/**\n * \u63d2\u4ef6\u7684\u7b2c\u4e8c\u4e2aActivity\n *\/\npublic class Main2Activity extends PluginBaseActivity {\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main2);\n    }\n\n}<\/code><\/pre>\n<h3>plugin library module<\/h3>\n<p>\u524d\u9762\u4e24\u4e2amodule\u90fd\u5f88\u7b80\u5355\uff0c\u90a3\u4e48\u6838\u5fc3\u6280\u672f\u5728\u54ea\u91cc\uff1f\u7b54\u6848\u5c31\u662f\u63d2\u4ef6\u6846\u67b6\u5c42\u4ee3\u7801\uff0c\u662f\u63d2\u4ef6\u5316\u5f00\u53d1\u6280\u672f\u7684\u6838\u5fc3\u3002\u8fd9\u4e2amodule\u8981\u540c\u65f6\u88ab\u5916\u58f3app\u548c\u63d2\u4ef6module\u5f15\u7528\u3002\u5176\u4e2d\uff0c3\u4e2a\u6280\u672f\u8981\u70b9\uff1a<\/p>\n<h4>PluginManager\u7c7b<\/h4>\n<p>\u5b83\u662f\u4e00\u4e2a\u5355\u4f8b\uff0c\u8d1f\u8d23\u8bfb\u53d6\u63d2\u4ef6apk\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u521b\u5efa\u51fa\u4e13\u5c5e\u4e8e\u63d2\u4ef6\u7684\u7c7b\u52a0\u8f7d\u5668<code>DexClassLoader<\/code>\uff0c\u8d44\u6e90\u7ba1\u7406\u5668<code>Resources<\/code>\uff0c\u4ee5\u53ca\u5305\u4fe1\u606f<code>PackageInfo<\/code>\u5e76\u7528<code>public get<\/code>\u65b9\u6cd5\u516c\u5f00\u51fa\u53bb\u3002<\/p>\n<pre><code class=\"language-java\">import android.content.Context;\nimport android.content.Intent;\nimport android.content.pm.PackageInfo;\nimport android.content.pm.PackageManager;\nimport android.content.res.AssetManager;\nimport android.content.res.Resources;\n\nimport java.io.File;\nimport java.lang.reflect.Method;\n\nimport dalvik.system.DexClassLoader;\n\n\/**\n * \u63d2\u4ef6apk\u7684\u7ba1\u7406\u7c7b\n * &lt;p&gt;\n * loadPluginApk(String path);\n * init(Context context);\n * getPackageInfo();\n * getDexClassLoader();\n * getResources();\n *\/\npublic class PluginManager {\n\n    \/\/*****\u5e94\u8be5\u662f\u5355\u4f8b\u6a21\u5f0f\uff0c\u56e0\u4e3a\u4e00\u4e2a\u5bbf\u4e3bapp\u53ea\u9700\u8981\u4e00\u4e2a\u63d2\u4ef6\u7ba1\u7406\u5668\u5bf9\u8c61\u5373\u53ef*****\n    private PluginManager() {\n    }\n\n    private volatile static PluginManager instance; \/\/volatile \u4fdd\u8bc1\u6bcf\u4e00\u6b21\u53d6\u7684instance\u5bf9\u8c61\u90fd\u662f\u6700\u65b0\u7684\n\n    public static PluginManager getInstance() {\n        if (instance == null) {\n            synchronized (PluginManager.class) {\n                if (instance == null) {\n                    instance = new PluginManager();\n                }\n            }\n        }\n        return instance;\n    }\n\n    private Context mContext; \/\/\u4e0a\u4e0b\u6587\n\n    private PackageInfo packageInfo; \/\/\u5305\u4fe1\u606f\n    private DexClassLoader dexClassLoader; \/\/\u7c7b\u52a0\u8f7d\u5668\n    private Resources resources; \/\/\u8d44\u6e90\u5305\n\n    public void init(Context context) {\n        mContext = context.getApplicationContext(); \/\/\u8981\u7528application \u56e0\u4e3a\u8fd9\u662f\u5355\u4f8b\uff0c\u76f4\u63a5\u7528Activity\u5bf9\u8c61\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\n    }\n\n    \/**\n     * \u4ece\u63d2\u4ef6apk\u4e2d\u8bfb\u51fa\u6211\u4eec\u6240\u9700\u8981\u7684\u4fe1\u606f\n     *\n     * @param apkPath\n     *\/\n    public void loadPluginApk(String apkPath) {\n        \/\/\u5148\u62ff\u5230\u5305\u4fe1\u606f\n        packageInfo = mContext.getPackageManager().getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); \/\/\u53ea\u62ffActivity\n        if (packageInfo == null)\n            throw new RuntimeException(&quot;\u63d2\u4ef6\u52a0\u8f7d\u5931\u8d25&quot;); \/\/\u5982\u679capkPath\u662f\u4f20\u7684\u9519\u7684\uff0c\u90a3\u5c31\u62ff\u4e0d\u5230\u5305\u4fe1\u606f\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e5f\u5c31\u4e0d\u7528\u6267\u884c\n\n        \/\/\u7c7b\u52a0\u8f7d\u5668\uff0cDexClassLoader\u4e13\u95e8\u8d1f\u8d23\u5916\u90e8dex\u7684\u7c7b\n        File outFile = mContext.getDir(&quot;odex&quot;, Context.MODE_PRIVATE);\n        dexClassLoader = new DexClassLoader(apkPath, outFile.getAbsolutePath(), null, mContext.getClassLoader());\n\n        \/\/\u521b\u5efaAssetManager\uff0c\u7136\u540e\u521b\u5efaResources\n        try {\n            AssetManager assetManager = AssetManager.class.newInstance();\n            Method method = AssetManager.class.getDeclaredMethod(&quot;addAssetPath&quot;, String.class);\n            method.invoke(assetManager, apkPath);\n            resources = new Resources(assetManager,\n                    mContext.getResources().getDisplayMetrics(),\n                    mContext.getResources().getConfiguration());\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/\/\u628a\u8fd93\u4e2a\u73a9\u610f\u516c\u5f00\u51fa\u53bb\n    public PackageInfo getPackageInfo() {\n        return packageInfo;\n    }\n\n    public DexClassLoader getDexClassLoader() {\n        return dexClassLoader;\n    }\n\n    public Resources getResources() {\n        return resources;\n    }\n\n    \/**\n     * \u65e2\u7136\u65e0\u8bba\u662f\u5bbf\u4e3b\u542f\u52a8\u63d2\u4ef6\u7684Activity\uff0c\u8fd8\u662f\u63d2\u4ef6\u5185\u90e8\u7684\u8df3\u8f6c\u90fd\u8981\u4f7f\u7528ProxyActivity\u4f5c\u4e3a\u4ee3\u7406\n     * \u4f55\u4e0d\u5199\u4e00\u4e2a\u516c\u5171\u65b9\u6cd5\u4ee5\u4f9b\u8c03\u7528\u5462\uff1f\n     *\n     * @param context\n     * @param realActivityClassName\n     *\/\n    public void gotoActivity(Context context, String realActivityClassName) {\n        Intent intent = new Intent(context, ProxyActivity.class);\n        intent.putExtra(PluginApkConst.TAG_CLASS_NAME, realActivityClassName);\n        context.startActivity(intent);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public class PluginApkConst {\n    public final static String TAG_CLASS_NAME = &quot;className&quot;;\n    public static final String TAG_FROM = &quot;from&quot;;\n}<\/code><\/pre>\n<h4>ProxyActivity\u7c7b<\/h4>\n<p>\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u4ee3\u7406\uff0c\u4e00\u4e2a\u5080\u5121\uff0c\u5bbf\u4e3b\u80fd\u591f\u901a\u8fc7\u5b83\uff0c\u6765\u95f4\u63a5\u5730\u7ba1\u7406\u771f\u6b63\u63d2\u4ef6Activity\u7684\u751f\u547d\u5468\u671f\u3002\u90a3\u5b83\u662f\u5982\u4f55\u95f4\u63a5\u7ba1\u7406\u771f\u6b63Activity\u7684\u751f\u547d\u5468\u671f\uff1f\u7528\u7c7b\u4f3c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">import android.app.Activity;\nimport android.content.res.Resources;\nimport android.os.Bundle;\n\n\/**\n * \u4ee3\u7406Activity\n * \u4f5c\u7528\uff1a\u63a5\u6536\u6765\u81ea\u5bbf\u4e3b\u7684\u8df3\u8f6c\u610f\u56fe\uff0c\u5e76\u4e14\u62ff\u5230\u5176\u4e2d\u7684\u53c2\u6570\n * \u8fd9\u91cc\u53ea\u80fd\u7ee7\u627fActivity\uff0c\u800c\u4e0d\u662fAppCompatActivity\uff0c\u5426\u5219\u4f1a\u62a5\u201c\u7a7a\u6307\u9488\u201d\n * \u539f\u56e0\u662f\uff0cAppCompatActivity\u4f1a\u8c03\u7528\u4e0a\u4e0b\u6587\uff0c\u4f60\u95ee\u4e3a\u5565\uff1f\u4e0d\u77e5\u9053\u554a\uff0c\u95ee\u8c37\u6b4c\u5927\u4f6c\n *\/\npublic class ProxyActivity extends Activity {\n\n    private String realActivityName; \/\/\u65e2\u7136\u6211\u53ea\u662f\u4e2a\u4ee3\u7406\uff0c\u90a3\u4e48\u81ea\u7136\u6709\u771f\u6b63\u7684Activity\n\n    private IPlugin iPlugin;\n\n    \/\/ \u7531ProxyActivity\u4ee3\u4e3a\u7ba1\u7406\u771f\u6b63Activity\u7684\u751f\u547d\u5468\u671f\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        realActivityName = getIntent().getStringExtra(PluginApkConst.TAG_CLASS_NAME); \/\/\u5bbf\u4e3b\uff0c\u5c06\u771f\u6b63\u7684\u8df3\u8f6c\u610f\u56fe\uff0c\u653e\u5728\u4e86\u8fd9\u4e2a\u53c2\u6570className\u4e2d\uff0c\n        \/\/\u62ff\u5230realActivityName\uff0c\u63a5\u4e0b\u6765\u7684\u5de5\u4f5c\uff0c\u81ea\u7136\u5c31\u662f\u5c55\u793a\u51fa\u771f\u6b63\u7684Activity\n        try { \/\/\u539f\u5219\uff0c\u53cd\u5c04\u521b\u5efaRealActivity\u5bf9\u8c61\uff0c\u4f46\u662f\uff0c\u53bb\u62ff\u8fd9\u4e2a\u5b83\u7684class\uff0c\u53ea\u80fd\u7528dexClassLoader\n            Class&lt;?&gt; realActivityClz = PluginManager.getInstance().getDexClassLoader().loadClass(realActivityName);\n            Object obj = realActivityClz.newInstance();\n            if (obj instanceof IPlugin) { \/\/\u6240\u6709\u7684\u63d2\u4ef6Activity\uff0c\u90fd\u5fc5\u987b\u662fIPlugin\u7684\u5b9e\u73b0\u7c7b\n                iPlugin = (IPlugin) obj;\n                Bundle bd = new Bundle();\n                bd.putInt(PluginApkConst.TAG_FROM, IPlugin.FROM_EXTERNAL);\n                iPlugin.attach(this);\n                iPlugin.onCreate(bd); \/\/\u53cd\u5c04\u521b\u5efa\u7684\u63d2\u4ef6Activity\u7684\u751f\u547d\u5468\u671f\u51fd\u6570\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u90a3\u4e48\uff0c\u5c31\u7531ProxyActivity\u4ee3\u4e3a\u6267\u884c\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    @Override\n    protected void onStart() {\n        iPlugin.onStart();\n        super.onStart();\n    }\n\n    @Override\n    protected void onResume() {\n        iPlugin.onResume();\n        super.onResume();\n    }\n\n    @Override\n    protected void onRestart() {\n        iPlugin.onRestart();\n        super.onRestart();\n    }\n\n    @Override\n    protected void onPause() {\n        iPlugin.onPause();\n        super.onPause();\n    }\n\n    @Override\n    protected void onStop() {\n        iPlugin.onStop();\n        super.onStop();\n    }\n\n    @Override\n    protected void onDestroy() {\n        iPlugin.onDestroy();\n        super.onDestroy();\n    }\n\n    \/\/\u7136\u540e\uff0c\u4e0b\u97622\u4e2a\u65b9\u6cd5\u5fc5\u987b\u91cd\u5199\uff0c\u56e0\u4e3a\u63d2\u4ef6\u4e2d\u4f7f\u7528\u7684\u662f\u5916\u90e8\u7684\u7c7b\u548c\u8d44\u6e90\uff0c\u6240\u4ee5\u5fc5\u987b\u7528\u5bf9\u5e94\u7684DexClassLoader\n    @Override\n    public ClassLoader getClassLoader() {\n        ClassLoader classLoader = PluginManager.getInstance().getDexClassLoader();\n        return classLoader != null ? classLoader : super.getClassLoader();\n    }\n\n    @Override\n    public Resources getResources() {\n        Resources resources = PluginManager.getInstance().getResources();\n        return resources != null ? resources : super.getResources();\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>\u524d\u9762<code>PluginManager<\/code>\u8fd4\u56de\u4e86\u4e13\u5c5e\u4e8e\u63d2\u4ef6\u7684\u7c7b\u52a0\u8f7d\u5668<code>DexClassLoader<\/code>\uff0c\u8d44\u6e90\u7ba1\u7406\u5668<code>Resources<\/code>\uff0c\u90a3\u4e48\u8fd9\u4e2a<code>ProxyActivity<\/code>\u771f\u6b63\u5c55\u793a\u7684\u662f\u63d2\u4ef6\u7684Activity\u5185\u5bb9\uff0c\u5c31\u8981\u4f7f\u7528\u63d2\u4ef6\u81ea\u5df1\u7684\u7c7b\u52a0\u8f7d\u5668\u548c\u8d44\u6e90\u7ba1\u7406\u5668<\/p>\n<p><code>public class ProxyActivity extends Activity {}<\/code>\uff0c\u6587\u4e2dProxyActivity\u7ee7\u627f\u7684\u662f<code>android.app.Activity<\/code>\uff0c\u800c\u4e0d\u662f<code>android.support.v7.app.AppCompatActivity<\/code>\uff0c\u8fd9\u662f\u56e0\u4e3a\uff0c<code>AppCompatActivity<\/code>\u4f1a\u68c0\u6d4b\u4e0a\u4e0b\u6587context\uff0c\u4ece\u800c\u5bfc\u81f4\u7a7a\u6307\u9488\u3002\u81f3\u4e8e\u66f4\u6df1\u5c42\u7684\u539f\u56e0\uff0c\u6709\u5174\u8da3\u7684\u5927\u4f6c\u53ef\u4ee5\u7ee7\u7eed\u6316\u6398\uff0c\u6ca1\u5174\u8da3\u7684\u8bdd\u76f4\u63a5\u7528<code>android.app.Activity<\/code>\u5373\u53ef<\/p>\n<\/blockquote>\n<h4>IPlugin\u63a5\u53e3<\/h4>\n<pre><code class=\"language-java\">import android.app.Activity;\nimport android.content.Intent;\nimport android.os.Bundle;\n\n\/**\n * \u63d2\u4ef6Activity\u7684\u63a5\u53e3\u89c4\u8303\n *\/\npublic interface IPlugin {\n\n    int FROM_INTERNAL = 0; \/\/\u63d2\u4ef6\u5355\u72ec\u6d4b\u8bd5\u65f6\u7684\u5185\u90e8\u8df3\u8f6c\n    int FROM_EXTERNAL = 1; \/\/\u5bbf\u4e3b\u6267\u884c\u7684\u8df3\u8f6c\u903b\u8f91\n\n    \/**\n     * \u7ed9\u63d2\u4ef6Activity\u6307\u5b9a\u4e0a\u4e0b\u6587\n     *\n     * @param activity\n     *\/\n    void attach(Activity activity);\n\n    \/\/ \u4ee5\u4e0b\u5168\u90fd\u662fActivity\u751f\u547d\u5468\u671f\u51fd\u6570,\n    \/\/ \u63d2\u4ef6Activity\u672c\u8eab\uff0c\u5728\u88ab\u7528\u4f5c&quot;\u63d2\u4ef6&quot;\u7684\u65f6\u5019\u4e0d\u5177\u5907\u751f\u547d\u5468\u671f\uff0c\u7531\u5bbf\u4e3b\u91cc\u9762\u7684\u4ee3\u7406Activity\u7c7b\u4ee3\u4e3a\u7ba1\u7406\n    void onCreate(Bundle saveInstanceState);\n\n    void onStart();\n\n    void onResume();\n\n    void onRestart();\n\n    void onPause();\n\n    void onStop();\n\n    void onDestroy();\n\n    void onActivityResult(int requestCode, int resultCode, Intent data);\n}<\/code><\/pre>\n<h4>PluginBaseActivity\u62bd\u8c61\u7c7b<\/h4>\n<p>\u63d2\u4ef6module\u4e2d\u4e5f\u8bb8\u4e0d\u53ea\u4e00\u4e2aActivity\uff0c\u6211\u4eec\u542f\u52a8\u63d2\u4ef6Activity\u4e4b\u540e\uff0c\u63d2\u4ef6\u5185\u90e8\u5982\u679c\u9700\u8981\u8df3\u8f6c\uff0c\u4ecd\u7136\u8981\u9075\u5b88\u63d2\u4ef6\u5316\u7684\u89c4\u5219\uff0c\u90a3\u5c31\u7ed9\u4ed6\u4eec\u521b\u5efa\u4e00\u4e2a\u5171\u540c\u7684\u7236\u7c7b<code>PluginBaseActivity<\/code><\/p>\n<pre><code class=\"language-java\">import android.app.Activity;\nimport android.content.Intent;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.View;\n\nimport androidx.appcompat.app.AppCompatActivity;\n\n\/**\n * \u63d2\u4ef6Activity\u7684\u57fa\u7c7b\uff0c\u63d2\u4ef6\u4e2d\u7684\u6240\u6709Activity\uff0c\u90fd\u8981\u7ee7\u627f\u5b83\n *\/\npublic abstract class PluginBaseActivity extends AppCompatActivity implements IPlugin {\n\n    private final String TAG = &quot;PluginBaseActivityTag&quot;;\n    protected Activity proxy; \/\/\u4e0a\u4e0b\u6587\n\n    \/\/\u8fd9\u91cc\u57fa\u672c\u4e0a\u90fd\u5728\u91cd\u5199\u539f\u672cActivity\u7684\u51fd\u6570\uff0c\u56e0\u4e3a \u8981\u517c\u5bb9&quot;\u63d2\u4ef6\u5355\u72ec\u6d4b\u8bd5&quot; \u548c &quot;\u96c6\u6210\u5230\u5bbf\u4e3b\u6574\u4f53\u6d4b\u8bd5&quot;\uff0c\u6240\u4ee5\u8981\u8fdb\u884c\u60c5\u51b5\u533a\u5206\n    private int from = IPlugin.FROM_INTERNAL; \/\/\u9ed8\u8ba4\u662f&quot;\u63d2\u4ef6\u5355\u72ec\u6d4b\u8bd5&quot;\n\n    @Override\n    public void attach(Activity proxyActivity) {\n        proxy = proxyActivity;\n    }\n\n    @Override\n    public void onCreate(Bundle saveInstanceState) {\n        if (saveInstanceState != null)\n            from = saveInstanceState.getInt(PluginApkConst.TAG_FROM);\n\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onCreate(saveInstanceState);\n            proxy = this; \/\/\u5982\u679c\u662f\u4ece\u5185\u90e8\u8df3\u8f6c\uff0c\u90a3\u5c31\u5c06\u4e0a\u4e0b\u6587\u5b9a\u4e3a\u81ea\u5df1\n        }\n    }\n\n    @Override\n    public void onStart() {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onStart();\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonStart()&quot;);\n        }\n    }\n\n    @Override\n    public void onResume() {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onResume();\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonResume()&quot;);\n        }\n    }\n\n    @Override\n    public void onRestart() {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onRestart();\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonRestart()&quot;);\n        }\n    }\n\n    @Override\n    public void onPause() {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onPause();\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonPause()&quot;);\n        }\n    }\n\n    @Override\n    public void onStop() {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onStop();\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonStop()&quot;);\n        }\n    }\n\n    @Override\n    public void onDestroy() {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onDestroy();\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonDestroy()&quot;);\n        }\n    }\n\n    @Override\n    public void onActivityResult(int requestCode, int resultCode, Intent data) {\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.onActivityResult(requestCode, resultCode, data);\n        } else {\n            Log.d(TAG, &quot;\u5bbf\u4e3b\u542f\u52a8\uff1aonActivityResult()&quot;);\n        }\n    }\n\n    \/\/\u4e0b\u9762\u662f\u51e0\u4e2a\u751f\u547d\u5468\u671f\u4e4b\u5916\u7684\u91cd\u5199\u51fd\u6570\n    @Override\n    public void setContentView(int layoutResID) { \/\/\u8bbe\u7f6econtentView\u5206\u60c5\u51b5\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.setContentView(layoutResID);\n        } else {\n            proxy.setContentView(layoutResID);\n        }\n    }\n\n    @Override\n    public View findViewById(int id) {\n        if (from == FROM_INTERNAL) {\n            return super.findViewById(id);\n        } else {\n            return proxy.findViewById(id);\n        }\n    }\n\n    @Override\n    public void startActivity(Intent intent) { \/\/\u540c\u7406\n        if (from == IPlugin.FROM_INTERNAL) {\n            super.startActivity(intent); \/\/\u539fintent\u53ea\u80fd\u7528\u4e8e\u63d2\u4ef6\u5355\u72ec\u8fd0\u884c\u65f6\n        } else {\n            \/\/\u5982\u679c\u662f\u96c6\u6210\u6a21\u5f0f\u4e0b\uff0c\u63d2\u4ef6\u5185\u7684\u8df3\u8f6c\uff0c\u63a7\u5236\u6743\u4ecd\u7136\u662f\u5728\u5bbf\u4e3b\u4e0a\u4e0b\u6587\u91cc\u9762\uff0c\u6240\u4ee5--!\n            \/\/\u5148\u8df3\u5230\u4ee3\u7406Activity\uff0c\u7531\u4ee3\u7406Activity\u5c55\u793a\u771f\u6b63\u7684Activity\u5185\u5bb9\n            PluginManager.getInstance().gotoActivity(proxy, intent.getComponent().getClassName());\n        }\n    }\n}<\/code><\/pre>\n<p><code>PluginBaseActivity<\/code>\u62bd\u8c61\u7c7b\u4e2d\uff0c3\u4e2a\u91cd\u70b9\u9700\u8981\u7279\u522b\u8bf4\u660e<\/p>\n<ol>\n<li>\n<p>\u63d2\u4ef6module\u9700\u8981\u5355\u72ec\u6d4b\u8bd5\uff0c\u4e5f\u9700\u8981\u4f5c\u4e3a\u63d2\u4ef6\u6765\u96c6\u6210\u6d4b\u8bd5\uff0c\u6240\u4ee5\u8fd9\u91ccIPlugin\u63a5\u53e3\u4e2d\u5b9a\u4e49\u4e86<code>FROM_INTERNAL<\/code>\u548c<code>FROM_EXTERNAL<\/code>\u8fdb\u884c\u60c5\u5f62\u533a\u5206<\/p>\n<\/li>\n<li>\n<p>\u9664\u4e86IPlugin\u5fc5\u987b\u5b9e\u73b0\u7684\u4e00\u4e9b\u751f\u547d\u5468\u671f\u65b9\u6cd5\u4e4b\u5916\uff0c\u6700\u540e\u8fd8\u65b0\u589e\u4e863\u4e2a\u65b9\u6cd5\uff1a<code>setContentView<\/code>\uff0c<code>findViewById<\/code>\uff0c<code>startActivity<\/code>\uff0c\u8bbe\u7f6e\u5e03\u5c40\uff0c\u5bfb\u627e\u7ec4\u4ef6\uff0c\u8df3\u8f6cActivity\uff0c\u4e5f\u662f\u9700\u8981\u533a\u5206\u5355\u6d4b\u8fd8\u662f\u96c6\u6210\u6d4b\u8bd5\u7684\uff0c\u6240\u4ee5\uff0c\u4e5f\u8981\u505a<code>if\/else<\/code>\u5224\u5b9a<\/p>\n<\/li>\n<li>\n<p>\u4e0a\u9762\u8bf4\u7684<code>startActivity<\/code>\uff0c\u5f53\u4ece\u5916\u90e8\u8df3\u8f6c\uff0c\u4e5f\u5c31\u662f\u5bbf\u4e3b\u6765\u542f\u52a8\u63d2\u4ef6Activity\u7684\u65f6\u5019\uff0c\u4e5f\u53ea\u80fd\u8df3\u5230<code>ProxyActivity<\/code>\uff0c\u7136\u540e\u628a\u771f\u6b63\u7684\u76ee\u6807Activity\u653e\u5728\u53c2\u6570\u4e2d<\/p>\n<\/li>\n<li>\n<p>\u5728<code>plugin library module<\/code>\u4e0b\u7684<code>AndroidManifest.xml<\/code>\u6ce8\u518c<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"language-xml\">&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    package=&quot;cn.appblog.plugin_lib&quot; &gt;\n    &lt;application&gt;\n        &lt;activity android:name=&quot;.ProxyActivity&quot; \/&gt;\n    &lt;\/application&gt;\n&lt;\/manifest&gt;<\/code><\/pre>\n<h2>\u5982\u4f55\u4f7f\u7528Demo<\/h2>\n<p>\u66f4\u6539<code>plugin_module<\/code>\u7684\u5185\u5bb9\uff0c\u91cd\u65b0\u751f\u6210\u4e00\u4e2aapk\uff0c\u653e\u5230<code>app\/src\/main\/assets<\/code>\u76ee\u5f55\uff0c\u6587\u4ef6\u540d\u5fc5\u987b\u548c\u5916\u58f3app\u5185\u5199\u7684\u4e00\u6837\uff0c\u8fd0\u884c\u5916\u58f3app\u5373\u53ef\u3002<\/p>\n<h2>\u6700\u7ec8\u6548\u679c\u5c55\u793a<\/h2>\n<p>\u96c6\u6210\u6d4b\u8bd5\uff0c\u7531\u5916\u58f3app\u542f\u52a8\u63d2\u4ef6Activity<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u7531\u5916\u58f3app\u542f\u52a8\u63d2\u4ef6Activity-\u96c6\u6210\u6d4b\u8bd5.gif\" alt=\"\u96c6\u6210\u6d4b\u8bd5\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u7531\u5916\u58f3app\u542f\u52a8\u63d2\u4ef6Activity-\u63d2\u4ef6\u5355\u72ec\u6d4b\u8bd5.gif\" alt=\"\u63d2\u4ef6\u5355\u72ec\u6d4b\u8bd5\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u652f\u4ed8\u5b9dAPP\u672c\u8eab\u66f4\u50cf\u662f\u4e00\u4e2a&quot;\u7a7a\u58f3&quot;\uff0c\u91cc\u9762\u53ef\u4ee5\u642d\u8f7d\u5f88\u591a\u5c0f\u529f\u80fd\uff0c\u8fd9\u4e9b\u5c0f\u529f\u80fd\u90fd\u662f\u4ee5&quot;\u63d2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[60,470],"class_list":["post-1834","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-activity","tag-470"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1834"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1834\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}