This is a creation in Article, where the information may have evolved or changed.
Environment configuration
- Install Go 1.5 or later, the specific installation steps are available on the official website Https://golang.org/doc/install
Installing Gomobile Https://godoc.org/golang.org/x/mobile/cmd/gomobile
Execute the following command, you may need to flip the wall during installation
go get -v golang.org/x/mobile/cmd/gomobilegomobile init -v
The following issues may occur during the installation process
- Prompt
unrecognized import path
error, usually after turning over the wall can be resolved
- Prompt
import path does not begin with hostname
error, if set GOROOT
then cancel this setting
- Prompt
$GOPATH not set
for errors, check for settings GOPATH
and do not add when using commandssudo
- Prompting a directory for missing permissions, authorizing the current user
sudo chown -R <当前用户名> <提示的目录名>
Writing code
- Create an Android project and write native code. Note: The
targetsdkversion
for Android Works cannot be 23.
- Create the corresponding app package name under
Gopath
and write the GO code.
-
Create a new Gradle module under Android Engineering and modify the module's build.gradle
file.
plugins {ID "org.golang.mobile.bind" version "0.2.4"}gobind {//Go app package name pkg = "com. Bookislife/goandroid "//Gopath path, can be viewed by executing ' go env ' to view Gopath ="/users/sidney/gopath "//go binary file go ="/usr/loca L/cellar/go/1.5.3/bin/go "//gomobile binary file, default to ' Gopath/bin/gomobile ', if installed in other directories need to manually specify//Gomobile ="/users/sidne Y/gopath/bin/gomobile "}
- ensures that the
Settings.gradle
for Android app root target references this module.
- compiles the go code,
./gradlew Gobind
, and the AAR
file containing the Native file is generated under the Go module created in step 3 when the compilation is complete.
- normally uses the Native code in Android Engineering, and subsequent operations are the same as for normal applications.