{"id":94026,"date":"2020-04-22T05:00:03","date_gmt":"2020-04-21T21:00:03","guid":{"rendered":"http:\/\/4563.org\/?p=94026"},"modified":"2020-04-22T05:00:03","modified_gmt":"2020-04-21T21:00:03","slug":"%e6%96%b0%e6%92%b8%e7%9a%84-gradle-%e6%8f%92%e4%bb%b6%ef%bc%8c%e9%9b%86%e6%88%90-xmake-%e6%9e%84%e5%bb%ba-android-jni-%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=94026","title":{"rendered":"\u65b0\u64b8\u7684 gradle \u63d2\u4ef6\uff0c\u96c6\u6210 xmake \u6784\u5efa Android JNI \u7a0b\u5e8f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u65b0\u64b8\u7684 gradle \u63d2\u4ef6\uff0c\u96c6\u6210 xmake \u6784\u5efa Android JNI \u7a0b\u5e8f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : waruqi <\/span>  <span><i><\/i> 20<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u7b80\u4ecb<\/h2>\n<p>xmake-gradle\u662f\u4e00\u4e2a\u65e0\u7f1d\u6574\u5408 xmake \u7684 gradle \u63d2\u4ef6\u3002<\/p>\n<p>\u76ee\u524d\u5728 gradle \u4e2d\u505a android jni \u76f8\u5173\u96c6\u6210\u5f00\u53d1\uff0c\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7 ndkBuild \u6216\u8005 CMake \u6765\u652f\u6301\uff0cgradle \u4e5f\u5185\u7f6e\u4e86\u8fd9\u4e24\u5de5\u5177\u7684\u96c6\u6210<\/p>\n<p>\u4f46\u662f\u7ef4\u62a4 Android.mk \u8fd8\u662f\u975e\u5e38\u7e41\u7410\u7684\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u4f1a\u6bd4\u8f83\u75db\u82e6\uff0c\u800c cmake \u7684 dsl \u8bed\u6cd5\u4e0d\u591f\u7b80\u6d01\u76f4\u89c2\uff0c\u6211\u4e2a\u4eba\u4e5f\u4e0d\u662f\u5f88\u559c\u6b22\uff0c\u56e0\u6b64\u6211\u5148\u524d\u6574\u4e86 xmake \u6765\u5b9e\u73b0\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u4f18\u52bf\u5c31\u662f: \u7b80\u5355\uff0c\u5feb\u901f\uff0c\u5bf9\u65b0\u624b\u53cb\u597d\uff0c\u53e6\u5916\u529f\u80fd\u4e5f\u5f88\u5f3a\u5927\uff0c\u5177\u4f53\u6709\u90a3\u4e9b\u529f\u80fd\uff0c\u5927\u5bb6\u53ef\u4ee5\u5230 xmake \u9879\u76ee\u4e3b\u9875\u770b\u4e0b\u76f8\u5173\u4ecb\u7ecd\u3002<\/p>\n<p>\u800c\u4e4b\u524d\u60f3\u8981\u7528 xmake \u7f16\u8bd1 android so \u5e93\uff0c\u53ea\u80fd\u901a\u8fc7\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\u6bd4\u5982:<\/p>\n<pre><code>xmake f -p android --ndk=xxxx xmake <\/code><\/pre>\n<p>\u867d\u7136\u5df2\u7ecf\u5f88\u7b80\u5355\u4e86\uff0c\u4f46\u662f\u5982\u679c\u8981\u8ddf android apk\/aar \u4e00\u8d77\u6253\u5305\u96c6\u6210\uff0c\u8fd8\u662f\u9700\u8981\u5f88\u591a\u989d\u5916\u7684\u5de5\u4f5c\uff0c\u4e3a\u4e86\u63d0\u9ad8\u5f00\u53d1\u8005\u7684\u6548\u7387\uff0c\u6211\u6700\u8fd1\u65b0\u6574\u4e86\u8fd9\u4e2a grafle \u63d2\u4ef6\uff0c\u6765\u65e0\u7f1d\u96c6\u6210\u5230 gradle \u7684\u6574\u4e2a\u6784\u5efa\u4f53\u7cfb\u4e2d\u53bb\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u5c31\u53ef\u4ee5\u5728 android studio \u4e2d\u65b9\u4fbf\u7684\u7528 xmake \u6765\u7f16\u8bd1 jni \u5e93\uff0c\u4ee5\u53ca\u81ea\u52a8\u96c6\u6210\u4e86\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u76f8\u5173 gradle \u914d\u7f6e\u57fa\u672c\u8ddf cmake \u548c ndkbuild \u7684\u4fdd\u6301\u4e00\u81f4\uff0c\u5927\u90e8\u5206\u90fd\u662f\u517c\u5bb9\u7684\uff0c\u5207\u6362\u6210\u672c\u4e5f\u4f1a\u964d\u4f4e\u5f88\u591a\u3002<\/p>\n<p>\u6b22\u8fce\u5927\u5bb6\u6765\u8bd5\u8bd5\u54e6\uff0c\u65b0\u9c9c\u51fa\u7089\u7684\u63d2\u4ef6\uff0c\u5982\u679c\u4f60\u60f3\u8981\u4e86\u89e3\u66f4\u591a\uff0c\u8bf7\u53c2\u8003\uff1a<\/p>\n<ul>\n<li>\u63d2\u4ef6 Github<\/li>\n<li>xmake \u9879\u76ee<\/li>\n<\/ul>\n<h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<p>\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u597d\u5bf9\u5e94\u7684 xmake \u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5173\u4e8e\u5b89\u88c5\u8bf4\u660e\u89c1\uff1axmake\u3002<\/p>\n<h2>\u5e94\u7528\u63d2\u4ef6<\/h2>\n<h3>\u901a\u8fc7\u63d2\u4ef6 DSL \u96c6\u6210<\/h3>\n<pre><code>plugins {   id 'org.tboox.gradle-xmake-plugin' version '1.0.6' } <\/code><\/pre>\n<h3>\u88ab\u5e9f\u5f03\u7684\u63d2\u4ef6\u96c6\u6210\u65b9\u5f0f<\/h3>\n<pre><code>buildscript {   repositories {     maven {       url \"https:\/\/plugins.gradle.org\/m2\/\"     }   }   dependencies {     classpath 'org.tboox:gradle-xmake-plugin:1.0.6'   }   repositories {     mavenCentral()   } }  apply plugin: \"org.tboox.gradle-xmake-plugin\" <\/code><\/pre>\n<h2>\u914d\u7f6e<\/h2>\n<h3>\u6700\u7b80\u5355\u7684\u914d\u7f6e\u793a\u4f8b<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u6dfb\u52a0<code>xmake.lua<\/code>\u6587\u4ef6\u5230<code>projectdir\/jni\/xmake.lua<\/code>\uff0c\u90a3\u4e48\u6211\u4eec\u53ea\u9700\u8981\u5728 build.gradle \u4e2d\u542f\u7528\u751f\u6548\u4e86 xmake \u6307\u5b9a\u4e0b\u5bf9\u5e94\u7684 JNI \u5de5\u7a0b\u8def\u5f84\u5373\u53ef\u3002<\/p>\n<h4>build.gradle<\/h4>\n<pre><code>android {     externalNativeBuild {         xmake {             path \"jni\/xmake.lua\"         }     } } <\/code><\/pre>\n<h4>JNI<\/h4>\n<p>JNI \u5de5\u7a0b\u7ed3\u6784<\/p>\n<pre><code>projectdir   - src     - main       - java   - jni     - xmake.lua     - *.cpp <\/code><\/pre>\n<p>xmake.lua:<\/p>\n<pre><code>add_rules(\"mode.debug\", \"mode.release\") target(\"nativelib\")     set_kind(\"shared\")     add_files(\"nativelib.cc\") <\/code><\/pre>\n<h3>\u66f4\u591a Gradle \u914d\u7f6e\u8bf4\u660e<\/h3>\n<pre><code>android {     defaultConfig {         externalNativeBuild {             xmake {                 \/\/ \u8ffd\u52a0\u8bbe\u7f6e\u5168\u5c40 c \u7f16\u8bd1 flags                 cFlags \"-DTEST\"                  \/\/ \u8ffd\u52a0\u8bbe\u7f6e\u5168\u5c40 c++\u7f16\u8bd1 flags                 cppFlags \"-DTEST\", \"-DTEST2\"                  \/\/ \u8bbe\u7f6e\u5207\u6362\u7f16\u8bd1\u6a21\u5f0f\uff0c\u4e0e`xmake f -m debug`\u7684\u914d\u7f6e\u5bf9\u5e94\uff0c\u5177\u4f53\u6a21\u5f0f\u503c\u6839\u636e\u81ea\u5df1\u7684 xmake.lua \u8bbe\u7f6e\u800c\u5b9a                 buildMode \"debug\"                  \/\/ \u8bbe\u7f6e\u9700\u8981\u7f16\u8bd1\u7684 abi \u5217\u8868\uff0c\u652f\u6301\uff1aarmeabi, armeabi-v7a, arm64-v8a, x86, x86_64                 \/\/ \u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u7684\u8bdd\uff0c\u6211\u4eec\u4e5f\u652f\u6301\u4ece defaultConfig.ndk.abiFilters \u4e2d\u83b7\u53d6 abiFilters                 abiFilters \"armeabi-v7a\", \"arm64-v8a\"             }         }     }      externalNativeBuild {         xmake {             \/\/ \u8bbe\u7f6e jni \u5de5\u7a0b\u4e2d xmake.lua \u6839\u6587\u4ef6\u8def\u5f84\uff0c\u8fd9\u662f\u5fc5\u987b\u7684\uff0c\u4e0d\u8bbe\u7f6e\u5c31\u4e0d\u4f1a\u542f\u7528 jni \u7f16\u8bd1             path \"jni\/xmake.lua\"              \/\/ \u542f\u7528\u8be6\u7ec6\u8f93\u51fa\uff0c\u4f1a\u663e\u793a\u5b8c\u6574\u7f16\u8bd1\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5176\u4ed6\u503c\uff1averbose, warning, normal             logLevel \"verbose\"              \/\/ \u6307\u5b9a c++ stl \u5e93\uff0c\u9ed8\u8ba4\u4e0d\u6307\u5b9a\u4f1a\u4f7f\u7528 c++_static\uff0c\u5176\u4ed6\u503c\uff1ac++_static\/c++_shared, gnustl_static\/gnustl_shared, stlport_static\/stlport_shared             stl \"c++_shared\"              \/\/ \u8bbe\u7f6e xmake \u53ef\u6267\u884c\u7a0b\u5e8f\u8def\u5f84\uff08\u901a\u5e38\u4e0d\u7528\u8bbe\u7f6e\uff09             \/\/ program \/usr\/local\/bin\/xmake              \/\/ \u7981\u7528 stdc++\u5e93\uff0c\u9ed8\u8ba4\u662f\u542f\u7528\u7684             \/\/ stdcxx false              \/\/ \u8bbe\u7f6e\u5176\u4ed6\u6307\u5b9a\u7684 ndk \u76ee\u5f55\u8def\u5f84 \uff08\u8fd9\u662f\u53ef\u9009\u7684\uff0c\u9ed8\u8ba4 xmake \u4f1a\u81ea\u52a8\u4ece$ANDROID_NDK_HOME \u6216\u8005`~\/Library\/Android\/sdk\/ndk-bundle`\u4e2d\u68c0\u6d4b\uff09             \/\/ \u5f53\u7136\u5982\u679c\u7528\u6237\u901a\u8fc7`xmake g --ndk=xxx`\u914d\u7f6e\u4e86\u5168\u5c40\u8bbe\u7f6e\uff0c\u4e5f\u4f1a\u81ea\u52a8\u4ece\u8fd9\u4e2a\u91cc\u9762\u68c0\u6d4b             \/\/ ndk \"\/Users\/ruki\/files\/android-ndk-r20b\/\"              \/\/ \u8bbe\u7f6e ndk \u4e2d sdk \u7248\u672c             \/\/ sdkver 21         }     } } <\/code><\/pre>\n<h2>\u7f16\u8bd1 JNI<\/h2>\n<h3>\u7f16\u8bd1 JNI \u5e76\u4e14\u751f\u6210 APK<\/h3>\n<p>\u5f53<code>gradle-xmake-plugin<\/code>\u63d2\u4ef6\u88ab\u5e94\u7528\u751f\u6548\u540e\uff0c<code>xmakeBuild<\/code>\u4efb\u52a1\u4f1a\u81ea\u52a8\u6ce8\u5165\u5230\u73b0\u6709\u7684<code>assemble<\/code>\u4efb\u52a1\u4e2d\u53bb\uff0c\u81ea\u52a8\u6267\u884c jni \u5e93\u7f16\u8bd1\u548c\u96c6\u6210\u3002<\/p>\n<pre><code>$ .\/gradlew app:assembleDebug &gt; Task :nativelib:xmakeConfigureForArm64 &gt; Task :nativelib:xmakeBuildForArm64 &gt;&gt; xmake build [ 50%]: ccache compiling.debug nativelib.cc [ 75%]: linking.debug libnativelib.so [100%]: build ok! &gt;&gt; install artifacts to \/Users\/ruki\/projects\/personal\/xmake-gradle\/nativelib\/libs\/arm64-v8a &gt; Task :nativelib:xmakeConfigureForArmv7 &gt; Task :nativelib:xmakeBuildForArmv7 &gt;&gt; xmake build [ 50%]: ccache compiling.debug nativelib.cc [ 75%]: linking.debug libnativelib.so [100%]: build ok! &gt;&gt; install artifacts to \/Users\/ruki\/projects\/personal\/xmake-gradle\/nativelib\/libs\/armeabi-v7a &gt; Task :nativelib:preBuild &gt; Task :nativelib:assemble &gt; Task :app:assembleDebug <\/code><\/pre>\n<h3>\u5f3a\u5236\u91cd\u5efa JNI<\/h3>\n<pre><code>$ .\/gradlew nativelib:xmakeRebuild <\/code><\/pre>\n<p>\u200b-<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u65b0\u64b8\u7684 gradle \u63d2\u4ef6\uff0c\u96c6\u6210 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/94026"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94026"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/94026\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94026"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}