{"id":13945,"date":"2024-07-11T15:52:33","date_gmt":"2024-07-11T07:52:33","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13945"},"modified":"2024-09-29T15:50:40","modified_gmt":"2024-09-29T07:50:40","slug":"androidruntime-cpp%e7%b1%bb%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13945","title":{"rendered":"AndroidRuntime.cpp\u7c7b\u89e3\u6790"},"content":{"rendered":"<p>\u6e90\u7801\u4f4d\u7f6e\uff1aframeworks\/base\/core\/jni\/AndroidRuntime.cpp<\/p>\n<p>\u5934\u6587\u4ef6\u89c1\uff1a<code>frameworks\/native\/libs\/android_runtime_lazy\/include\/android_runtime\/AndroidRuntime.h<\/code><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13945\/#start%E6%96%B9%E6%B3%95\" title=\"start\u65b9\u6cd5\">start\u65b9\u6cd5<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13945\/#%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%9AstartVm%E6%96%B9%E6%B3%95_%EF%BC%88%E5%88%9B%E5%BB%BAJava%E8%99%9A%E6%8B%9F%E6%9C%BA%EF%BC%8C%E8%A7%A3%E6%9E%90%E8%BF%94%E5%9B%9EJNIEnv%E4%B8%8EJavaVM%EF%BC%89\" title=\"\u7b2c\u4e00\u6b65\uff1astartVm\u65b9\u6cd5 \uff08\u521b\u5efaJava\u865a\u62df\u673a\uff0c\u89e3\u6790\u8fd4\u56deJNIEnv\u4e0eJavaVM\uff09\">\u7b2c\u4e00\u6b65\uff1astartVm\u65b9\u6cd5 \uff08\u521b\u5efaJava\u865a\u62df\u673a\uff0c\u89e3\u6790\u8fd4\u56deJNIEnv\u4e0eJavaVM\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13945\/#%E7%AC%AC%E4%BA%8C%E6%AD%A5%EF%BC%9AstartReg%E6%96%B9%E6%B3%95_%EF%BC%88%E6%B3%A8%E5%86%8C%E7%B3%BB%E7%BB%9F%E7%B1%BB%E7%9A%84jni%E6%96%B9%E6%B3%95%EF%BC%89\" title=\"\u7b2c\u4e8c\u6b65\uff1astartReg\u65b9\u6cd5 \uff08\u6ce8\u518c\u7cfb\u7edf\u7c7b\u7684jni\u65b9\u6cd5\uff09\">\u7b2c\u4e8c\u6b65\uff1astartReg\u65b9\u6cd5 \uff08\u6ce8\u518c\u7cfb\u7edf\u7c7b\u7684jni\u65b9\u6cd5\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13945\/#%E7%AC%AC%E4%B8%89%E6%AD%A5%EF%BC%9A%E5%8F%8D%E5%B0%84%E8%B0%83%E7%94%A8java%E7%B1%BBclassName%E7%9A%84main%E6%96%B9%E6%B3%95%EF%BC%88Android%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%90%AF%E5%8A%A8%E7%AC%AC%E4%B8%80%E4%B8%AAjava%E7%B1%BB%E7%9A%84main%E6%96%B9%E6%B3%95%EF%BC%89\" title=\"\u7b2c\u4e09\u6b65\uff1a\u53cd\u5c04\u8c03\u7528java\u7c7bclassName\u7684main\u65b9\u6cd5\uff08Android\u865a\u62df\u673a\u542f\u52a8\u7b2c\u4e00\u4e2ajava\u7c7b\u7684main\u65b9\u6cd5\uff09\">\u7b2c\u4e09\u6b65\uff1a\u53cd\u5c04\u8c03\u7528java\u7c7bclassName\u7684main\u65b9\u6cd5\uff08Android\u865a\u62df\u673a\u542f\u52a8\u7b2c\u4e00\u4e2ajava\u7c7b\u7684main\u65b9\u6cd5\uff09<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"start%E6%96%B9%E6%B3%95\"><\/span>start\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5176\u4e2d\u5404\u79cd\u5173\u952e\u7c7b\u7684\u5b9a\u4e49\u4f4d\u7f6e\uff1a<br \/>\nJniInvocation:<code>libnativehelper\/JniInvocation.cpp<\/code><br \/>\n<strong>JNIEnv\/JavaVM<\/strong>:<code>libnativehelper\/include_jni\/jni.h<\/code>\uff0c\u8be6\u89c1\uff1a<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14621\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14621<\/a><\/p>\n<pre><code class=\"language-cpp\">\/\/mJavaVM\u4e3a\u5168\u5c40\u9759\u6001\u53d8\u91cf\n 309 \/*static*\/ JavaVM* AndroidRuntime::mJavaVM = NULL;\n\n1128 void AndroidRuntime::start(const char* className, const Vector&lt;String8&gt;&amp; options, bool zygote)\n1129 {\n...\n1169     \/\/const char* kernelHack = getenv(&quot;LD_ASSUME_KERNEL&quot;);\n1170     \/\/ALOGD(&quot;Found LD_ASSUME_KERNEL=&#039;%s&#039;\\n&quot;, kernelHack);\n         1.\u7b2c\u4e00\u6b65\n1172     \/* start the virtual machine *\/\n1173     JniInvocation jni_invocation;\n1174     jni_invocation.Init(NULL);\n1175     JNIEnv* env;\n1176     if (startVm(&amp;mJavaVM, &amp;env, zygote) != 0) {\n1177         return;\n1178     }\n1179     onVmCreated(env);\n\n         2.\u7b2c\u4e8c\u6b65\n1181     \/*\n1182      * Register android functions.\n1183      *\/\n1184     if (startReg(env) &lt; 0) {\n1185         ALOGE(&quot;Unable to register all android natives\\n&quot;);\n1186         return;\n1187     }\n\n         2.\u7b2c\u4e09\u6b65\n1212     \/*\n1213      * Start VM.  This thread becomes the main thread of the VM, and will\n1214      * not return until the VM exits.\n1215      *\/\n1216     char* slashClassName = toSlashClassName(className != NULL ? className : &quot;&quot;);\n1217     jclass startClass = env-&gt;FindClass(slashClassName);\n1218     if (startClass == NULL) {\n1219         ALOGE(&quot;JavaVM unable to locate class &#039;%s&#039;\\n&quot;, slashClassName);\n1220         \/* keep going *\/\n1221     } else {\n1222         jmethodID startMeth = env-&gt;GetStaticMethodID(startClass, &quot;main&quot;,\n1223             &quot;([Ljava\/lang\/String;)V&quot;);\n1224         if (startMeth == NULL) {\n1225             ALOGE(&quot;JavaVM unable to find main() in &#039;%s&#039;\\n&quot;, className);\n1226             \/* keep going *\/\n1227         } else {\n1228             env-&gt;CallStaticVoidMethod(startClass, startMeth, strArray);\n1229 \n1230 #if 0\n1231             if (env-&gt;ExceptionCheck())\n1232                 threadExitUncaughtException(env);\n1233 #endif\n1234         }\n1235     }\n1236     free(slashClassName);\n1237 \n1238     ALOGD(&quot;Shutting down VM\\n&quot;);\n1239     if (mJavaVM-&gt;DetachCurrentThread() != JNI_OK)\n1240         ALOGW(&quot;Warning: unable to detach main thread\\n&quot;);\n1241     if (mJavaVM-&gt;DestroyJavaVM() != 0)\n1242         ALOGW(&quot;Warning: VM did not shut down cleanly\\n&quot;);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%9AstartVm%E6%96%B9%E6%B3%95_%EF%BC%88%E5%88%9B%E5%BB%BAJava%E8%99%9A%E6%8B%9F%E6%9C%BA%EF%BC%8C%E8%A7%A3%E6%9E%90%E8%BF%94%E5%9B%9EJNIEnv%E4%B8%8EJavaVM%EF%BC%89\"><\/span>\u7b2c\u4e00\u6b65\uff1astartVm\u65b9\u6cd5 \uff08\u521b\u5efaJava\u865a\u62df\u673a\uff0c\u89e3\u6790\u8fd4\u56deJNIEnv\u4e0eJavaVM\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6700\u7ec8\u4f1a\u8c03\u7528JNI_CreateJavaVM\u65b9\u6cd5<\/p>\n<pre><code class=\"language-cpp\">1080     \/*\n1081      * Initialize the VM.\n1082      *\n1083      * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.\n1084      * If this call succeeds, the VM is ready, and we can start issuing\n1085      * JNI calls.\n1086      *\/\n1087     if (JNI_CreateJavaVM(pJavaVM, pEnv, &amp;initArgs) &lt; 0) {\n1088         ALOGE(&quot;JNI_CreateJavaVM failed\\n&quot;);\n1089         return -1;\n1090     }<\/code><\/pre>\n<p>JNI_CreateJavaVM\u5b9e\u73b0\u4f4d\u4e8e\uff1aart\/runtime\/jni\/java_vm_ext.cc:1188<br \/>\n\u89c1<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14042\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14042<\/a><\/p>\n<p>\u90a3\u4e48\u6700\u7ec8mJavaVM\u53d8\u91cf\u6307\u5411JavaVMExt\u5b9e\u4f8b\uff0cenv\u6307\u5411JNIEnvExt\u5b9e\u4f8b\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%BA%8C%E6%AD%A5%EF%BC%9AstartReg%E6%96%B9%E6%B3%95_%EF%BC%88%E6%B3%A8%E5%86%8C%E7%B3%BB%E7%BB%9F%E7%B1%BB%E7%9A%84jni%E6%96%B9%E6%B3%95%EF%BC%89\"><\/span>\u7b2c\u4e8c\u6b65\uff1astartReg\u65b9\u6cd5 \uff08\u6ce8\u518c\u7cfb\u7edf\u7c7b\u7684jni\u65b9\u6cd5\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/www.jianshu.com\/p\/e6b9611f3045\">https:\/\/www.jianshu.com\/p\/e6b9611f3045<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/u012124438\/article\/details\/107186262\">https:\/\/blog.csdn.net\/u012124438\/article\/details\/107186262<\/a><\/p>\n<pre><code class=\"language-cpp\">1610 \/*\n1611  * Register android native functions with the VM.\n1612  *\/\n1613 \/*static*\/ int AndroidRuntime::startReg(JNIEnv* env)\n1614 {\n1615     ATRACE_NAME(&quot;RegisterAndroidNatives&quot;);\n1616     \/*\n1617      * This hook causes all future threads created in this process to be\n1618      * attached to the JavaVM.  (This needs to go away in favor of JNI\n1619      * Attach calls.)\n1620      *\/\n1621     androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);\n1622 \n1623     ALOGV(&quot;--- registering native functions ---\\n&quot;);\n1624 \n1625     \/*\n1626      * Every &quot;register&quot; function calls one or more things that return\n1627      * a local reference (e.g. FindClass).  Because we haven&#039;t really\n1628      * started the VM yet, they&#039;re all getting stored in the base frame\n1629      * and never released.  Use Push\/Pop to manage the storage.\n1630      *\/\n1631     env-&gt;PushLocalFrame(200);\n1632 \n1633     if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) &lt; 0) {\n1634         env-&gt;PopLocalFrame(NULL);\n1635         return -1;\n1636     }\n1637     env-&gt;PopLocalFrame(NULL);\n1638 \n1639     \/\/createJavaThread(&quot;fubar&quot;, quickTest, (void*) &quot;hello&quot;);\n1640 \n1641     return 0;\n1642 }<\/code><\/pre>\n<p>\u2192gRegJNI\u6570\u7ec4\u521d\u59cb\u5316\uff1a\u5143\u7d20\u7c7b\u578b\u4e3aRegJNIRec\u7ed3\u6784\u4f53\uff0c\u7ed3\u6784\u5b58\u50a8mProc\u7684\u51fd\u6570\u6307\u9488\u3002<\/p>\n<pre><code class=\"language-cpp\">1400 #ifdef NDEBUG\n1401     #define REG_JNI(name)      { name }\n1402     struct RegJNIRec {\n1403         int (*mProc)(JNIEnv*);\n1404     };\n1405 #else\n1406     #define REG_JNI(name)      { name, #name }\n1407     struct RegJNIRec {\n1408         int (*mProc)(JNIEnv*);\n1409         const char* mName;\n1410     };\n1411 #endif\n1412 \n1413 typedef void (*RegJAMProc)();\n1414 \n1415 static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)\n1416 {\n1417     for (size_t i = 0; i &lt; count; i++) {\n1418         if (array[i].mProc(env) &lt; 0) {\n1419 #ifndef NDEBUG\n1420             ALOGD(&quot;----------!!! %s failed to load\\n&quot;, array[i].mName);\n1421 #endif\n1422             return -1;\n1423         }\n1424     }\n1425     return 0;\n1426}\n1427 \n1428 static const RegJNIRec gRegJNI[] = {\n1429     REG_JNI(register_android_util_SeempLog),\n         ...\n    }<\/code><\/pre>\n<p>jni\u6ce8\u518c\u539f\u7406\u793a\u4f8b\uff1a<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14550\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14550<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%B8%89%E6%AD%A5%EF%BC%9A%E5%8F%8D%E5%B0%84%E8%B0%83%E7%94%A8java%E7%B1%BBclassName%E7%9A%84main%E6%96%B9%E6%B3%95%EF%BC%88Android%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%90%AF%E5%8A%A8%E7%AC%AC%E4%B8%80%E4%B8%AAjava%E7%B1%BB%E7%9A%84main%E6%96%B9%E6%B3%95%EF%BC%89\"><\/span>\u7b2c\u4e09\u6b65\uff1a\u53cd\u5c04\u8c03\u7528java\u7c7bclassName\u7684main\u65b9\u6cd5\uff08Android\u865a\u62df\u673a\u542f\u52a8\u7b2c\u4e00\u4e2ajava\u7c7b\u7684main\u65b9\u6cd5\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5229\u7528\u4e0a\u9762startVm\u65b9\u6cd5\u5f97\u5230\u7684JNIEnv\u6307\u9488<br \/>\n\u5bf9\u4e8ezygote\uff0cclassName\u5c31\u662f<code>com.android.internal.os.ZygoteInit<\/code> java\u7c7b\uff0c\u6e90\u7801\u89c1\uff1a<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13955\">http:\/\/xinyiworld.top\/wordpress_it\/?p=13955<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"13945\" 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>\u6e90\u7801\u4f4d\u7f6e\uff1aframeworks\/base\/core\/jni\/AndroidRuntime.cpp \u5934 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1063],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13945"}],"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=13945"}],"version-history":[{"count":27,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13945\/revisions"}],"predecessor-version":[{"id":14819,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13945\/revisions\/14819"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13945"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}