{"id":4569,"date":"2021-09-02T08:37:06","date_gmt":"2021-09-02T00:37:06","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=4569"},"modified":"2021-09-02T08:37:06","modified_gmt":"2021-09-02T00:37:06","slug":"%e8%b7%a8%e6%a8%a1%e5%9d%97%e7%9a%84java%e7%b1%bb%e8%b0%83%e7%94%a8%ef%bc%88java%e7%b1%bb%e6%89%93%e5%8d%b0%e6%97%a5%e5%bf%97%ef%bc%89","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=4569","title":{"rendered":"\u8de8\u6a21\u5757\u7684Java\u7c7b\u8c03\u7528\uff08Java\u7c7b\u6253\u5370\u65e5\u5fd7\uff09"},"content":{"rendered":"<p><strong>\u573a\u666f<\/strong>\uff1a\u60f3\u5728libcore\/dalvik\/src\/main\/java\/dalvik\/system\/BaseDexClassLoader.java\u7c7b\u91cc\u6253\u5370\u4e00\u4e0bAndroid\u65e5\u5fd7\uff0c\u7814\u7a76\u4e00\u4e0b\u8fd9\u4e2a\u7c7b\u3002Log\u7c7b\u5df2\u7ecf\u5bfc\u5165<br \/>\n<img src=\"https:\/\/upload-images.jianshu.io\/upload_images\/18328858-3ad9d982bb7b51bf.png?imageMogr2\/auto-orient\/strip%7CimageView2\/2\/w\/1240\" alt=\"image.png\" \/><br \/>\n\u7f16\u8bd1\u62a5\u9519\uff0c\u627e\u4e0d\u5230Log\u7c7b\u3002<br \/>\n<img src=\"https:\/\/upload-images.jianshu.io\/upload_images\/18328858-6fedafe6f66d9b26.png?imageMogr2\/auto-orient\/strip%7CimageView2\/2\/w\/1240\" alt=\"image.png\" \/><\/p>\n<p>\u770b\u4e00\u4e0bLog.java\u7c7b\u7684\u8def\u5f84\uff1aframeworks\/base\/core\/java\/android\/util\/Log.java<\/p>\n<p>\u8fd9\u4e24\u4e2a\u7c7b\uff0c\u5b8c\u5168\u4e0d\u5728\u4e00\u4e2a\u6a21\u5757\u4e0b\uff0cBaseDexClassLoader.java\u5c5e\u4e8elibcore\u6a21\u5757\uff0cLog\u5c5e\u4e8eframeworks\/base\u6a21\u5757\u3002\u5b9a\u4f4d\u5230Log.java\u4f4d\u4e8eSdk\\platforms\\android-29\\android.jar\u91cc\uff0c\u800candroid.jar\u670920M\uff0c\u4e0d\u53ef\u80fd\u76f4\u63a5\u628a\u8fd9\u4e2ajar\u76f4\u63a5\u590d\u5236\u5230libcore\u6a21\u5757\u7136\u540e\u5f15\u7528\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5148\u5206\u6790\u4e00\u4e0bandroid.jar\u662f\u5982\u4f55\u6253\u5305\u751f\u6210\u7684\uff0c\u7136\u540e\u4f9d\u846b\u82a6\u753b\u74e2\uff0c\u751f\u6210\u4e00\u4e2aczlog.jar\u5305\u3002<\/p>\n<h1>android.jar\u51fa\u751f\u8bb0<\/h1>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.360doc.com\/content\/14\/0214\/23\/9075092_352577757.shtml\">http:\/\/www.360doc.com\/content\/14\/0214\/23\/9075092_352577757.shtml<\/a><br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/wenku.baidu.com\/view\/8144fa1314791711cc79175e.html\">https:\/\/wenku.baidu.com\/view\/8144fa1314791711cc79175e.html<\/a><br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/c_z_w\/article\/details\/78994001\">https:\/\/blog.csdn.net\/c_z_w\/article\/details\/78994001<\/a><\/p>\n<h1>\u81ea\u5df1\u6253\u9020\u4e00\u4e2aAndroid\u5de5\u5177\u7c7b\u5e93<\/h1>\n<p>\u53c2\u8003<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/584ff56909b3\">https:\/\/www.jianshu.com\/p\/584ff56909b3<\/a>\u8fd9\u7bc7\u535a\u5ba2\u5173\u4e8eLog\u7c7b\u76f8\u5173\u7684\u5206\u6790\uff0c\u51c6\u5907\u88c1\u526a\u51fa\u4e00\u4e2alog\u76f8\u5173\u7684so\u548cjar\u5305\u3002<br \/>\n- so\u5e93\u88c1\u526a<br \/>\n\u4eff\u7167frameworks\/base\/core\/jni\/Android.mk\uff0c\u589e\u52a0\u4e00\u4e2a\u751f\u6210so\u5e93\u7684\u811a\u672c\uff0c\u53ea\u4f9d\u8d56\u548clib\u76f8\u5173\u7684\u5e93\u3002<\/p>\n<pre><code class=\"line-numbers\"># ========================================================\n# lib_cz_log.so\n# ========================================================\n# CZAdd\n# try make a small Android logutil\ninclude $(CLEAR_VARS)\nLOCAL_MODULE := lib_cz_log\nLOCAL_SRC_FILES := \\\n    AndroidRuntime.cpp \\\n    android_util_Log.cpp\nLOCAL_C_INCLUDES += \\\n    $(JNI_H_INCLUDE) \\\n    libcore\/include\nLOCAL_SHARED_LIBRARIES := \\\n    libnativehelper \\\n    liblog \\\n    libcutils \\\n    libutils\ninclude $(BUILD_SHARED_LIBRARY)\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u540e\u9762\u5c31\u62a5\u9519\u4e86\uff0c\u8fd8\u6709\u4e1c\u897f\u8981\u4f9d\u8d56\u3002<br \/>\n\u8fd9\u7bc7\u535a\u5ba2<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/dahailantian1\/article\/details\/78584651?utm_source=blogxgwz4\">https:\/\/blog.csdn.net\/dahailantian1\/article\/details\/78584651?utm_source=blogxgwz4<\/a>\u91cc\u8bf4\u5230\uff1a<br \/>\nAndroid\u7684java\u6838\u5fc3\u5e93\u4e2d\u662f\u65e0\u6cd5\u4f7f\u7528Logcat\u6253\u5370\u7684\uff0c\u56e0\u4e3aAndroid\u7684java\u5c42\u8981\u4f7f\u7528Java\u5e93\uff0c\u5373Java\u5e93\u662fAndroid\u7684Java\u8fd0\u884c\u7684\u524d\u63d0\u3002\uff08\u6211\u7406\u89e3\u7684\u610f\u601d\u5c31\u662f<strong>Logcat\u662f\u5e94\u7528\u5c42\u7528\u7684\u4e1c\u897f\uff0c\u800cLogcat\u53c8\u8981\u4f9d\u8d56libcore\u5e93\uff0c\u6211\u5374\u5728libcore\u5e93\u672c\u672b\u5012\u7f6e\u5730\u4f7f\u7528Logcat\u3002<\/strong>\uff09<br \/>\n\u6240\u4ee5\u5728BaseDexClassLoader.java\u91cc\u6253\u5370\u65e5\u5fd7\uff0c\u76f4\u63a5\u4f7f\u7528Logger\u7c7b(\u6e90\u7801\u4f4d\u7f6e\uff1alibcore\/luni\/src\/main\/java\/java\/util\/logging)\uff0c\u6ca1\u6709\u5fc5\u8981\u6298\u817e\u81ea\u5df1\u641e\u4e2alog\u5de5\u5177\u5e93\u4e86\u3002<\/p>\n<p>\u6e90\u7801\u91ccBaseDexClassLoader.java\u81ea\u8eab\u5c31\u8c03\u7528\u4e86java.util.List\u7b49\u7c7b\uff0c\u6240\u4ee5\u518d\u8c03\u7528\u4e2aLogger\u7c7b\u80af\u5b9a\u6ca1\u6709\u95ee\u9898\u4e86\u3002\u4f46\u662fBaseDexClassLoader\u548cmmap\u51fd\u6570\u91cc\uff0c\u90fd\u4e0d\u80fd\u968f\u4fbf\u5730\u52a0\u65e5\u5fd7\uff0c\u5426\u5219\u7cfb\u7edf\u5c31\u4f1a\u5361\u6b7b\u8d77\u4e0d\u6765\u3002\u8981\u52a0\u4ee5\u9650\u5236\uff1a<\/p>\n<pre><code class=\"line-numbers\">public class BaseDexClassLoader extends ClassLoader {\n    private final DexPathList pathList;\n    private final String TAG = \"BaseDexClassLoader\";\n    private static boolean allowLog = false;\n    \/**\n     * Constructs an instance.\n     *\n     * @param dexPath the list of jar\/apk files containing classes and\n     * resources, delimited by {@code File.pathSeparator}, which\n     * defaults to {@code \":\"} on Android\n     * @param optimizedDirectory directory where optimized dex files\n     * should be written; may be {@code null}\n     * @param libraryPath the list of directories containing native\n     * libraries, delimited by {@code File.pathSeparator}; may be\n     * {@code null}\n     * @param parent the parent class loader\n     *\/\n    public BaseDexClassLoader(String dexPath, File optimizedDirectory,\n            String libraryPath, ClassLoader parent) {\n        super(parent);\n        this.pathList = new DexPathList(this, dexPath, libraryPath, optimizedDirectory);\n        if(allowLog){\n            Logger logger = Logger.getLogger(\"CZLog11\");\n            logger.warning(\"BaseDexClassLoader: \" + \",dexPath = \" + dexPath  + \"\\npathList = \" + pathList);\n        }\n    }\n<\/code><\/pre>\n<p>\u5982\u4e0a\u56fe\uff0c\u6211\u5728\u6e90\u7801\u91cc\u52a0\u4e00\u4e2aboolean\u503c<br \/>\n```allowLog```\u7528\u4ee5\u63a7\u5236Logger\u7684\u521b\u5efa\u548c\u8f93\u51fa\u3002<br \/>\n\u7136\u540e\u7528\u53cd\u5c04\u5199\u4e2a\u63a7\u5236```allowLog```\u7684apk:<\/p>\n<pre><code class=\"line-numbers\">ReflectManger.setStaticField(Class.forName(\"dalvik.system.BaseDexClassLoader\"),\"allowLog\",true);\nLog.e(TAG, \"\u8bbe\u7f6e\u6210\u529f1\");\n<\/code><\/pre>\n<p>\u5f85\u7cfb\u7edf\u8d77\u6765\u5e76\u521d\u59cb\u5316\u5b8c\u6bd5\u4e4b\u540e\uff0c\u518d\u8c03\u7528\u8fd9\u4e2aapk\u5f00\u542f\u65e5\u5fd7\uff0c\u8fd9\u65f6\u8f93\u51fa\u65e5\u5fd7\u5c31\u4e0d\u4f1a\u6709\u5565\u95ee\u9898\u4e86\u3002<\/p>\n<pre><code class=\"line-numbers\">  Object allowLog = ReflectManger.getField(Class.forName(\"dalvik.system.BaseDexClassLoader\"), null, \"allowLog\");\n            Log.e(TAG, \"allowLog: \" + allowLog);\n            ReflectManger.setStaticField(Class.forName(\"dalvik.system.BaseDexClassLoader\"),\"allowLog\",true);\n            Log.e(TAG, \"\u8bbe\u7f6e\u6210\u529f1\");\n            MyClassLoader myClassLoader = new MyClassLoader(Environment.getExternalStorageDirectory().getAbsolutePath(), null, null, getClassLoader());\n            Object allowLog2 = ReflectManger.getField(Class.forName(\"dalvik.system.BaseDexClassLoader\"), null, \"allowLog\");\n            Log.e(TAG, \"allowLog2: \" + allowLog2);\n<\/code><\/pre>\n<p>\u63a7\u5236\u53f0\u8f93\u51fa<\/p>\n<pre><code class=\"line-numbers\">MainActivity: allowLog: false\nMainActivity: \u8bbe\u7f6e\u6210\u529f1\nCZLog11: BaseDexClassLoader: ,dexPath = \/storage\/emulated\/0\ntory \"\/storage\/emulated\/0\"],nativeLibraryDirectories=[\/vendor\/lib, \/system\/lib]]\nMainActivity: allowLog2: true\n<\/code><\/pre>\n<p>CZLog11\u5c31\u662f\u6211\u7cfb\u7edf\u8bbe\u7f6e\u7684\u65e5\u5fd7TAG\u4e86\u3002<\/p>\n<p>\u62d3\u5c55\u4e00\u4e0b\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/d26c56335495\">https:\/\/www.jianshu.com\/p\/d26c56335495<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"4569\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u573a\u666f\uff1a\u60f3\u5728libcore\/dalvik\/src\/main\/java\/dalvik\/system\/Ba [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[199],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4569"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4569"}],"version-history":[{"count":1,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4569\/revisions"}],"predecessor-version":[{"id":4570,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4569\/revisions\/4570"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4569"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}