fix: enable CGO for Android to fix DNS resolution issues#149
fix: enable CGO for Android to fix DNS resolution issues#149tsosunchia merged 2 commits intonxtrace:mainfrom
Conversation
Switch to Android NDK toolchain, enable CGO for Android builds.
Already tested with Termux 0.118.3 with Xiaomi 15 Ultra (Android 16).
Thanks to: https://dave.engineer/blog/2025/11/cross-compiling-go-android/
WalkthroughGitHub Actions工作流程添加了Android NDK支持,为Android目标启用CGO,安装必要的NDK工具链,并配置Android特定的编译器环境变量。 Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
.github/workflows/build.yml (2)
112-117: 考虑是否需要设置 CGO_CFLAGS 和 CGO_LDFLAGS。与 Darwin 构建(第 214-215 行)相比,Android 构建未设置
CGO_CFLAGS和CGO_LDFLAGS。如果项目依赖特定的 C 库或需要指定 sysroot,可能需要添加这些标志。如果当前配置已在 Termux 上测试通过,可以暂时保持现状,但建议在遇到链接问题时考虑添加:
CGO_CFLAGS="--sysroot=${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/sysroot" CGO_LDFLAGS="--sysroot=${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/sysroot"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/build.yml around lines 112 - 117, The Android build block (when GOOS="android") sets ANDROID_API, TOOLCHAIN_BIN and CC but omits CGO_CFLAGS/CGO_LDFLAGS; add exports for CGO_CFLAGS and CGO_LDFLAGS pointing their --sysroot to the NDK sysroot (derived from steps.setup-ndk.outputs.ndk-path/toolchains/llvm/prebuilt/linux-x86_64/sysroot) and append them to GITHUB_ENV alongside CC so cgo builds link against the correct sysroot when using the TOOLCHAIN_BIN/CC.
112-117: 架构硬编码为 aarch64,可能影响未来扩展性。当前实现将编译器目标硬编码为
aarch64-linux-android,这与 matrix 中仅有android/arm64的配置相符。但如果将来需要支持其他 Android 架构(如arm、x86_64),此处需要相应修改。考虑根据
$GOARCH动态选择编译器目标:♻️ 可选:动态选择 Android 编译器目标
if [ "$GOOS" = "android" ]; then ANDROID_API=21 TOOLCHAIN_BIN="${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin" - export CC="$TOOLCHAIN_BIN/aarch64-linux-android${ANDROID_API}-clang" + case "$GOARCH" in + arm64) ANDROID_TARGET="aarch64-linux-android" ;; + arm) ANDROID_TARGET="armv7a-linux-androideabi" ;; + amd64) ANDROID_TARGET="x86_64-linux-android" ;; + 386) ANDROID_TARGET="i686-linux-android" ;; + *) echo "Unsupported Android arch: $GOARCH"; exit 1 ;; + esac + export CC="$TOOLCHAIN_BIN/${ANDROID_TARGET}${ANDROID_API}-clang" echo "CC=${CC}" >> "$GITHUB_ENV" fi🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/build.yml around lines 112 - 117, The workflow hardcodes the Android compiler target to aarch64 (the CC value) which prevents supporting other Android architectures; update the android branch (the if [ "$GOOS" = "android" ] block) to derive the toolchain triple from $GOARCH (e.g., via a case or mapping for arm64 -> aarch64-linux-android, arm -> armv7a-linux-androideabi, x86 -> i686-linux-android, x86_64 -> x86_64-linux-android), then build TOOLCHAIN_BIN using steps.setup-ndk.outputs.ndk-path and set/export CC using the computed triple plus $ANDROID_API so the CC assignment is dynamic instead of hardcoded.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In @.github/workflows/build.yml:
- Around line 112-117: The Android build block (when GOOS="android") sets
ANDROID_API, TOOLCHAIN_BIN and CC but omits CGO_CFLAGS/CGO_LDFLAGS; add exports
for CGO_CFLAGS and CGO_LDFLAGS pointing their --sysroot to the NDK sysroot
(derived from
steps.setup-ndk.outputs.ndk-path/toolchains/llvm/prebuilt/linux-x86_64/sysroot)
and append them to GITHUB_ENV alongside CC so cgo builds link against the
correct sysroot when using the TOOLCHAIN_BIN/CC.
- Around line 112-117: The workflow hardcodes the Android compiler target to
aarch64 (the CC value) which prevents supporting other Android architectures;
update the android branch (the if [ "$GOOS" = "android" ] block) to derive the
toolchain triple from $GOARCH (e.g., via a case or mapping for arm64 ->
aarch64-linux-android, arm -> armv7a-linux-androideabi, x86 ->
i686-linux-android, x86_64 -> x86_64-linux-android), then build TOOLCHAIN_BIN
using steps.setup-ndk.outputs.ndk-path and set/export CC using the computed
triple plus $ANDROID_API so the CC assignment is dynamic instead of hardcoded.
Switch to Android NDK toolchain, enable CGO for Android builds.
Already tested with Termux 0.118.3 with Xiaomi 15 Ultra (Android 16).
Thanks to: https://dave.engineer/blog/2025/11/cross-compiling-go-android/
Summary by CodeRabbit
发布说明
此版本包含的更改主要涉及 CI/CD 构建流程的内部优化,无直接面向用户的功能更新。