打包Golang并在Android环境下运行

实践:

1、

 

1、编译
CGO_ENABLED=0 GOARCH=arm GOOS=linux go build -o target-android
2、检查android目录
/data/local/tmp
3、导入文件
adb push ./target-android /data/local/tmp
adb push ./config.yaml /data/local/tmp
4、
myhost:/data/local/tmp # ls
ls
config.yaml target-android

5、执行文件

msm8909:/data/local/tmp # ./target-android
./target-android
/system/bin/sh: ./target-android: can't execute: Permission denied

修改权限

 chmod +x target-android

 

go源码

func main() {
    // CGO_ENABLED=0 GOARCH=arm GOOS=linux go build  -o target-android  

    fmt.Println("runtime.GOOS=", runtime.GOOS, " runtime.GOARCH=", runtime.GOARCH)
 
成功运行

./target-android 
runtime.GOOS= linux runtime.GOARCH= arm

注:
环境信息
Windows

go version
go version go1.21.3 windows/amd64

 

 

Android

myhost:/ # cat /proc/cpuinfo
cat /proc/cpuinfo
processor : 0
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5

processor : 1
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5

processor : 2
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5

processor : 3
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5

Hardware : Qualcomm Technologies, Inc MSM8909
Revision : 0000
Serial : 0000000000000000
Processor : ARMv7 Processor rev 5 (v7l)
myhost:/ #

 

myhost:/ # cat /proc/version
cat /proc/version
Linux version 3.10.49 (android@Machine000) (gcc version 4.8 (GCC) ) #2 SMP PREEMPT Tue Feb 14 14:51:37 CST 2023
myhost:/ #

myhost:/ # ls
ls
acct init.qcom.class_core.sh res
bugreports init.qcom.early_boot.sh root
cache init.qcom.factory.rc sbin
charger init.qcom.rc sdcard
config init.qcom.sh seapp_contexts
d init.qcom.syspart_fixup.sh selinux_version
data init.qcom.usb.rc sepolicy
default.prop init.qcom.usb.sh service_contexts
dev init.rc storage
etc init.target.rc sys
file_contexts.bin init.usb.configfs.rc system
firmware init.usb.rc tombstones
fstab.qcom init.zygote32.rc ueventd.qcom.rc
init mnt ueventd.rc
init.class_main.sh oem vendor
init.environ.rc persist verity_key
init.mdm.sh proc
init.qcom.bms.sh property_contexts
msm8909:/ #

 

myhost:/ # printenv
printenv
_=/system/bin/printenv
ANDROID_DATA=/data
DOWNLOAD_CACHE=/data/cache
LOGNAME=root
HOME=/
ANDROID_ROOT=/system
BOOTCLASSPATH=/system/framework/core-oj.jar:/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/apache-xml.jar:/system/framework/org.apache.http.legacy.boot.jar:/system/framework/qcom.fmradio.jar:/system/framework/tcmiface.jar:/system/framework/telephony-ext.jar:/system/framework/oem-services.jar
SHELL=/system/bin/sh
ANDROID_BOOTLOGO=1
TMPDIR=/data/local/tmp
ANDROID_ASSETS=/system/app
TERM=dumb
ASEC_MOUNTPOINT=/mnt/asec
ANDROID_SOCKET_adbd=8
HOSTNAME=myhost
EXTERNAL_STORAGE=/sdcard
SECONDARY_STORAGE=/storage/sdcard1
ANDROID_STORAGE=/storage
USER=root
PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin
SYSTEMSERVERCLASSPATH=/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar
myhost:/ #

 

GoArm · golang/go Wiki · GitHub https://github.com/golang/go/wiki/GoArm

Introduction

Go is fully supported on Linux and Darwin. Any Go program that you can compile for x86/x86_64 should work on Arm. Besides Linux and Darwin, Go is also experimentally supported on FreeBSD, OpenBSD and NetBSD.

Supported architectures

Go supports the following ARM architectural families.

ArchitectureStatusGOARM valueGOARCH value
ARMv4 and below not supported n/a n/a
ARMv5 supported GOARM=5 GOARCH=arm
ARMv6 supported GOARM=6 GOARCH=arm
ARMv7 supported GOARM=7 GOARCH=arm
ARMv8 supported n/a GOARCH=arm64

Starting from Go 1.1, the appropriate GOARM value will be chosen if you compile the program from source on the target machine. In cross compilation situations, it is recommended that you always set an appropriate GOARM value along with GOARCH.

Supported operating systems

  • ARM on Linux. You must run an EABI kernel. These are generally known as armel for softfloat (compatible with ARMv5) or armhf for hardware floating point (ARMv6 and above).
  • ARM on Darwin: ARMv7 is required.
  • ARM on FreeBSD, OpenBSD, and NetBSD: ARMv6K or above is required.

 

 

打包Golang并在Android环境下运行-CSDN博客 https://blog.csdn.net/lszy24/article/details/78948130

 

posted @ 2023-11-28 10:00  papering  阅读(451)  评论(0编辑  收藏  举报