Linux下GoLang应用开发交叉编译到Android

xingyun86 2019-12-4 1750

假设Linux下go环境已经配置好

1.下载NDK

页面:https://developer.android.google.cn/ndk/downloads/older_releases.html
个人建议:下载r14b版本(之后的版本使用clang,会有差异)
https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip?hl=zh_cn

2.Linux保存内容为android_build.sh脚本,赋予可执行权限(755),运行即可

#!/bin/sh
export NDK_ROOT=/home/ppshuai/android-ndk-r14b
export TARGET_OS=android
export TARGET_ARCH=arm64
export TARGET_PREFIX=aarch64-linux-android-
echo  $NDK_ROOT

export PATH=$PATH:$NDK_ROOT:$NDK_ROOT/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin

#mkdir -p $NDK_ROOT/sysroot/usr/include/asm
#cp $NDK_ROOT/sysroot/usr/include/aarch64-linux-android/asm $NDK_ROOT/sysroot/usr/include/ -rf

CGO_ENABLED=1 \
GOOS=$TARGET_OS \
GOARCH=$TARGET_ARCH \
CPP=$TARGET_PREFIX"cpp" \
CC=$TARGET_PREFIX"gcc" \
CXX=$TARGET_PREFIX"g++" \
LD=$TARGET_PREFIX"ld" \
NM=$TARGET_PREFIX"nm" \
AR=$TARGET_PREFIX"ar" \
AS=$TARGET_PREFIX"as" \
RANLIB=$TARGET_PREFIX"ranlib" \
CGO_CFLAGS="-g -O2 --sysroot="$NDK_ROOT"/sysroot/" \
CGO_LDFLAGS="-g -O2 -fPIC --sysroot="$NDK_ROOT"/platforms/android-23/arch-arm64/" \
go build \
-x \
-v \
-a \
-ldflags "-w -s" -o arm64.out test.go


3.go-sqlite3驱动下载

git clone https://github.com/mattn/go-sqlite3.git

4.编译好后,可以在PC上adb连接Android手机测试

D:\DevelopmentEnvironment>adb push arm64.out /data/local/tmp/
D:\DevelopmentEnvironment>adb shell chmod +x /data/local/tmp/arm64.out
D:\DevelopmentEnvironment>adb shell
shell@X3:/ $cd /data/local/tmp/
shell@X3:/data/local/tmp $./arm64.out
1
1
1
astaxieupdate
研发部门
2012-12-09
1


上传的附件:
×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回