• 技术文章 >后端开发 >Golang

    go语言支不支持安卓开发

    青灯夜游青灯夜游2023-01-05 11:24:40原创175

    go语言支持安卓开发;2014年的时候go语言的开发者就提过android平台会引入go开发,GO语言1.5版本也发布了gomobile用于移动应用程序的开发。Gomobile是一个用于构建和运行用Go编写的移动应用程序的工具,是将Go代码库转换成Android/iOS库的一种方式。

    本教程操作环境:windows7系统、GO 1.18版本、Dell G3电脑。

    大概14年的时候go语言的开发者就提过android平台会引入go开发,GO语言的1.5版本也发布了go mobile

    Gomobile

    Gomobile 是一个用于构建和运行用 Go 编写的移动应用程序的工具,是将 Go 代码库转换成 Android/iOS 库的一种方式.

    将 Go 编译 Android Jar 包

    准备工作:

    mkdir -p ${HOME}/android
    curl https://dl.google.com/android/repository/android-ndk-r24-linux.zip -o android-ndk-r24.zip
    unzip android-ndk-r24.zip && mv android-ndk-r24 ${HOME}/android

    网址: developer.android.com/s, 选择 Command line tools 当中的下载项

    mkdir -p  ${HOME}/android/android-sdk
    
    # Download Tools
    curl https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip -o commandlinetools.zip
    unzip commandlinetools.zip
    mv cmdline-tools ${HOME}/android/android-sdk
    
    # Download Android SDK
    ${HOME}/android/android-sdk/cmdline-tools/bin/sdkmanager "platform-tools" "platforms;android-23" --sdk_root=${HOME}/android/android-sdk/cmdline-tools
    注: android-23 当中的 23 是 API 级别. 这个对应的是 Android6.0, 对于 Android10, 需要 android-29. 自行决定使用哪个API级别的 Android 版本
    go install golang.org/x/mobile/cmd/gomobile@latest
    go install golang.org/x/mobile/cmd/gobind@latest

    // 编译 demo/makefile

    # config
    export ANDROID_HOME=${HOME}/android/android-sdk/cmdline-tools
    export ANDROID_NDK_HOME=${HOME}/android/android-ndk-r24
    export TOOL=${HOME}/android/android-ndk-r24
    
    android: depend
        gomobile bind -target=android/arm64 -androidapi=23 -o device.aar -v -x ${HOME}/demo
    
    ios: depend
        gomobile bind -target=ios -o device.framework -v ${HOME}/demo
    
    depend:
        cd ${HOME}/demo
        gomobile init
        go get golang.org/x/mobile/bind
    
    clean:
        rm -rvf libdevice.*
    androidapi 是 API 版本, target 是CPU架构

    // 源代码 demo/demo.go

    // demo.go
    package demo
    
    import (
        "fmt"
        "io/ioutil"
        "path/filepath"
        "time"
    )
    
    var done = make(chan struct{})
    
    func Start(dir string) {
        fmt.Println("dir", dir)
    
        file := filepath.Join(dir, "test.log")
        err := ioutil.WriteFile(file, []byte(time.Now().String()), 0666)
        if err != nil {
            fmt.Printf("Writefile:%v\n", err)
        }
        select {
        case <-done:
        case <-time.After(10 * time.Minute):
        }
    }
    
    func Stop(mac string) {
        fmt.Println("stop mac", mac)
        close(done)
    }

    将 Go 编译 Android 可执行程序

    准备工作:

    mkdir -p ${HOME}/android
    curl https://dl.google.com/android/repository/android-ndk-r24-linux.zip -o android-ndk-r24.zip
    unzip android-ndk-r24.zip && mv android-ndk-r24 ${HOME}/android
    export ARCH=arm
    export NDK_ROOT=${HOME}/android/ndk-toolchain/${ARCH}
    python ${HOME}/android/android-ndk-r24/build/tools/make_standalone_toolchain.py --arch $ARCH --api 22 --install-dir $NDK_ROOT
    arch 指定目标编译架构 {arm,arm64,x86,x86_64}, api, 指定 Android API版本
    export CC=${HOME}/android/ndk-toolchain/arm/bin/arm-linux-androideabi-gcc
    export GOOS=android
    export GOARCH=arm
    export GOARM=7
    export CGO_ENABLED=1
    
    go build -x main.go
    这里的 main.go 只是一个简单的 Go 程序.

    【相关推荐:Go视频教程编程教学

    以上就是go语言支不支持安卓开发的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:go语言 Golang
    上一篇:go语言中怎么注释多行 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • go语言中指针有哪些运算• 字节跳动需要用go语言吗• Go语言有队列和栈结构吗• go语言中的输出方法有哪些• golang的接口有啥用
    1/1

    PHP中文网