{"id":207491,"date":"2025-07-08T12:36:30","date_gmt":"2025-07-08T04:36:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207491\/"},"modified":"2025-07-08T12:36:30","modified_gmt":"2025-07-08T04:36:30","slug":"%e5%9c%a8-android-%e4%b8%8a%e4%bd%bf%e7%94%a8%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207491\/","title":{"rendered":"\u5728 Android \u4e0a\u4f7f\u7528\u7f51\u7edc"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728 Android \u4e0a\u4f7f\u7528\u7f51\u7edc<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 23:51:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8Golang\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5728 Android \u4e0a\u4f7f\u7528\u7f51\u7edc\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4e00\u76f4\u5728\u5c1d\u8bd5\u8ba9 go \u5e94\u7528\u7a0b\u5e8f\u5728 android 10 \u4e0a\u8fd0\u884c\uff0c\u4f46\u9047\u5230\u4e86\u969c\u788d\u3002\u6211\u5f88\u786e\u5b9a\u6211\u73b0\u5728\u77e5\u9053\u4e3a\u4ec0\u4e48\u4f1a\u9047\u5230\u8fd9\u4e2a\u969c\u788d\uff0c\u4f46\u4e0d\u77e5\u9053\u5982\u4f55\u89e3\u51b3\u5b83\u3002\u9996\u5148\uff0c\u4ea4\u53c9\u7f16\u8bd1\u662f\u7b80\u5355\u7684\u4e8b\u60c5\u3002\u4f8b\u5982\uff0c<\/p>\n<pre>package main\nimport \"fmt\"\nfunc main() {\n    fmt.println(\"hello, android!\")\n}\n<\/pre>\n<p>\u4f7f\u7528 <code>goarm=arm64 goos=linux go build<\/code> \u7f16\u8bd1\u540e\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c<\/p>\n<p>\u4f46\u662f\u73b0\u5728\u5982\u679c\u6211\u6dfb\u52a0\u4e00\u4e9b\u7f51\u7edc\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n    \"io\/ioutil\"\n)\n\nfunc main() {\n    fmt.println(\"hello, android!\")\n    client := &amp;http.client{}\n    resp, err := client.get(\"https:\/\/www.google.com\")\n    if err != nil {\n        fmt.println(err)\n        return\n    }\n    bodybytes, err := ioutil.readall(resp.body)\n    if err != nil {\n        fmt.println(err)\n        return\n    }\n    fmt.printf(\"%s\", bodybytes)\n}\n<\/pre>\n<p>\u5e76\u4ee5\u4e0e\u8fd0\u884c\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u7f16\u8bd1\uff0c\u4f46\u6211\u4f1a\u6536\u5230\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>get \"https:\/\/www.google.com\": dial tcp: lookup www.google.com on [::1]:53: read udp [::1]:47618-&gt;[::1]:53: read: connection refused<\/pre>\n<p>\u6700\u521d\u6211\u4ee5\u4e3a\u8fd9\u662f\u6211\u7684 android \u8bbe\u5907\u4e0a\u7684\u4e00\u4e9b\u7f51\u7edc\u914d\u7f6e\uff0c\u4f46\u7ecf\u8fc7\u5927\u91cf\u7684\u5c1d\u8bd5\u540e\uff0c\u6211\u5f88\u786e\u5b9a\u5b83\u4e0e android \u7f51\u7edc\u201c\u4e0d\u540c\u201d\u8fd9\u4e00\u4e8b\u5b9e\u6709\u5173\u3002\u4e5f\u8bb8\u8fd9\u662f\u4eff\u751f\u4e0e libc \u7684\u95ee\u9898\uff08\uff1f\uff09\uff0c\u4f46\u81f3\u5c11\u8fd8\u6709\u5176\u4ed6\u4eba\u4f3c\u4e4e\u9047\u5230\u4e86\u540c\u6837\u7684\u969c\u788d\uff0c\u5e76\u901a\u8fc7\u4f7f\u7528 ndk \u9644\u5e26\u7684\u5de5\u5177\u94fe\u8fdb\u884c\u6784\u5efa\u6765\u89e3\u51b3\u3002<\/p>\n<p><strong>\u6709\u4eba\u77e5\u9053\u5982\u4f55\u771f\u6b63\u8ba9\u5b83\u53d1\u6325\u4f5c\u7528\u5417\uff1f<\/strong><\/p>\n<p>\u6700\u521d\u6211\u5e0c\u671b\u5b83\u50cf <code>goos=android goarch=arm64 go build<\/code> \u4e00\u6837\u7b80\u5355\uff0c\u4f46\u5931\u8d25\u4e86\uff1a<\/p>\n<pre># command-line-arguments\n\/usr\/lib\/golang\/pkg\/tool\/linux_amd64\/link: running gcc failed: exit status 1\n\/usr\/bin\/ld: \/tmp\/go-link-384615804\/go.o: relocations in generic elf (em: 183)\n&lt;bunch of these&gt;\n\/usr\/bin\/ld: \/tmp\/go-link-384615804\/go.o: error adding symbols: file in wrong format\ncollect2: error: ld returned 1 exit status<\/pre>\n<p>\u4e00\u4e9b\u8c37\u6b4c\u641c\u7d22\u51fa\u73b0\u4e86 https:\/\/github.com\/golang\/go\/issues\/30216 &#8211; \u8fd9\u8868\u660e\u4f60\u9700\u8981\u663e\u5f0f\u542f\u7528 cgo\u3002\u4f46\u662f <code>goarch=arm64 goos=android cgo_enabled=1 go build<\/code> \u4ecd\u7136\u5931\u8d25\uff0c\u8fd9\u6b21\u662f\uff1a<\/p>\n<pre># runtime\/cgo\ngcc_android.c:6:10: fatal error: android\/log.h: no such file or directory\n    6 | #include &lt;android\/log.h&gt;\n      |          ^~~~~~~~~~~~~~~\ncompilation terminated.<\/pre>\n<p>\u6211\u5c1d\u8bd5\u4e86\u5f88\u591a\u5176\u4ed6\u7ec4\u5408\uff0c\u6700\u63a5\u8fd1\u7684\u662f\u5728\u6211\u7684 ndk \u5b89\u88c5\u4e2d\u6307\u5411 go \u7f16\u8bd1\u5668\u3002\u57fa\u672c\u4e0a\u7c7b\u4f3c\u4e8e <code>goarch=arm64 goos=android cc=$ndkroot\/toolchains\/arm-linux-androideabi-4.9\/prebuilt\/linux-x86_64\/bin\/arm-linux-androideabi-gcc cgo_enabled=1 cgo_cflags=\"--sysroot $ndkroot \/sysroot -i$ndkroot\/sysroot\/usr\/include\/arm-linux-androideabi\/\" \u8f6c\u5230 build<\/code>\u3002\u4f46\u8fd9\u5931\u8d25\u4e86\uff1a<\/p>\n<pre># runtime\/cgo\nIn file included from _cgo_export.c:4:0:\ncgo-gcc-export-header-prolog:25:14: error: size of array '_check_for_64_bit_pointer_matching_GoInt' is negative<\/pre>\n<p>\u6211\u6307\u5411\u7f16\u8bd1\u5668\u7684\u65b9\u5f0f\u6709\u95ee\u9898 &#8211; \u4f8b\u5982\uff0c\u5982\u679c\u4e0d\u6307\u5b9a sysroot\uff0c\u6211\u5c06\u627e\u4e0d\u5230\u4efb\u4f55\u5934\u6587\u4ef6 (stdlib.h)\uff0c\u5373\u4f7f\u5982\u6b64\uff0c\u5b83\u4e5f\u4e0d\u4f1a\u627e\u5230\u7279\u5b9a\u4e8e\u4f53\u7cfb\u7ed3\u6784\u7684\u5305\u542b\u6587\u4ef6 (asm \/types.h) \u800c\u4e0d\u6307\u5b9a\u9644\u52a0\u5305\u542b\u8def\u5f84\u3002\u56e0\u6b64\uff0c\u8be5\u9519\u8bef\u53ef\u80fd\u662f\u4f53\u7cfb\u7ed3\u6784\u548c\u7f16\u8bd1\u5668\u4e4b\u95f4\u7684\u4e0d\u5339\u914d\uff0c\u6211\u53ea\u662f\u4e0d\u786e\u5b9a\u5728\u54ea\u91cc\u6216\u5982\u4f55\u4fee\u590d\u5b83\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5df2\u9605\u8bfb\u6240\u6709\u5185\u5bb9\uff0c\u8c22\u8c22\uff01\u4e00\u4e9b\u9644\u52a0\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li>go \u7248\u672c\uff1a1.14.13 linux\/amd64<\/li>\n<li>ndk \u7248\u672c\uff1ar16b<\/li>\n<\/ul>\n<p>\u6211\u4e5f\u7814\u7a76\u8fc7\u50cf gomobile \u8fd9\u6837\u7684\u4e1c\u897f\uff0c\u4f46\u5b83\u4f3c\u4e4e\u66f4\u9002\u5408\u6784\u5efa android \u5e94\u7528\u7a0b\u5e8f\uff08\u5982 apk\uff09\uff0c\u5c3d\u7ba1\u6211\u53ef\u80fd\u5728\u8fd9\u91cc\u9057\u6f0f\u4e86\u4e00\u4e9b\u4e1c\u897f\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60f3\u901a\u4e86\uff01\u597d\u5427\uff0c\u6211\u7684\u5177\u4f53\u793a\u4f8b\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u6211\u4e0d\u592a\u786e\u5b9a\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u3002\u7b54\u6848\u57fa\u672c\u4e0a\u6765\u81ea \uff0c\u4f60\u5fc5\u987b\uff1a<\/p>\n<ul>\n<li>\u6784\u5efa\u72ec\u7acb\u7248\u672c\u7684 ndk\uff08\u6267\u884c\u6b64\u64cd\u4f5c\u7684 python \u811a\u672c\u4e2d\u7684\u6ce8\u91ca\u8868\u793a\u5b83\u4ee5\u66f4\u65b9\u4fbf\u7684\u65b9\u5f0f\u5c06\u5176\u6253\u5305\u4ee5\u4f9b\u5176\u4ed6\u5de5\u5177\u4f7f\u7528\uff09<\/li>\n<li>\u4f7f\u7528 ndk \u4e2d\u7684\u4ea4\u53c9\u7f16\u8bd1\u5668\u91cd\u5efa go \u7684\u6807\u51c6\u5e93<\/li>\n<li>\u4f7f\u7528 ndk \u4ea4\u53c9\u7f16\u8bd1\u5668\u6784\u5efa\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u6211\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>$ $(NDKROOT)\/build\/tools\/make_standalone_toolchain.py --arch arm64 --api 24 --install-dir $(HOME)\/ndk_standalone\n$ CC=$(HOME)\/ndk_standalone\/bin\/clang CXX=$(HOME)\/ndk_standalone\/bin\/clang GOOS=android GOARCH=arm64 go install std\n$ CC=$(HOME)\/ndk_standalone\/bin\/clang CXX=$(HOME)\/ndk_standalone\/bin\/clang GOOS=android GOARCH=arm64 CGO_ENABLED=1 go build<\/pre>\n<p>\u7f51\u7edc\u5de5\u4f5c\u6b63\u5e38\uff01<\/p>\n<p>\u6211\u4e0d\u592a\u786e\u5b9a\u4e3a\u4ec0\u4e48\uff08\u6216\u8005\u662f\u5426\uff09clang \u4e0e\u76f4\u63a5\u8c03\u7528\u7f16\u8bd1\u5668\u4e0d\u540c\u3002\u4e5f\u8bb8\u6211\u4f1a\u591a\u73a9\u73a9\u8fd9\u4e2a\u3002\u53e6\u5916\uff0c\u5728\u7f16\u8bd1\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u6211\u4ecd\u7136\u5fc5\u987b\u663e\u5f0f\u542f\u7528 cgo\uff08\u4f46\u4e0d\u9002\u7528\u4e8e go std \u5e93\uff09\uff0c\u5426\u5219\u94fe\u63a5\u5931\u8d25\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207491","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207491"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207491\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}