{"rsdb":{"rid":"387735","subhead":"","postdate":"0","aid":"270085","fid":"98","uid":"1","topic":"1","content":"
\n

\u76ee\u5f55
1\u3001\u57fa\u7840\u6982\u5ff5<\/a>
\u251c\u2500\u25001.1\u3001JNI<\/a>
\u251c\u2500\u25001.2\u3001NDK<\/a>
\u251c\u2500\u25001.3\u3001CMake\u4e0endk-build<\/a>
2\u3001\u73af\u5883\u642d\u5efa<\/a>
3\u3001Native C++<\/a> \u9879\u76ee\uff08HelloWord\u6848\u4f8b\uff09<\/a>
\u251c\u2500\u2500 3.1\u3001\u9879\u76ee\u521b\u5efa\uff08java<\/a>\u3001kotlin\uff09<\/a>
\u251c\u2500\u2500 3.2\u3001CMake\u7684\u5e94\u7528\uff08\u8be6\u7ec6\u8bb2\u89e3\uff09<\/a>
\u251c\u2500\u2500 3.3\u3001ndk-build\u7684\u5e94\u7528\uff08\u8be6\u7ec6\u8bb2\u89e3\uff09<\/a><\/p> \n

1\u3001\u57fa\u7840\u6982\u5ff5<\/h2> \n

1.1\u3001JNI<\/h4> \n
\n

JNI\uff08Java Native Interface\uff09Java\u672c\u5730\u63a5\u53e3\uff0c\u4f7f\u5f97Java\u4e0eC\/C++<\/a>\u5177\u6709\u4ea4\u4e92\u80fd\u529b<\/p> \n <\/blockquote> \n

1.2\u3001NDK<\/h4> \n
\n

NDK\uff08Native Development Kit\uff09 \u672c\u5730\u5f00\u53d1\u5de5\u5177\u5305\uff0c\u5141\u8bb8\u4f7f\u7528\u539f\u751f\u8bed\u8a00\uff08C\u548cC++\uff09\u6765\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u5206\u529f\u80fd<\/p> \n <\/blockquote> \n

Android NDK\u5f00\u53d1\u7684\u4e3b\u8981\u4f5c\u7528\uff1a<\/p> \n

1\u3001\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u63d0\u5347\u5e94\u7528\u6027\u80fd\uff1b\n2\u3001\u4ee3\u7801\u4fdd\u62a4\uff0c\u589e\u52a0\u53cd\u7f16\u8bd1\u96be\u5ea6\uff1b\n3\u3001\u751f\u6210\u5e93\u6587\u4ef6\uff0c\u5e93\u53ef\u91cd\u590d\u4f7f\u7528\uff0c\u4e5f\u4fbf\u4e8e\u5e73\u53f0\u3001\u9879\u76ee\u95f4\u79fb\u690d\uff1b<\/code><\/pre> \n 

1.3\u3001CMake\u4e0endk-build<\/h4> \n

\u5f53\u6211\u4eec\u57fa\u4e8eNDK\u5f00\u53d1\u51fanative\u529f\u80fd\u540e\uff0c\u901a\u5e38\u9700\u8981\u7f16\u8bd1\u6210\u5e93\u6587\u4ef6\uff0c\u7ed9Android\u9879\u76ee\u4f7f\u7528\u3002
\u76ee\u524d\uff0c\u6709\u4e24\u79cd\u4e3b\u6d41\u7684\u7f16\u8bd1\u65b9\u5f0f\uff1a__CMake__\u4e0endk-build<\/strong><\/p> \n

\n

__CMake__\u4e0e__ndk-build__\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u7f16\u8bd1\u5de5\u5177\uff08\u4e0eAndroid\u4ee3\u7801\u548cC\/C++\u4ee3\u7801\u65e0\u5173\uff09<\/p> \n <\/blockquote> \n

CMake<\/strong><\/p> \n

CMake\u662fAndroidstudio2.2\u4e4b\u540e\u5f15\u5165\u7684\u8de8\u5e73\u53f0\u7f16\u8bd1\u5de5\u5177\uff08\u7279\u70b9\uff1a\u7b80\u5355\u6613\u7528\uff0c2.2\u4e4b\u540e\u662f\u9ed8\u8ba4\u7684NDK\u7f16\u8bd1\u5de5\u5177\uff09\n\n\u5982\u4f55\u914d\u7f6e\uff1a\n   1\u3001\u521b\u5efaCMakeLists.txt\u6587\u4ef6\uff0c\u914d\u7f6eCMake\u5fc5\u8981\u53c2\u6570\uff1b\n   2\u3001\u4f7f\u7528gradle\u914d\u7f6eCMakeLists.txt\u4ee5\u53canative\u76f8\u5173\u53c2\u6570\uff1b\n\n\u5982\u4f55\u7f16\u8bd1\u5e93\u6587\u4ef6\uff1a\n   1\u3001Android Studio\u6267\u884cBuild\u5373\u53ef\uff1b<\/code><\/pre> \n 

ndk-build<\/strong><\/p> \n

ndk-build\u662fNDK\u4e2d\u5305\u542b\u7684\u811a\u672c\u5de5\u5177\uff08\u53ef\u5728NDK\u76ee\u5f55\u4e0b\u627e\u5230\u8be5\u5de5\u5177\uff0c\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0c\u901a\u5e38\u914d\u7f6eNDK\u7684\u73af\u5883\u53d8\u91cf\uff09\n\n\u5982\u4f55\u914d\u7f6e\uff1a\n   1\u3001\u521b\u5efaAndroid.mk\u6587\u4ef6\uff0c\u914d\u7f6endk-build\u5fc5\u8981\u53c2\u6570\uff1b\n   2\u3001\u53ef\u9009\u521b\u5efaapplication.mk\u6587\u4ef6\uff0c\u914d\u7f6endk-build\u53c2\u6570 (\u8be5\u6587\u4ef6\u7684\u914d\u7f6e\u9879\u53ef\u4f7f\u7528gradle\u7684\u914d\u7f6e\u66ff\u4ee3)\uff1b\n   3\u3001\u4f7f\u7528gradle\u914d\u7f6eAndroid.mk\u4ee5\u53canative\u76f8\u5173\u53c2\u6570\uff1b\n\n2\u3001\u5982\u4f55\u7f16\u8bd1\u5e93\u6587\u4ef6\uff08\u4e24\u79cd\u65b9\u5f0f\uff09\uff1a\n   1\u3001Android Studio\u6267\u884cBuild\u5373\u53ef\uff08\u6267\u884c\u4e86\uff1aAndroid.mk + gradle\u914d\u7f6e\uff09\uff1b\n   2\u3001\u4e5f\u53ef\u5728Terminal\u3001Mac\u7ec8\u7aef\u3001cmd\u7ec8\u7aef\u4e2d\u901a\u8fc7ndk-build\u547d\u4ee4\u76f4\u63a5\u6784\u5efa\u5e93\u6587\u4ef6\uff08\u6267\u884c\u4e86\uff1aAndroid.mk\uff09<\/code><\/pre> \n 

2\u3001\u73af\u5883\u642d\u5efa<\/h2> \n
\n

JNI\u5b89\u88c5<\/strong>
JNI \u662fJDK\u91cc\u7684\u5185\u5bb9\uff0c\u7535\u8111\u4e0a\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6eJDK\u5373\u53ef (JDK1.1\u4e4b\u540e\u5c31\u6b63\u5f0f\u652f\u6301\u4e86);<\/p> \n <\/blockquote> \n

\n

NDK\u5b89\u88c5<\/strong>
\u53ef\u4ece\u5b98\u7f51\u81ea\u884c\u4e0b\u8f7d\u3001\u89e3\u538b\u5230\u672c\u5730\uff0c\u4e5f\u53ef\u57fa\u4e8eAndroidStudio\u4e0b\u8f7d\u89e3\u538b\u5230\u9ed8\u8ba4\u76ee\u5f55;<\/p> \n <\/blockquote> \n

\n

\u7f16\u8bd1\u5de5\u5177\u5b89\u88c5<\/strong>
cmake \u53ef\u57fa\u4e8eAndroidStudio\u4e0b\u8f7d\u5b89\u88c5;
ndk-build \u662fNDK\u91cc\u7684\u811a\u672c\u5de5\u5177\uff0cNDK\u5b89\u88c5\u597d\u5373\u53ef\u4f7f\u7528ndk-build;<\/p> \n <\/blockquote> \n

\u5f53\u524d\u6f14\u793a\uff0c\u4f7f\u7528\u7684Android Studio\u7248\u672c\u5982\u4e0b\uff08\u5f53\u524d\u6700\u65b0\u7248\uff09\uff1a<\/p> \n

<\/p> \n

\u542f\u52a8Android Studio --> \u6253\u5f00SDK Manager --> SDK Tools\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p> \n

<\/p> \n

\u6211\u4eec\u9009\u62e9NDK\u3001CMake\u3001LLDB\uff08\u8c03\u8bd5Native\u65f6\u624d\u4f1a\u4f7f\u7528\uff09\uff0c\u9009\u62e9Apply\u8fdb\u884c\u5b89\u88c5\uff0c\u7b49\u5b89\u88c5\u6210\u529f\u540e\uff0cNDK\u5f00\u53d1\u6240\u4f9d\u8d56\u7684\u73af\u5883\u4e5f\u5c31\u90fd\u9f50\u5168\u4e86\u3002<\/p> \n

3\u3001Native C++ \u9879\u76ee\uff08HelloWord\u6848\u4f8b\uff09<\/h2> \n

3.1\u3001\u9879\u76ee\u521b\u5efa\uff08java<\/a> \/ kotlin\uff09<\/h4> \n

\u65b0\u5efa\u9879\u76ee\uff0c\u9009\u62e9 Native C++\uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

<\/p> \n

<\/p> \n

\u65b0\u521b\u5efa\u7684\u9879\u76ee\uff0c\u9ed8\u8ba4\u5df2\u5305\u542b\u5b8c\u6574\u7684native \u793a\u4f8b\u4ee3\u7801\u3001cmake\u914d\u7f6e \uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

<\/p> \n

\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49Java native\u65b9\u6cd5\uff0c\u5e76\u5728cpp\u76ee\u5f55\u4e2d\u5199native\u5b9e\u73b0\u4e86\uff0c\u5f88\u65b9\u4fbf\u3002<\/p> \n

\u4f46\u662f\uff0c\u5f53\u6211\u4eec\u5199\u5b8cnative\u7684\u5b9e\u73b0\u4ee3\u7801\uff0c\u5e0c\u671b\u8fd0\u884cAPP\uff0c\u67e5\u770bJNI\u7684\u4ea4\u4e92\u6548\u679c\uff0c\u6b64\u65f6\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u7f16\u8bd1\u5de5\u5177\u4e86\uff0c\u54b1\u4eec\u8fd8\u662f\u5148\u770b\u4e00\u4e0bAndroid Studio\u9ed8\u8ba4\u7684Native\u7f16\u8bd1\u65b9\u5f0f\u5427\uff1aCMake<\/strong><\/p> \n

3.2\u3001CMake\u7684\u5e94\u7528<\/h4> \n

\u5728CMake\u7f16\u8bd1\u4e4b\u524d\uff0c\u54b1\u4eec\u5e94\u8be5\u5148\u505a\u54ea\u4e9b\u51c6\u5907\u5de5\u4f5c\uff1f<\/p> \n

1\u3001NDK\u73af\u5883\u662f\u5426\u914d\u7f6e\u6b63\u786e\uff1f\n-- \u5982\u679c\u672a\u914d\u7f6e\u6b63\u786e\u662f\u65e0\u6cd5\u8fdb\u884cC\/C++\u5f00\u53d1\u7684\uff0c\u66f4\u4e0d\u7528\u8bf4CMake\u7f16\u8bd1\u4e86\n\n2\u3001C\/C++\u529f\u80fd\u662f\u5426\u5b9e\u73b0\uff1f \n-- \u6b64\u6b21\u6f14\u793a\u4e3b\u8981\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u521b\u5efa\u7684native-lib.cpp\u6587\u4ef6\uff0c\u5173\u4e8e\u5177\u4f53\u5982\u4f55\u5b9e\u73b0\uff1a\u540e\u7eed\u6587\u7ae0\u518d\u8be6\u7ec6\u8bb2\u89e3\n\n3\u3001CMakeLists.txt\u662f\u5426\u521b\u5efa\u5e76\u6b63\u786e\u914d\u7f6e\uff1f \n-- \u8be5\u6587\u4ef6\u662fCMake\u5de5\u5177\u7f16\u8bd1\u7684\u57fa\u7840\uff0c\u672a\u914d\u7f6e\u6216\u914d\u7f6e\u9879\u9519\u8bef\uff0c\u5747\u4f1a\u5f71\u54cd\u7f16\u8bd1\u7ed3\u679c\n\n4\u3001gradle\u662f\u5426\u6b63\u786e\u914d\u7f6e\uff1f\n-- gradle\u914d\u7f6e\u4e5f\u662fCMake\u5de5\u5177\u7f16\u8bd1\u7684\u57fa\u7840\uff0c\u672a\u914d\u7f6e\u6216\u914d\u7f6e\u9879\u9519\u8bef\uff0c\u5747\u4f1a\u5f71\u54cd\u7f16\u8bd1\u7ed3\u679c<\/code><\/pre> \n 

\u9664\u6b64\u4e4b\u5916\uff0c\u54b1\u4eec\u8fd8\u5e94\u8be5\u5b66\u4e60CMake\u7684\u54ea\u4e9b\u91cd\u8981\u77e5\u8bc6\uff1f<\/p> \n

1\u3001CMake\u5de5\u5177\u7f16\u8bd1\u751f\u6210\u7684\u5e93\u6587\u4ef6\u9ed8\u8ba4\u5728\u4ec0\u4e48\u4f4d\u7f6e\uff1fapk\u4e2d\u5e93\u6587\u4ef6\u53c8\u662f\u5728\u4ec0\u4e48\u4f4d\u7f6e\uff1f\n2\u3001CMake\u5de5\u5177\u5982\u4f55\u6307\u5b9a\u7f16\u8bd1\u751f\u6210\u7684\u5e93\u6587\u4ef6\u4f4d\u7f6e\uff1f\n3\u3001CMake\u5de5\u5177\u5982\u4f55\u6307\u5b9a\u751f\u6210\u4e0d\u540cCPU\u5e73\u53f0\u5bf9\u5e94\u7684\u5e93\u6587\u4ef6\uff1f<\/code><\/pre> \n 

\u5e26\u7740\u8fd9\u4e9b\u95ee\u9898\uff0c\u54b1\u4eec\u5f00\u59cbCMake\u4e4b\u65c5\u5427\uff1a<\/p> \n

3.2.1\u3001NDK\u73af\u5883\u68c0\u67e5<\/h6> \n

\u7f16\u8bd1\u524d\uff0c\u5efa\u8bae\u5148\u68c0\u67e5\u4e0b\u5de5\u7a0b\u7684NDK\u914d\u7f6e\u60c5\u51b5\uff08\u4e0d\u7136\u5bb9\u6613\u62a5\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u7684\u9519\u8bef\uff09\uff1a
File --> Project Structure --> SDK Location\uff0c\u5982\u4e0b\u56fe\uff08\u6211\u672c\u5730\u7684Android Studio\u9ed8\u8ba4\u6ca1\u6709\u7ed9\u914d\u7f6eNDK\u8def\u5f84\uff0c\u90a3\u4e48\uff0c\u9700\u8981\u81ea\u5df1\u624b\u52a8\u6307\u5b9a\u4e00\u4e0b\uff09\uff1a<\/p> \n

<\/p> \n

<\/p> \n

3.2.2\u3001C\/C++\u529f\u80fd\u5b9e\u73b0<\/h6> \n

\u56e0\u4e3a\u672c\u8282\u4e3b\u8bb2CMake\u7f16\u8bd1\u5de5\u5177\uff0c\u4ee3\u7801\u5c31\u4e0d\u5355\u72ec\u5199\u4e86\uff0c\u54b1\u4eec\u76f4\u63a5\u4f7f\u7528\u5de5\u7a0b\u9ed8\u8ba4\u751f\u6210\u7684native-liv.cpp\uff0c\u7b80\u5355\u8c03\u6574\u4e00\u4e0bnative\u5b9e\u73b0\u65b9\u6cd5\u7684\u4ee3\u7801\u5427\uff08\u4fee\u6539\u8fd4\u56de\u6587\u672c\u4fe1\u606f\uff09\uff1a<\/p> \n

<\/p> \n

\u56e0Native C++\u5de5\u7a0b\u9ed8\u8ba4\u5df2\u914d\u7f6e\u597d\u4e86CMakeLists.txt\u548cgradle\uff0c\u6240\u4ee5\u54b1\u4eec\u53ef\u76f4\u63a5\u8fd0\u884c\u5de5\u7a0b\u770b\u6548\u679c\uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

JNI\u4ea4\u4e92\u6548\u679c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\uff0c\u8bf4\u660eCMake\u7f16\u8bd1\u6210\u529f\u4e86\u3002\u90a3\u4e48\uff0c\u8fd9\u7a76\u7adf\u662f\u600e\u4e48\u505a\u5230\u7684\u5462\uff1f\u54b1\u4eec\u63a5\u7740\u5206\u6790\u4e00\u4e0b\u5427\uff1a<\/p> \n

3.2.3\u3001CMake\u751f\u6210\u7684\u5e93\u6587\u4ef6\u4e0eapk\u4e2d\u7684\u5e93\u6587\u4ef6<\/h6> \n

\u5b89\u5353\u5de5\u7a0b\u7f16\u8bd1\u65f6\uff0c\u4f1a\u6267\u884cCMake\u7f16\u8bd1\uff0c\u5728 \u5de5\u7a0b\/app\/build\/...\/cmake\/ \u4e2d\u4f1a\u4ea7\u751f\u5bf9\u5e94\u7684so\u6587\u4ef6\uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

\u7ee7\u7eed\u7f16\u8bd1\u5b89\u5353\u5de5\u7a0b\uff0c\u4f1a\u6839\u636ebuild\u4e2d\u7684\u5185\u5bb9\uff0c\u751f\u6210\u6211\u4eec\u7684*.apk\u5b89\u88c5\u5305\u6587\u4ef6\u3002\u6211\u4eec\u627e\u5230\u3001\u53cd\u7f16\u8bd1apk\u5b89\u88c5\u5305\u6587\u4ef6\uff0c\u67e5\u627eso\u5e93\u6587\u4ef6\u3002\u539f\u6765\u5728apk\u5b89\u88c5\u5305\u4e2d\uff0cso\u5e93\u90fd\u88ab\u5b58\u653e\u5728lib\u76ee\u5f55\u4e2d\uff0c\u5982\u4e0b\u56fe\uff1a<\/p> \n

<\/p> \n

3.2.4\u3001CMake\u662f\u5982\u4f55\u7f16\u8bd1\u751f\u6210so\u5e93\u7684\u5462\uff1f<\/h6> \n

\u5728\u524d\u9762\u4ecb\u7ecdCMake\u5b9a\u4e49\u65f6\uff0c\u63d0\u5230\u4e86CMake\u662f\u57fa\u4e8eCMakeLists.txt\u6587\u4ef6\u548cgradle\u914d\u7f6e\u5b9e\u73b0\u7f16\u8bd1Native\u7c7b\u7684\u3002\u90a3\u4e48\uff0c\u54b1\u4eec\u5148\u6765\u770b\u4e00\u4e0bCMakeLists.txt\u6587\u4ef6\u5427\uff1a<\/p> \n

#cmake\u6700\u4f4e\u7248\u672c\u8981\u6c42\ncmake_minimum_required(VERSION 3.4.1)\n\n#\u6dfb\u52a0\u5e93\nadd_library(\n        # \u5e93\u540d\n        native-lib\n\n        # \u7c7b\u578b\uff1a\n        # SHARED \u662f\u6307\u52a8\u6001\u5e93\uff0c\u5bf9\u5e94\u7684\u662f.so\u6587\u4ef6\n        # STATIC \u662f\u6307\u9759\u6001\u5e93\uff0c\u5bf9\u5e94\u7684\u662f.a\u6587\u4ef6\n        # \u5176\u4ed6\u7c7b\u578b:\u7565\n        SHARED\n\n        # native\u7c7b\u8def\u5f84\n        native-lib.cpp)\n\n# \u67e5\u627e\u4f9d\u8d56\u5e93\nfind_library( \n        # \u4f9d\u8d56\u5e93\u522b\u540d\n        log-lib\n\n        # \u5e0c\u671b\u52a0\u5230\u672c\u5730\u7684NDK\u5e93\u540d\u79f0\uff0clog\u6307NDK\u7684\u65e5\u5fd7\u5e93\n        log)\n\n\n#","orderid":"0","title":"\u5b89\u5353JNI\u7cbe\u7ec6\u5316\u8bb2\u89e3\uff0c\u8ba9\u4f60\u5f7b\u5e95\u4e86\u89e3JNI\uff08\u4e00\uff09\uff1a\u73af\u5883\u642d\u5efa\u4e0eHelloWord(\u4e00)","smalltitle":"","mid":"0","fname":"Android","special_id":"0","bak_id":"0","info":"0","hits":"48","pages":"4","comments":"0","posttime":"2019-11-22 18:12:19","list":"1574417539","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u5b89\u5353<\/A> JNI<\/A> \u7cbe\u7ec6\u5316<\/A> \u8bb2\u89e3<\/A> \u5f7b\u5e95<\/A> \u4e86\u89e3<\/A> \u73af\u5883<\/A> \u642d\u5efa<\/A> HelloWord<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.37","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5b89\u5353JNI\u7cbe\u7ec6\u5316\u8bb2\u89e3\uff0c\u8ba9\u4f60\u5f7b\u5e95\u4e86\u89e3JNI\uff08\u4e00\uff09\uff1a\u73af\u5883\u642d\u5efa\u4e0eHelloWord","lastview":"1576034759","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}