Skip to content

fix: enable CGO for Android to fix DNS resolution issues#149

Merged
tsosunchia merged 2 commits intonxtrace:mainfrom
jason-akw:patch-1
Mar 2, 2026
Merged

fix: enable CGO for Android to fix DNS resolution issues#149
tsosunchia merged 2 commits intonxtrace:mainfrom
jason-akw:patch-1

Conversation

@jason-akw
Copy link

@jason-akw jason-akw commented Mar 1, 2026

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 构建流程的内部优化,无直接面向用户的功能更新。

  • 构建和开发工具
    • 优化了跨平台编译配置。

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/
@coderabbitai
Copy link

coderabbitai bot commented Mar 1, 2026

Walkthrough

GitHub Actions工作流程添加了Android NDK支持,为Android目标启用CGO,安装必要的NDK工具链,并配置Android特定的编译器环境变量。

Changes

Cohort / File(s) Summary
Android构建工具链配置
.github/workflows/build.yml
为Android目标添加条件性CGO启用、NDK安装步骤、ANDROID_API环境变量设置,以及Android NDK工具链中的Clang编译器路径配置。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Android的路铺就了,
NDK工具链已备好,
Clang编译器展翅飞,
交叉构建梦想达,
兔子庆祝新支持! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地概括了主要变更:为Android启用CGO以修复DNS解析问题,与代码实际变更(为Android条件启用CGO、配置Android NDK和CC编译器)完全对应。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jason-akw jason-akw marked this pull request as ready for review March 1, 2026 21:47
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
.github/workflows/build.yml (2)

112-117: 考虑是否需要设置 CGO_CFLAGS 和 CGO_LDFLAGS。

与 Darwin 构建(第 214-215 行)相比,Android 构建未设置 CGO_CFLAGSCGO_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 架构(如 armx86_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.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f131a99 and bb2623e.

📒 Files selected for processing (1)
  • .github/workflows/build.yml

@tsosunchia tsosunchia merged commit 52b2dd6 into nxtrace:main Mar 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants