{"id":3482,"date":"2022-07-13T14:43:34","date_gmt":"2022-07-13T06:43:34","guid":{"rendered":"https:\/\/learn-house.idv.tw\/?p=3482"},"modified":"2022-07-27T22:30:51","modified_gmt":"2022-07-27T14:30:51","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8aosp%e4%b8%8b%e7%b7%a8%e8%ad%afjni","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=3482","title":{"rendered":"[Android]\u5982\u4f55\u5728AOSP\u4e0b\u7de8\u8b6fJNI"},"content":{"rendered":"<p>JNI\u662f\u4ec0\u9ebc\uff0c\u6211\u5c31\u4e0d\u591a\u4f5c\u4ecb\u7d39\uff0c\u4f30\u72d7\u4e00\u4e0b\u61c9\u8a72\u8cc7\u6599\u5c31\u4e00\u5806\uff0c\u800c\u4e14\u4e5f\u6703\u6709\u5f88\u591a\u6559\u5b78\u3002\u4e0d\u904e\u4e00\u822c\u627e\u5230\u7684\u6559\u5b78\u90fd\u662f\u900f\u904eAndroid Studio\u5bebAPP\u4f7f\u7528JNI\u7684\u65b9\u5f0f\u3002\u537b\u627e\u4e0d\u5230\u5982\u679c\u662fAOSP\u958b\u767c\u8005\uff0c\u5982\u4f55\u64b0\u5bebJNI\u8207\u914d\u7f6eAndroid.mk\u3002<\/p>\n<p>\u9996\u5148\u5728\u4f60\u7684\u4e0a\u5c64JAVA\u7684APP\u65b0\u589eclass\u4ecb\u9762\u4f86\u63d0\u4f9b\u547c\u53eb\u5e95\u5c64\u7684API\uff0c\u5176\u4e2dSystem.loadLibrary\u7684\u90e8\u5206\u9084\u53ef\u4ee5\u4e0d\u7528\u5148\u5beb\uff0c\u4e0d\u904e\u5beb\u4e86\u4e5f\u4e0d\u6703\u5f71\u97ff\u7522\u51faheader\u6a94\u3002<br \/>\n\u9019\u88e1\u5ba3\u544a\u4e00\u500b\u547c\u53eb\u5e95\u5c64\u7684\u51fd\u5f0f\uff0c\u5e36\u5165\u6574\u6578\u503c\u4e26\u56de\u50b3\u5b57\u4e32\u3002<!--more--><br \/>\nJniTest.java\u7684\u5167\u5bb9\u5982\u4e0b\uff1a<br \/>\n[java]<br \/>\npackage com.example.jni;<\/p>\n<p>public class JniTest {<\/p>\n<p>    static {<br \/>\n        System.loadLibrary(&quot;libtest_jni&quot;);<br \/>\n    }<\/p>\n<p>    public native String passInt2JNIAndReturnString(int value);<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>\u518d\u4f86\u5229\u7528javah\u547d\u4ee4\u4f86\u751f\u6210c\\c++\u7684\u6a19\u982d\u6a94\uff0c-d\u662fheader\u6a94\u8981\u7522\u751f\u5728\u54ea\u500b\u8def\u5f91\u3002<\/p>\n<table style=\"border: 1px solid #fcfcfc; padding: 4px; background-color: #000000;\" width=\"100%\">\n<tbody>\n<tr>\n<td>\n<div style=\"padding: 8px; color: #ffffff; line-height: 1.5;\">$.\/prebuilts\/jdk\/jdk9\/linux-x86\/bin\/javah -v -jni -d .\/vendor\/yourpath\/YourApp &#8211;class-path .\/vendor\/yourpath\/YourApp\/main\/java com.example.jni.JniTest<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u57f7\u884c\u5f8c\u7522\u751fcom_example_jni_JniTest.h\uff0c\u5176\u5167\u5bb9\u5982\u4e0b\uff0c\u9019\u500b\u6a94\u6848\u4e0d\u9700\u8981\u53bb\u4fee\u6539\u4ed6\uff1a<br \/>\n[c]<br \/>\n\/* DO NOT EDIT THIS FILE &#8211; it is machine generated *\/<br \/>\n#include &lt;jni.h&gt;<br \/>\n\/* Header for class com_example_jni_JniTest *\/<\/p>\n<p>#ifndef _Included_com_example_jni_JniTest<br \/>\n#define _Included_com_example_jni_JniTest<br \/>\n#ifdef __cplusplus<br \/>\nextern &quot;C&quot; {<br \/>\n#endif<br \/>\n\/*<br \/>\n * Class:     com_example_jni_JniTest<br \/>\n * Method:    passInt2JNIAndReturnString<br \/>\n * Signature: (I)Ljava\/lang\/String;<br \/>\n *\/<br \/>\nJNIEXPORT jstring JNICALL Java_com_example_jni_JniTest_passInt2JNIAndReturnString<br \/>\n  (JNIEnv *, jobject, jint);<\/p>\n<p>#ifdef __cplusplus<br \/>\n}<br \/>\n#endif<br \/>\n#endif<br \/>\n[\/c]<\/p>\n<p>\u5c07\u9019\u500bheader\u653e\u5230\u4f60\u7684jni lib\u540c\u5c64\u76ee\u9304\u4e0b\uff0c\u64b0\u5beb\u4f60\u7684\u7a0b\u5f0f\u78bc\uff0cjniTest.cpp<br \/>\n[c]<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;log\/log.h&gt;<br \/>\n#include &quot;com_example_jni_JniTest.h&quot;<\/p>\n<p>JNIEXPORT jstring JNICALL Java_com_example_jni_JniTest_passInt2JNIAndReturnString (JNIEnv *env, jobject, jint value) {<br \/>\n   ALOGD(&quot;your pass value:%d&quot;, value);<br \/>\n   return env-&gt;NewStringUTF(env, &quot;You got it!!&quot;);<br \/>\n}<br \/>\n[\/c]<\/p>\n<p>\u518d\u4f86\u64b0\u5beb\u7de8\u8b6f\u4f60\u7684jni lib\u7684Android.mk<br \/>\n[c]<br \/>\nLOCAL_PATH := $(call my-dir)<br \/>\ninclude $(CLEAR_VARS)<br \/>\nLOCAL_MODULE    := libtest_jni<br \/>\nLOCAL_SRC_FILES := jniTest.cpp<br \/>\ninclude $(BUILD_SHARED_LIBRARY)<br \/>\n[\/c]<br \/>\nAndroid.bp\u5247\u5982\u4e0b\uff1a<br \/>\n[c]<br \/>\ncc_library_shared {<br \/>\n    name: &quot;libtest_jni&quot;,<br \/>\n    srcs: [<br \/>\n        &quot;jniTest.cpp&quot;,<br \/>\n    ],<br \/>\n}<br \/>\n[\/c]<\/p>\n<p>\u56de\u5230\u4f60\u4e0a\u5c64JAVA\u7684APP\uff0c\u64b0\u5bebAndroid.mk\uff0c\u5ba3\u544a\u8981\u4f7f\u7528JNI\u7684lib\uff0c\u53ea\u8981\u52a0\u5165\u9019\u5169\u500b\u5ba3\u544a\u5373\u53ef<br \/>\n[c]LOCAL_REQUIRED_MODULES += libtest_jni #\u9019\u500b\u662f\u8aaa\u8981\u5148\u53bb\u7de8\u8b6flibtest_jni<br \/>\nLOCAL_JNI_SHARED_LIBRARIES := libtest_jni #\u9019\u500b\u624d\u662f\u4f7f\u7528\u7684\u5ba3\u544a[\/c]<br \/>\nAndroid.bp\u5247\u4e00\u6a23\u4e5f\u662f\u52a0\u5165\u9019\u5169\u500b\u5ba3\u544a\uff1a<br \/>\n[c]<br \/>\n    required: [&quot;libsound_jni&quot;],<br \/>\n    jni_libs: [&quot;libsound_jni&quot;],<br \/>\n[\/c]<\/p>\n<p>\u4e0a\u5c64JAVA\u7684APP\u547c\u53eb\u65b9\u5f0f\uff0c\u4ee5\u4e0b\u5c31\u5beb\u500b\u5927\u6982\uff0c\u5c31\u6c92\u9644\u5b8c\u6574\u7684\u7a0b\u5f0f\u78bc<br \/>\n[java]<br \/>\nimport com.example.jni.TestJNI;<\/p>\n<p>JniTest jnitest = new JniTest();<br \/>\nLog.d(&quot;JNI&quot;, jnitest.passInt2JNIAndReturnString(3));<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JNI\u662f\u4ec0\u9ebc\uff0c\u6211\u5c31\u4e0d\u591a\u4f5c\u4ecb\u7d39\uff0c\u4f30\u72d7\u4e00\u4e0b\u61c9\u8a72\u8cc7\u6599\u5c31\u4e00\u5806\uff0c\u800c\u4e14\u4e5f\u6703\u6709\u5f88\u591a\u6559\u5b78\u3002\u4e0d\u904e\u4e00\u822c\u627e\u5230\u7684\u6559\u5b78\u90fd\u662f\u900f\u904eAndro<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=3482\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3482","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3482"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3482\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}