{"id":1628,"date":"2023-03-25T21:20:53","date_gmt":"2023-03-25T13:20:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1628"},"modified":"2023-04-23T22:03:00","modified_gmt":"2023-04-23T14:03:00","slug":"dynamic-registration-of-jni-functions-for-jni-and-ndk-development","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/dynamic-registration-of-jni-functions-for-jni-and-ndk-development\/","title":{"rendered":"JNI\u548cNDK\u5f00\u53d1\u4e4b\u52a8\u6001\u6ce8\u518cJNI\u51fd\u6570"},"content":{"rendered":"<h2>\u4ec0\u4e48\u662f\u9759\u6001\u6ce8\u518c<\/h2>\n<p>\u9759\u6001\u6ce8\u518c\u662f\u6839\u636eJNI\u7684\u65b9\u6cd5\u540d\u5b57\u6765\u5b9e\u73b0\u4e0eJava\u65b9\u6cd5\u7684\u6620\u5c04\u5bf9\u5e94\u5173\u7cfb\u3002\u5982\u4e0b\u6240\u793a\uff0c\u65b9\u6cd5\u540d\u7684\u7ec4\u6210\u662f\u6839\u636e<code>JNIEXPORT jstring JNICALL Java_+\u5305\u540d+_\u7c7b\u540d+_\u65b9\u6cd5\u540d<\/code>\u7ec4\u6210\u3002\u5305\u540d\u76f4\u63a5\u8981\u7528_\u8fde\u63a5\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-cpp\">JNIEXPORT jstring JNICALL Java_com_common_secure_SecureProtocol_encrypt(JNIEnv *, jobject, jstring)<\/code><\/pre>\n<ul>\n<li>\u4f18\u70b9<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u4f7f\u7528javah\u547d\u4ee4\u5b9e\u73b0\u5934\u6587\u4ef6\u548c\u51fd\u6570\u540d\u7684\u751f\u6210\u3002<\/p>\n<ul>\n<li>\u7f3a\u70b9<\/li>\n<\/ul>\n<p>\u56e0\u4e3a\u65b9\u6cd5\u7684\u540d\u79f0\u8fa8\u8bc6\u5ea6\u8f83\u9ad8\uff0c\u6240\u4ee5\u65b9\u6cd5\u5b89\u5168\u6027\u4e0d\u9ad8\uff0c\u4f7f\u7528IDA\u53cd\u8c03\u8bd5so\u6587\u4ef6\u7684\u65f6\u5019\u5f88\u5bb9\u6613\u66b4\u9732\u63a5\u53e3\u4fe1\u606f\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u52a8\u6001\u6ce8\u518c<\/h2>\n<p>\u52a8\u6001\u6ce8\u518c\u662f\u4f7f\u7528<code>RegisterNatives<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e0eJava\u65b9\u6cd5\u7684\u6620\u5c04\u5bf9\u5e94\u5173\u7cfb\u3002\u51fd\u6570\u539f\u578b\uff1a<\/p>\n<pre><code class=\"language-cpp\">jint RegisterNatives(jclass clazz, const JNINativeMethod* methods, jint nMethods)<\/code><\/pre>\n<ul>\n<li><code>jclass clazz<\/code>\u53c2\u6570\u662fjava\u5bf9\u5e94\u7684\u7c7b<\/li>\n<li><code>JNINativeMethod* methods<\/code>\u662fjava\u7c7b\u5bf9\u5e94\u7684\u51fd\u6570<\/li>\n<\/ul>\n<p><code>JNINativeMethod<\/code>\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u7ed3\u6784\u5982\u4e0b<\/p>\n<pre><code class=\"language-cpp\">ypedef struct {\n    const char* name;\n    const char* signature;\n    void*       fnPtr;\n} JNINativeMethod;<\/code><\/pre>\n<p>\u7ed3\u6784\u4f53\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570<code>name<\/code>\u662fjava \u65b9\u6cd5\u540d\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>signature<\/code>\u7528\u4e8e\u63cf\u8ff0\u65b9\u6cd5\u7684\u53c2\u6570\u4e0e\u8fd4\u56de\u503c\uff1b\u7b2c\u4e09\u4e2a\u53c2\u6570<code>fnPtr<\/code>\u662f\u51fd\u6570\u6307\u9488\uff0c\u6307\u5411 jni \u51fd\u6570\uff1b<\/p>\n<p>\u5176\u4e2d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>signature<\/code>\u4f7f\u7528\u5b57\u7b26\u4e32\u8bb0\u5f55\u65b9\u6cd5\u7684\u53c2\u6570\u4e0e\u8fd4\u56de\u503c\uff0c\u5177\u4f53\u683c\u5f0f\u5f62\u5982<code>()V<\/code>\u3001<code>(II)V<\/code>\uff0c\u5176\u4e2d\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u62ec\u53f7\u5185\u8868\u793a\u7684\u662f\u53c2\u6570\uff0c\u62ec\u53f7\u53f3\u4fa7\u8868\u793a\u7684\u662f\u8fd4\u56de\u503c\uff1b<\/p>\n<pre><code class=\"language-cpp\">JNINativeMethod nativeMethods[] = {\n     {&quot;encrypt&quot;, &quot;(Ljava\/lang\/String;)Ljava\/lang\/String;&quot;, (void *) encrypt}\n};<\/code><\/pre>\n<ul>\n<li><code>jint nMethods<\/code>\u662f\u6ce8\u518c\u7684\u51fd\u6570\u7684\u4e2a\u6570<\/li>\n<\/ul>\n<h2>\u5b9e\u73b0\u52a8\u6001\u6ce8\u518c<\/h2>\n<p>\u5728Java\u5c42MainActivity\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public native String encrypt(String data);<\/code><\/pre>\n<p>\u5728<code>JNI_OnLoad<\/code>\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u52a8\u6001\u6ce8\u518c<\/p>\n<pre><code class=\"language-cpp\">#include &lt;jni.h&gt;\n#include &lt;string&gt;\n#include &lt;android\/log.h&gt;\n#include &lt;cassert&gt;\n#include &lt;cstdlib&gt;\n#include &lt;iostream&gt;\n\nusing namespace std;\n\n#define TAG &quot;LOG_CORE:&quot;\n#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)\n\n\/\/native \u65b9\u6cd5\u5b9e\u73b0\njstring encrypt(JNIEnv *env) {\n    return env-&gt;NewStringUTF(&quot;JNI\u52a8\u6001\u6ce8\u518c\u6210\u529f&quot;);\n}\n\n\/*\n\u9700\u8981\u6ce8\u518c\u7684\u51fd\u6570\u5217\u8868\uff0c\u653e\u5728JNINativeMethod \u7c7b\u578b\u7684\u6570\u7ec4\u4e2d\n\u4ee5\u540e\u5982\u679c\u9700\u8981\u589e\u52a0\u51fd\u6570\uff0c\u53ea\u9700\u5728\u8fd9\u91cc\u6dfb\u52a0\u5373\u53ef\n\u53c2\u6570\uff1a\n1.java\u4e2d\u7528native\u5173\u952e\u5b57\u58f0\u660e\u7684\u51fd\u6570\u540d\n2.\u7b7e\u540d\uff08\u4f20\u8fdb\u6765\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u8bf4\u660e\uff09\n3.C\/C++\u4e2d\u5bf9\u5e94\u51fd\u6570\u7684\u51fd\u6570\u540d\uff08\u5730\u5740\uff09\n*\/\nstatic JNINativeMethod nativeMethods[] = {\n        {&quot;encrypt&quot;, &quot;(Ljava\/lang\/String;)Ljava\/lang\/String;&quot;, (void *) encrypt}\n};\n\n\/\/\u6b64\u51fd\u6570\u901a\u8fc7\u8c03\u7528RegisterNatives\u65b9\u6cd5\u6765\u6ce8\u518c\u6211\u4eec\u7684\u51fd\u6570\nstatic int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *nativeMethods,int methodsNum) {\n    jclass clazz;\n    \/\/\u627e\u5230\u58f0\u660enative\u65b9\u6cd5\u7684\u7c7b\n    clazz = env-&gt;FindClass(className);\n    if (clazz == NULL) {\n        return JNI_FALSE;\n    }\n    \/\/\u6ce8\u518c\u51fd\u6570 \u53c2\u6570\uff1ajava\u7c7b \u6240\u8981\u6ce8\u518c\u7684\u51fd\u6570\u6570\u7ec4 \u6ce8\u518c\u51fd\u6570\u7684\u4e2a\u6570\n    if (env-&gt;RegisterNatives(clazz, nativeMethods, methodsNum) &lt; 0) {\n        return JNI_FALSE;\n    }\n    return JNI_TRUE;\n}\n\nstatic int registerNatives(JNIEnv *env) {\n    \/\/\u6307\u5b9a\u7c7b\u7684\u8def\u5f84\uff0c\u901a\u8fc7FindClass \u65b9\u6cd5\u6765\u627e\u5230\u5bf9\u5e94\u7684\u7c7b\n    const char *className = &quot;com\/rzr\/jni\/register\/MainActivity&quot;;\n    return registerNativeMethods(env, className, nativeMethods,\n                                 sizeof(nativeMethods) \/ sizeof(nativeMethods[0]));\n}\n\njint JNI_OnLoad(JavaVM *vm, void *reserved) {\n    LOGE(&quot;JNI_OnLoad \u521d\u59cb\u5316&quot;);\n    JNIEnv *env = NULL;\n    jint result = -1;\n    if (vm-&gt;GetEnv((void **) &amp;env, JNI_VERSION_1_4) != JNI_OK) {\n        return -1;\n    }\n    assert(env != NULL);\n    \/\/\u6ce8\u518c\u51fd\u6570 registerNatives -&gt;registerNativeMethods -&gt;env-&gt;RegisterNatives\n    if (!registerNatives(env)) {\n        return -1;\n    }\n    result = JNI_VERSION_1_4;\n    return result;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u9759\u6001\u6ce8\u518c \u9759\u6001\u6ce8\u518c\u662f\u6839\u636eJNI\u7684\u65b9\u6cd5\u540d\u5b57\u6765\u5b9e\u73b0\u4e0eJava\u65b9\u6cd5\u7684\u6620\u5c04\u5bf9\u5e94\u5173\u7cfb\u3002\u5982\u4e0b\u6240\u793a\uff0c\u65b9\u6cd5\u540d\u7684\u7ec4\u6210\u662f\u6839\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[133],"class_list":["post-1628","post","type-post","status-publish","format-standard","hentry","category-android-ndk","tag-jni"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1628","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=1628"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1628\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}