{"id":410,"date":"2023-09-16T11:02:48","date_gmt":"2023-09-16T03:02:48","guid":{"rendered":""},"modified":"2023-09-16T11:02:48","modified_gmt":"2023-09-16T03:02:48","slug":"Android\u4e2d\u5bf9ClassLoader\u7684\u8ba4\u8bc6","status":"publish","type":"post","link":"https:\/\/mushiming.com\/410.html","title":{"rendered":"Android\u4e2d\u5bf9ClassLoader\u7684\u8ba4\u8bc6"},"content":{"rendered":"
Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e00)\u4e4bJava\u4e2d\u7684ClassLoader
Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e8c)\u4e4bAndroid\u4e2d\u7684ClassLoader
Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e09)\u4e4b\u52a8\u6001\u52a0\u8f7d\u8d44\u6e90
Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u56db)\u4e4b\u63d2\u4ef6\u5316\u5f00\u53d1<\/p>\n
\u9879\u76ee\u5730\u5740<\/strong><\/p>\n Android\u63d2\u4ef6\u5316\u6846\u67b6\u7cfb\u5217\u4e4b\u7c7b\u52a0\u8f7d\u5668 \u5728Java\u865a\u62df\u673a\u4e2d\u52a0\u8f7d\u7684\u5bf9\u8c61\u4e3b\u8981\u662f\u5b57\u8282\u7801, \u4e5f\u5c31\u662f \u5728\u4e0a\u4e00\u7bc7\u535a\u5ba2\u4e2d, \u6211\u4eec\u77e5\u9053Java\u865a\u62df\u673a\u7684ClassLoader\u6709\u4e09\u4e2a:<\/p>\n \u5e76\u4e14ClassLoader\u52a0\u8f7d\u7c7b\u7684\u987a\u5e8f\u51c6\u5b88 \u5728Android\u865a\u62df\u673a\u4e2d, \u4e5f\u6709\u4e09\u4e2aClassLoader, \u5e76\u4e14\u52a0\u8f7d\u987a\u5e8f\u4e5f\u662f\u51c6\u5b88 <\/p>\n Android\u4e2d\u7684ClassLoader<\/p>\n \u597d\u4e86, \u65e2\u7136\u6211\u4eec\u73b0\u5728\u77e5\u9053 \u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u4e00\u4e2a\u670d\u52a1, \u7528\u4e8e\u63d0\u4f9b\u7528\u6237\u4fe1\u606f\u7ba1\u7406\u7684. \u800c\u6211\u4eec\u7684\u7528\u6237\u4fe1\u606f\u662f\u6258\u7ba1\u5728\u7b2c\u4e09\u65b9\u540e\u7aef\u4e91\u4e0a, \u73b0\u5728\u7531\u4e8e\u67d0\u4e9b\u56e0\u7d20, \u9700\u8981\u66f4\u6362\u540e\u7aef\u4e91. \u6240\u4ee5\u6211\u4eec\u4e5f\u9700\u8981\u91cd\u65b0\u53bb\u5b9e\u73b0\u6211\u4eec\u7684\u7528\u6237\u4fe1\u606f\u7ba1\u7406\u7c7b. \u5728\u4e0d\u53d1\u5e03\u65b0\u7248\u672c\u7684\u60c5\u51b5\u4e0b, \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u8bb2\u5230\u7684\u6280\u672f\u53bb\u5b9e\u73b0.<\/p>\n<\/blockquote>\n \u8be5\u63a5\u53e3\u5b9a\u4e49\u4e86\u7528\u6237\u670d\u52a1\u7684\u6807\u51c6, \u6211\u4eec\u5bfc\u51fajar\u5305 \u6211\u4eec\u65b0\u5efaAndroid Studio\u5de5\u7a0b \u7136\u540e\u6211\u4eec\u5bf9 \u8fd9\u91cc\u6211\u4eec\u7528\u6237\u9a8c\u8bc1\u5c31\u7b80\u5355\u5730\u5bf9\u6bd4\u4e00\u4e0b\u7528\u6237\u540d\u8ddf\u5bc6\u7801\u662f\u5426\u4e00\u6837...<\/p>\n \u7136\u540e\u6211\u4eec\u8f93\u51faAPK \u63a5\u4e0b\u6765\u6211\u4eec\u65b0\u5efaAndroid Studio\u5de5\u7a0b \u7136\u540e\u628a \u628a \u5176\u5b9e\u8fd9\u91cc \u7136\u540e\u6211\u4eec\u65b0\u5efa\u5de5\u5177\u7c7b<\/p>\n<\/blockquote>\n <\/p>\n Android 5.0<\/p>\n \u8fd9\u662f\u5728Android 5\u4e0a\u9762\u7684\u6548\u679c \u5b9e\u73b0\u4e86\u6211\u4eec\u7684\u76ee\u6807->\u52a8\u6001\u52a0\u8f7d<\/strong>.<\/p>\n<\/blockquote>\n \u4f46\u662f\u5728Android4.1\u4e0a\u9762\u4f1a\u62a5\u9519. Android\u63d2\u4ef6\u5316\u6846\u67b6\u7cfb\u5217\u4e4b\u7c7b\u52a0\u8f7d\u5668\u6587\u7ae0\u4e2d\u6307\u51fa\u4e86\u539f\u56e0\u662f\u56e0\u4e3aAndroid\u865a\u62df\u673a\u52a0\u8f7dClass\u7684\u5dee\u5f02, \u628a\u63d2\u4ef6\u5e94\u7528\u4e2d\u7684 <\/p>\n Android 4.1.gif<\/p>\n <\/p>\n )<\/p>\n \u4f5c\u8005\uff1aAugust1996\u53c2\u8003<\/h1>\n
Android\u52a8\u6001\u52a0\u8f7d\u4e4bClassLoader\u8be6\u89e3<\/p>\nJava\u865a\u62df\u673a\u4e0eAndroid\u865a\u62df\u673a\u52a0\u8f7d\u5bf9\u8c61\u7684\u4e0d\u540c<\/h1>\n
class<\/code>\u6587\u4ef6. \u800c
Android<\/code>\u865a\u62df\u673a\u4e2d\u4e3b\u8981\u52a0\u8f7d\u7684\u662f
dex<\/code>\u6587\u4ef6,
dex<\/code>\u6587\u4ef6\u5c31\u662f\u628a
class<\/code>\u4f18\u5316\u6574\u5408\u6253\u5305\u7684\u7ed3\u679c . \u6211\u4eec\u77e5\u9053\u4e00\u4e2a
apk<\/code>\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a
\u6253\u5305\u7684\u6587\u4ef6<\/code>, \u8fd0\u884c\u7684\u65f6\u5019\u4f1a\u91ca\u653e\u51fa
dex<\/code>\u5305. \u5f53\u7136\u4f18\u5316\u540e\u7684
odex<\/code>\u662f\u4e3b\u8981\u52a0\u8f7d\u7684\u5bf9\u8c61, \u4e3b\u8981\u4f18\u70b9\u662f\u6bd4
dex<\/code>\u52a0\u8f7d\u5730\u5feb.<\/p>\n
ClassLoader\u5206\u7c7b<\/h1>\n
\n
BootStrap ClassLoader<\/code><\/p>\n<\/li>\n
Extension ClassLoader<\/code><\/p>\n<\/li>\n
App ClassLoader<\/code><\/p>\n<\/li>\n<\/ul>\n
\u53cc\u4eb2\u59d4\u6258\u673a\u5236<\/code>.<\/p>\n
\n
\u53cc\u4eb2\u59d4\u6258\u673a\u5236<\/code>:<\/p>\n<\/blockquote>\n
\n
BootClassLoader<\/code> : \u4e3b\u8981\u7528\u4e8e\u52a0\u8f7d\u7cfb\u7edf\u7684\u7c7b\uff0c\u5305\u62ecjava\u548candroid\u7cfb\u7edf\u7684\u7c7b\u5e93<\/p>\n<\/li>\n
PathClassLoader<\/code> : \u4e3b\u8981\u7528\u4e8e\u52a0\u8f7d\u5e94\u7528\u5185\u4e2d\u7684\u7c7b, \u5b83\u52a0\u8f7d\u7684\u8def\u5f84\u662f\u56fa\u5b9a\u7684, \u56e0\u6b64\u65e0\u6cd5\u6307\u5b9a<\/p>\n<\/li>\n
DexClassLoader<\/code> : \u53ef\u4ee5\u7528\u4e8e\u52a0\u8f7d\u4efb\u610f\u8def\u5f84\u7684zip,jar\u6216\u8005apk\u6587\u4ef6, \u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u52a0\u8f7d<\/strong><\/p>\n<\/li>\n<\/ul>\n
\u5e94\u7528<\/h1>\n
DexClassLoader<\/code>\u53ef\u4ee5\u52a0\u8f7d\u4efb\u610f\u8def\u5f84\u7684zip,jar\u6216\u8005apk\u6587\u4ef6. \u90a3\u4e48\u5230\u5e95\u6709\u4ec0\u4e48\u7528\u5462? \u56de\u5230\u6211\u4eec\u7684\u76ee\u7684, \u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60ClassLoader? \u4e0d\u5c31\u662f\u5b9e\u73b0\u8fd0\u884c\u65f6\u52a8\u6001\u52a0\u8f7d\u670d\u52a1\u5668\u4e0a\u4e0b\u8f7d\u7684\u4e34\u65f6\u8865\u4e01\u6216\u8005\u63d2\u4ef6\u4e48?<\/strong><\/p>\n
\n
IUserService.java<\/h2>\n
package top.august.i;\n\npublic interface IUserService {\n String login(String username, String password);\n\n String logout();\n}\n<\/code><\/pre>\n
\n
UserService.jar<\/code><\/p>\n<\/blockquote>\n
UserPlugin<\/h2>\n
\n
UserPlugin<\/code>, \u7136\u540e\u5f15\u5165jar\u5305.<\/p>\n<\/blockquote>\n
IUserService<\/code>\u8fdb\u884c\u5b9e\u73b0<\/p>\n
public class UserService implements IUserService {\n\n private String username;\n\n @Override\n public String login(String s, String s1) {\n\n if (s.equals(s1)) {\n username = s;\n return \"\u540e\u7aef\u4e91A:\u767b\u5f55\u6210\u529f!\";\n }\n\n return \"\u540e\u7aef\u4e91A:\u767b\u5f55\u5931\u8d25...\";\n }\n\n @Override\n public String logout() {\n\n if (TextUtils.isEmpty(username)) {\n return \"\u540e\u7aef\u4e91A:\u672a\u767b\u5f55...\";\n }\n username = null;\n\n return \"\u540e\u7aef\u4e91A:\u6ce8\u9500\u6210\u529f!\";\n\n }\n}\n<\/code><\/pre>\n
UserPlugin.apk<\/code><\/p>\n
HostApp<\/h2>\n
\n
HostApp<\/code><\/p>\n<\/blockquote>\n
\n
UserPlugin.apk<\/code>\u653e\u5230
assets<\/code>\u76ee\u5f55\u4e0b<\/p>\n<\/li>\n
UserService.jar<\/code>\u653e\u5230libs\u4e0b\u9762\u5e76\u4e14\u5f15\u7528<\/p>\n<\/li>\n<\/ul>\n
UserPlugin.apk<\/code>\u53ef\u4ee5\u653e\u5230\u4efb\u610f\u8def\u5f84\u4e0b. \u56e0\u4e3a\u8fd9\u624d\u662f\u6211\u4eec\u670d\u52a1\u5668\u7684\u60c5\u51b5, \u628a\u63d2\u4ef6APK\u653e\u5230\u4efb\u610f\u4f4d\u7f6e. \u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u8bfb\u5199, \u6240\u4ee5\u624d\u653e\u5230assets\u76ee\u5f55\u4e0b<\/strong><\/p>\n
PluginUtil<\/h3>\n
\n
\npackage com.example.hostapp;\n\nimport android.content.Context;\n\nimport com.example.i.IUserService;\n\nimport java.io.File;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\n\nimport dalvik.system.DexClassLoader;\n\n\/**\n * Created by August on 2017\/3\/18.\n *\/\n\npublic class PluginUtil {\n\n private static final String PLUGIN_NAME = \"UserPlugin.apk\";\n\n \/**\n * \u52a0\u8f7d\u63d2\u4ef6APP\u7684\u7c7b\n *\n * @param context\n * @return\n * @throws IOException\n *\/\n public static IUserService load(Context context) throws IOException {\n String src = context.getFilesDir().getAbsolutePath() + File.separator + PLUGIN_NAME; \/\/ \u63d2\u4ef6APK\u5b58\u653e\u7684\u4f4d\u7f6e\n String dest = context.getFilesDir().getAbsolutePath() + File.separator + \"plugin\" + File.separator; \/\/ \u63d2\u4ef6APK\u8f6c\u6362\u4e3adex\u7684\u6587\u4ef6\u5939, \u4e5f\u5c31\u662fDexClassLoader\u52a0\u8f7d\u7684classPath\n\n\n \/\/ \u521b\u5efadest\u76ee\u5f55\n File destFile = new File(dest);\n if (!destFile.exists()) {\n destFile.mkdir();\n } else if (!destFile.isDirectory()) {\n destFile.delete();\n destFile.mkdir();\n }\n\n try {\n copyPlugin(context, src); \/\/ \u628a\u63d2\u4ef6\u4eceassets\u62f7\u8d1d\u51fa\u6765, \u5982\u679c\u662f\u4ece\u7f51\u7edc\u4e0b\u8f7d\u4e0b\u6765\u7684 \u90a3\u4e48\u5c31\u53ef\u4ee5\u4ece\u4e0b\u8f7d\u6587\u4ef6\u7684\u8def\u5f84\u62f7\u8d1d\u8fc7\u6765\n DexClassLoader classLoader = new DexClassLoader(src, dest, null, context.getClassLoader()); \/\/ \u6784\u9020\u81ea\u5df1\u7684DexClassLoader\n\n Class<?> cls = classLoader.loadClass(\"com.example.userplugin.impl.UserService\"); \/\/ \u52a0\u8f7d\u63d2\u4ef6APK\u4e2d\u7684UserService\u7c7b\n IUserService service = (IUserService) cls.newInstance(); \/\/ \u8fd4\u56de\u5b9e\u4f8b\n return service;\n } catch (Exception e) {\n e.printStackTrace();\n }\n return null;\n }\n\n\n \/**\n * \u62f7\u8d1d\u63d2\u4ef6APP\u5230\u63d2\u4ef6\u76ee\u5f55\n *\n * @param context\n * @param dest\n * @throws IOException\n *\/\n private static void copyPlugin(Context context, String dest) throws IOException {\n InputStream is = null;\n FileOutputStream fos = null;\n is = context.getAssets().open(PLUGIN_NAME);\n fos = new FileOutputStream(dest);\n int len;\n byte[] buf = new byte[1024];\n\n while ((len = is.read(buf)) != -1) {\n fos.write(buf, 0, len);\n }\n if (is != null) {\n is.close();\n }\n if (fos != null) {\n fos.close();\n }\n\n }\n}\n<\/code><\/pre>\n
MainActivity<\/h3>\n
\npackage com.example.hostapp;\n\nimport android.os.Bundle;\nimport android.support.v7.app.AppCompatActivity;\nimport android.view.View;\nimport android.widget.Toast;\n\nimport com.example.i.IUserService;\n\nimport java.io.IOException;\n\npublic class MainActivity extends AppCompatActivity {\n\n private IUserService mService;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n }\n\n public void load(View v) {\n try {\n mService = PluginUtil.load(this);\n if (mService != null) {\n Toast.makeText(this, \"\u52a0\u8f7d\u63d2\u4ef6APP\u6210\u529f\", Toast.LENGTH_SHORT).show();\n return;\n }\n } catch (IOException e) {\n e.printStackTrace();\n }\n Toast.makeText(this, \"\u52a0\u8f7d\u63d2\u4ef6APP\u5931\u8d25\", Toast.LENGTH_SHORT).show();\n }\n\n public void login(View v) {\n if (mService != null) {\n Toast.makeText(this, mService.login(\"August1996\", \"August1996\"), Toast.LENGTH_SHORT).show();\n }\n }\n\n public void logout(View v) {\n if (mService != null) {\n Toast.makeText(this, mService.logout(), Toast.LENGTH_SHORT).show();\n }\n }\n\n}\n\n<\/code><\/pre>\n
activity_main<\/h3>\n
\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:orientation=\"vertical\">\n\n <Button\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:onClick=\"load\"\n android:text=\"\u52a0\u8f7d\" \/>\n\n <Button\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:onClick=\"login\"\n android:text=\"\u767b\u5f55\" \/>\n\n <Button\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:onClick=\"logout\"\n android:text=\"\u6ce8\u9500\" \/>\n\n\n<\/LinearLayout>\n\n<\/code><\/pre>\n
\u6548\u679c\u56fe<\/h3>\n
\n
compile<\/code>\u6539\u6210
provided<\/code>\u65b9\u5f0f\u5c31\u53ef\u4ee5\u4e86, \u5177\u4f53\u533a\u522b\u770bAndroid Studi\u6dfb\u52a0\u4f9d\u8d56\u90a3\u4e9b\u4e8b.<\/p>\n
\u94fe\u63a5\uff1ahttps:\/\/www.jianshu.com\/p\/ba34eb96c45f
\u4f86\u6e90\uff1a\u7b80\u4e66
\u7b80\u4e66\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\uff0c\u4efb\u4f55\u5f62\u5f0f\u7684\u8f6c\u8f7d\u90fd\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\u5e76\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Android\u4e2d\u5bf9ClassLoader\u7684\u8ba4\u8bc6\u76ee\u5f55Android\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e00)\u4e4bJava\u4e2d\u7684ClassLoaderAndroid\u9ed1\u79d1\u6280\u52a8\u6001\u52a0\u8f7d(\u4e8c)\u4e4bAndroid\u4e2d\u7684C...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/410"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}