{"id":247,"date":"2020-11-04T20:53:16","date_gmt":"2020-11-04T12:53:16","guid":{"rendered":"http:\/\/8.154.33.202\/?p=247"},"modified":"2020-11-04T21:12:09","modified_gmt":"2020-11-04T13:12:09","slug":"android-%e6%b7%bb%e5%8a%a0%e6%a8%a1%e5%9d%97%e5%b9%b6%e4%bd%bf%e7%94%a8jni%e8%b0%83%e7%94%a8c","status":"publish","type":"post","link":"http:\/\/8.154.33.202\/?p=247","title":{"rendered":"android \u6dfb\u52a0\u6a21\u5757\u5e76\u4f7f\u7528JNI\u8c03\u7528c++"},"content":{"rendered":"\n<p>\u9996\u5148\u65b0\u5efa\u4e00\u4e2a\u6a21\u5757\uff0c\u6bd4\u5982Android Library\u3002\u5728\u8be5\u6a21\u5757\u7684java\u7c7b\u4e2d\uff0c\u9996\u5148\u8f7d\u5165c++\u7684\u5171\u4eab\u5e93\uff0c\u8bed\u53e5\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static{\n      System.loadLibrary(\"libname\");\n} \n\/\/libname\u5c31\u662f\u4e0b\u9762\u8981\u5efa\u7acb\u7684cpp\u6587\u4ef6\u7684\u540d\u79f0<\/pre>\n\n\n\n<p>\u540e\u9762\u58f0\u660e\u4f7f\u7528JNI\u7684java\u65b9\u6cd5\uff0c\u6bd4\u5982<br>public native String stringFromJNI();<br>\u5176\u4e2d\u7684native\u4e0d\u53ef\u7701\u7565<\/p>\n\n\n\n<p> \u5b8c\u6210\u540e\u5728\u8be5\u6a21\u5757\u4e0bsrc\/main\u76ee\u5f55\u4e0b\u65b0\u5efacpp\u6587\u4ef6\u5939\uff0c \u7136\u540e\u5728cpp\u6587\u4ef6\u5939\u4e0b\u65b0\u5efacpp\u6587\u4ef6(\u6587\u4ef6\u540d\u8981\u4e0ejava\u7c7b\u4e2d\u8f7d\u5165\u5171\u4eab\u5e93\u7684\u540d\u79f0\u76f8\u540c)\uff0c\u5b9e\u73b0\u8be5JNI\u65b9\u6cd5\u3002\u6240\u5bf9\u5e94\u7684c++\u51fd\u6570\u540d\u5f62\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;jni.h> \/\/\u5fc5\u8981\u8bed\u53e5\n\nextern \"C\"\nJNIEXPORT jstring JNICALL\nJava_\u5305\u540d_\u7c7b\u540d_\u65b9\u6cd5\u540d(JNIEnv *env) \u6ce8\uff1a\u4e00\u822c\u5728\u5199\u7684\u65f6\u5019AS\u4f1a\u7ed9\u51fa\u63d0\u793a\uff0c\u5efa\u8bae\u76f4\u63a5\u4f7f\u7528\u63d0\u793a\u751f\u6210\u3002<\/pre>\n\n\n\n<p>cpp\u6587\u4ef6\u5199\u5b8c\u540e, \u518d\u5728\u8be5\u6587\u4ef6\u5939\u4e0b\u65b0\u5efaCMakelists.txt\u6587\u4ef6\u3002\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># For more information about using CMake with Android Studio, read the\n# documentation: https:\/\/d.android.com\/studio\/projects\/add-native-code.html\n# Sets the minimum version of CMake required to build the native library.\n\n<strong>cmake_minimum_required(VERSION 3.4.1)<\/strong>\n\n# Creates and names a library, sets it as either STATIC\n# or SHARED, and provides the relative paths to its source code.\n# You can define multiple libraries, and CMake builds them for you.\n# Gradle automatically packages shared libraries with your APK.\n\n<strong>add_library( # Sets the name of the library.\n             libname\n\n             # Sets the library as a shared library.\n             SHARED\n\n             # Provides a relative path to your source file(s).\n             src\/main\/cpp\/libname.cpp)<\/strong>\n\n# Searches for a specified prebuilt library and stores the path as a\n# variable. Because CMake includes system libraries in the search path by\n# default, you only need to specify the name of the public NDK library\n# you want to add. CMake verifies that the library exists before\n# completing its build.\n\n<strong>find_library( # Sets the name of the path variable.\n              log-lib\n\n\n              # Specifies the name of the NDK library that\n              # you want CMake to locate.\n              log )<\/strong>\n\n# Specifies libraries CMake should link to your target library. You\n# can link multiple libraries, such as libraries you define in this\n# build script, prebuilt third-party libraries, or system libraries.\n\n<strong>target_link_libraries( # Specifies the target library.\n                       libname\n\n                       # Links the target library to the log library\n                       # included in the NDK.\n                       ${log-lib} )<\/strong>\n<\/pre>\n\n\n\n<p>\u7136\u540e\u518d\u5728\u8be5\u6a21\u5757\u4e0b\u7684build.gradle\u4e2d\u6dfb\u52a0\u5bf9c++\u7684\u7f16\u8bd1\u914d\u7f6e\u4fe1\u606f\u3002\u5728defaultConfig\u8282\u70b9\u4e0b\uff0c\u6dfb\u52a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">externalNativeBuild {\n    cmake {\n        cppFlags \"-fexceptions\"\n        arguments \"-DANDROID_STL=c++_shared\"\n    }\n    ndk{\n        abiFilters 'armeabi-v7a', 'arm64-v8a' ,'x86'\n    }\n}<\/pre>\n\n\n\n<p>\u4f5c\u7528\u662f\u914d\u7f6e\u4e3a\u5171\u4eab\u5e93\uff0c\u4e0e\u8bbe\u7f6e\u76ee\u6807\u5e73\u53f0\u3002\u63a5\u7740\u518d\u5c06CMakeLists.txt\u5173\u8054\u5230\u6a21\u5757\uff0c\u5728android\u8282\u70b9\u4e0b\u6dfb\u52a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">externalNativeBuild {\n    cmake {\n        path \"src\/main\/cpp\/CMakeLists.txt\"\n    }\n}<\/pre>\n\n\n\n<p>\u81f3\u6b64\u6a21\u5757\u7684\u4efb\u52a1\u7b97\u662f\u5b8c\u6210\u4e86\u3002\u53ef\u4ee5\u5728\u5176\u5b83\u6a21\u5757\u4e2d\u5f15\u5165\u8be5\u6a21\u5757\u4f7f\u7528\u3002\u5f15\u5165\u65b9\u5f0f\u4e3a\uff1a<br>import \u8be5\u6a21\u5757\u5305\u540d.\u6a21\u5757\u7c7b\u540d;<br>\u53e6\u5916\u8981\u5728\u5176\u6a21\u5757\u7684build.gradle\u4e2d\uff0cdependencies\u8282\u70b9\u4e2d\uff0c\u6dfb\u52a0<\/p>\n\n\n\n<p>implementation project(&#8216;:\u6a21\u5757\u540d&#8217;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u65b0\u5efa\u4e00\u4e2a\u6a21\u5757\uff0c\u6bd4\u5982Android Library\u3002\u5728\u8be5\u6a21\u5757\u7684java\u7c7b\u4e2d\uff0c\u9996\u5148\u8f7d\u5165c++\u7684\u5171\u4eab\u5e93\uff0c\u8bed\u53e5\u5982\u4e0b &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/8.154.33.202\/?p=247\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201candroid \u6dfb\u52a0\u6a21\u5757\u5e76\u4f7f\u7528JNI\u8c03\u7528c++\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/247"}],"collection":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=247"}],"version-history":[{"count":3,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":250,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/247\/revisions\/250"}],"wp:attachment":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}