Develop Android app with Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

I. Download the official source Https://github.com/golang/mobile

git clone https://github.com/golang/mobile.git

Two. Move the mobile directory to the $gopath/golang.org/x

Mkdir-p $gopath/golang.org/x && MV Mobile $gopath/golang.org/x/

Three. Copy the official example basic to Gopath

Cp-a $gopath/golang.org/x/mobile/example/basic $gopath/

Four. Run the compile command

CD $gopath/src/basic./make.bash

1. If an error occurs

Go build runtime:android/arm must be bootstrapped using Make.bash

Indicates that you have not configured the Go cross-compilation environment, that is, your current system cannot compile a binary file that can run on the Android/arm CPU *.so

Go to your NDK directory and run make-standalone-toolchain.sh

CD ~/ANDROID-NDK &&/build/tools/make-standalone-toolchain.shcopying prebuilt binaries ... Copying sysroot Headers and libraries ... Copying C + + runtime headers and libraries ... Creating package file:/tmp/ndk-oldfeel/arm-linux-androideabi-4.9.tar.bz2cleaning ... Done.

If it appears

Unable to auto-config arch from Toolchain

This means that Toolchain cannot be configured automatically and needs to be configured manually. First look at what CPU your NDK supports to compile

oldfeel@oldfeel:~/android-ndk$ ls toolchains/aarch64-linux-android-4.9         mipsel-linux-android-4.8aarch64-linux-android-clang3.4    mipsel-linux-android-4.9aarch64-linux-android-clang3.5    mipsel-linux-android-clang3.4arm-linux-androideabi-4.6         mipsel-linux-android-clang3.5arm-linux-androideabi-4.8         renderscriptarm-linux-androideabi-4.9         x86-4.6arm-linux-androideabi-clang3.4   x86-4.8arm-linux-androideabi-clang3.5    x86-4.9llvm-3.4                          x86_64-4.9llvm-3.5                           x86_64-clang3.4mips64el-linux-android-4.9       x86_64- clang3.5mips64el-linux-android-clang3.4  x86-clang3.4mips64el-linux-android-clang3.5   x86-clang3.5mipsel-linux-android-4.6

edit make-standalone-toolchain.sh, find and modify toolchain_name= for

Vim build/tools/make-standalone-toolchain.sh
toolchain_name=arm-linux-androideabi-4.9

Re-execute

./build/tools/make-standalone-toolchain.sh--platform=android-14copying prebuilt binaries ... Copying sysroot Headers and libraries ... Copying C + + runtime headers and libraries ... Creating package file:/tmp/ndk-oldfeel/arm-linux-androideabi-4.9.tar.bz2cleaning ... Done.

Unzip the/tmp/ndk-oldfeel/arm-linux-androideabi-4.9.tar.bz2 (you may be a different name, shown in the running results above) and arm-linux-androideabi-4.9/all the files inside * Move to the NDK directory

MV arm-linux-androideabi-4.9/* ~/android-ndk/

At this time, you can configure the Go cross-compilation environment under GO/SRC ( For example, my go source is placed in the ~/go)

Note: modify the ~/ANDROID-NDK in the second line of the command to its own NDK directory

CD ~/GO/SRCCC_FOR_TARGET=~/ANDROID-NDK/BIN/ARM-LINUX-ANDROIDEABI-GCC goos=android GOARCH=arm GOARM=7 CGO_ENABLED=1. /make.bash

Finally appeared

---installed Go for android/arm in/home/oldfeel/goinstalled commands In/home/oldfeel/go/bin

Description successfully configured, execute Make.bash again

CD $gopath/src/basic./make.bash

If it appears

./make.bash: Line 16:ndk-build: Command not found

Note that the NDK directory is not put into an environment variable, and you need to configure Android_home=sdk.dir

Vim ~/.profile

At the bottom of the add

Export android_home= $HOME/android-sdkexport path= $PATH: $HOME/android-ndk

Refresh environment variable (this refresh is only valid for the current terminal, restart the computer if you want to be valid for all terminals)

SOURCE ~/.profile

The execution of Make.bash should be successful.

CD $gopath/src/basic./make.bash


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.