This is a creation in Article, where the information may have evolved or changed.
At the end of last year's lawsuit against Oracle and Google, but Android developers are still eager to Google, like Apple, to develop their own programming language for Android application development, as to what language to choose, most developers want to be Golang, First of all, he is Google's self-developed programming language, because he is simple syntax, high efficiency.
As Golang iteration updates, we are increasingly seeing Google's efforts in this area, Golang.org/x/mobile release, allowing developers to use the go language to develop Android applications using the NDK. This link http://www.jianshu.com/p/403aa507935b describes how to use Gomobile for the development of Android applications and the development of ANDROCU files.
However, the use of the process will find a problem, go to develop the Android app, or all use go with the NDK development (using OpenGL drawing interface), or can only be used to write library files for Java to call (packaging an AAR file to call), and not as the Kotlin language, Really do syntax and programming on the one by one map.
Recently, however, the mobile library has been found to be updated, Go reverse bind android app in 100% Go.
and the readme is 100% bindings for go inversion and Android apps (translation may not be in place, English is too poor).
1 Installing Gomobile
Here you need go get mobile This package, command for go get golang.org/x/mobile/cmd/gomobile
. Because Googlesource is wall, you can go to GitHub to download the relevant files, the address is https://github.com/golang/mobile
, put this mobile folder under Gopath>src>golang.org>x, and then execute go get golang.org/x/mobile/cmd/...
, At this point you will see in the Gopath>bin folder that generated two executables, one is Gobind, and the other is gomobile.
2 Initializing Gomobile
With gomobile init -v
initialization, you may need to wait a while (direct gomobile init-v command, you must ensure that the NDK has been added to the environment variable path below, if not joined, can also be used gomobile init -v -ndk 你的ndk目录
to initialize)
3 Working with Android Studio
Use the official example directly, open Android Studio, click File>new>import Moudle, and gopath>src>golang.org>x>mobile> Example>reverse>android Import into your project, you will find an Android Moudle, here you will find that the activation activity is written in Golang, source code in the golang.org/x /mobile/example/reverse/reverse/reverse.go This file, here is the source code
package reverseimport ( "Java/android/databinding/DataBindingUtil" "Java/android/os" "Java/android/support/v7/app" gopkg "Java/reverse" rlayout "Java/reverse/R/layout" "Java/reverse/databinding" "Java/reverse/databinding/ActivityMainBinding")type MainActivity struct { app.AppCompatActivity binding databinding.ActivityMainBinding}func (a *MainActivity) OnCreate(this gopkg.MainActivity, b os.Bundle) { this.Super().OnCreate(b) db := DataBindingUtil.SetContentView(this, rlayout.Activity_main) a.binding = ActivityMainBinding.Cast(db) a.binding.SetAct(this)}func (a *MainActivity) OnDestroy(this gopkg.MainActivity) { a.binding = nil // break reference cycle this.Super().OnDestroy()}func (a *MainActivity) GetLabel() string { return "Hello from Go!"}
Can be found and we usually write activity in Java very similar, specific use, we can explore their own
To view this moudle build.gradle file, you will see the following code
gobind { pkg = "golang.org/x/mobile/example/reverse/reverse"}
Find the path to the gopath that must be added below the Linux system
gobind { pkg = "golang.org/x/mobile/example/reverse/reverse" GOPATH="/home/tenny/gopath"}
When you click the Run button, you'll find the app installed on your phone.
Webwxgetmsgimg.jpg
Problems that may arise (pit dead)
Android Studio newspaper ' Gomobile ' finished with Non-zero exit value 1 Error
View Gradle console with error ' Gomobile ' Go not Found
This error is my Deepin Linux system, the window does not appear, go not found prove that the go executable file is not found, but open the command line separately, using the GO command is available, later found that the original system will only find/usr/local/go/bin The following execution file, so you need to put the Go language pack at/usr/local/, and need to modify the environment variables.
Android Studio Newspaper Javac cannot execute binary file
This error is similar to the above one, also found on the Linux top, window does not appear, if you are manually installed JDK, then you need to use Adt-get install JDK, the specific method of Baidu itself.
3.
$ gomobile bind Golang.org/x/mobile/example/reverse/reversegomobile:loadexportdata failed Go install-pkgdir=/users/ Hajimehoshi/go/pkg/gomobile/pkg_android_arm-tags= ""-gcflags=-shared-ldflags=-shared golang.org/x/mobile/example /reverse/reverse failed:exit status 1../.. /.. /golang.org/x/mobile/example/reverse/reverse/reverse.go:9:2:cannot Find Package "java/android/databinding/ Databindingutil "in.:/usr/local/go/src/java/android/databinding/databindingutil (from $GOROOT)/var/f olders/7t/qw3np69559591s1v0mk5_p1m0000gn/t/gomobile-work-600308528/gen/src/java/android/databinding/ Databindingutil (from $GOPATH)/users/hajimehoshi/go/src/java/android/databinding/databindingutil. /.. /.. /golang.org/x/mobile/example/reverse/reverse/reverse.go:11:2: Cannot find package "Java/android/support/v7/app" in Any of:/usr/local/go/src/java/android/support/v7/app (from $GOROOT)/var/folders/7t/qw3np69559591s1v0mk5_p1 M0000gn/t/gomobile-work-600308528/gen/src/javA/android/support/v7/app (from $GOPATH)/users/hajimehoshi/go/src/java/android/support/v7/app. /.. /.. /golang.org/x/mobile/example/reverse/reverse/reverse.go:12:2: Cannot find package "java/go/reverse/r/layout" in any Of:/usr/local/go/src/java/go/reverse/r/layout (from $GOROOT)/var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn /t/gomobile-work-600308528/gen/src/java/go/reverse/r/layout (from $GOPATH)/users/hajimehoshi/go/src/java/go/ Reverse/r/layout. /.. /.. /golang.org/x/mobile/example/reverse/reverse/reverse.go:13:2: Cannot find package "java/go/reverse/databinding/ Activitymainbinding "in.:/usr/local/go/src/java/go/reverse/databinding/activitymainbinding (from $GOROOT) /var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn/t/gomobile-work-600308528/gen/src/java/go/reverse/databinding/ Activitymainbinding (from $GOPATH)/users/hajimehoshi/go/src/java/go/reverse/databinding/activitymainbinding
This error will appear on the Mac, this is the mobile library problem, thanks to the great God made the changes, need to googlesource checkout related branches
git fetch https:// Go.googlesource.com/mobile refs/changes/35/38635/1 && git checkout fetch_head
Googlesource because by the wall, May not git down, but don't worry, I upload a backup http://pan.baidu.com/s/1hsDUp28 Baidu cloud, you can download it, and then replace the mobile file on the line.