来自Go的Android API

huangapple go评论103阅读模式
英文:

Android API from Go

问题

我知道Go程序可以编译为Android。

如何在Go程序中使用Android特定的API,比如获取GPS坐标或者使用默认浏览器打开URL?

英文:

I know that Go programs can be compiled for Android.

How can I use Android specific API, like getting GPS coordinates or opening a URL with the default browser, from within a Go program?

答案1

得分: 9

我很抱歉,目前几乎不可能实现。在“遇见Go团队”I/O会议上,Go团队的成员表示他们没有计划将Android支持添加到Go中。

目前我们只有一个针对ARM架构的编译器。不幸的是,这对于真正的Android应用程序几乎没有用处,尽管这些程序可以从Android设备的命令行启动。

大部分Android框架是用Java编写的,所以要与其交互,你的代码应该编译为一个*.so库,通过JNI接口加载和调用。而目前的Go编译器(gc,不确定gccgo是否可以)无法实现这一点。

也许你可以使用cgo来绑定Android NDK API,这样就可以在Go中创建应用程序,支持API级别9(Android 2.3)。

更新:现在你可以使用Go中的JNI,并使用golang.org/x/mobile包自动生成Java绑定。在Go 1.4中,这仍然是实验性的,但计划在Go 1.5发布中将其包含进去。该包还提供了GL、音频和用户输入的绑定(希望他们也能添加iOS支持,这样将来就可以兼容Android和iOS)。无论如何,该包主要面向在Go中编写游戏,而不是将Go用作Android上Java的替代品。

英文:

I'm afraid it's hardly possible at the moment. In the "Meet the Go Team" I/O sessions, the guys from the Go team stated that they have no plans to add Android support to Go.

What we have now is just a compiler for ARM architecture. Unfortunately, this is pretty much useless for real Android apps, though such programs can be launched from the command line on Android devices.

Most of the Android framework is written in Java, so to interact with it your code should be compiled to a *.so libary, that will be loaded and called via the JNI interface. And it's not possible with the current Go compiler (gc, not sure about the gccgo).

Maybe you will be able to make bindings to the Android NDK API with cgo, that would allow you to create applications in Go since API level 9 (Android 2.3)

UPD: You can now use JNI from Go and create java bindings automatically with golang.org/x/mobile package. In Go 1.4 it's still experimental, but there are plans to include it into Go 1.5 release. The package also provides bindings for GL, audio and user input (hopefully they would also add iOS support and that would be compatible for Android and iOS one day). Anyway this package is mostly oriented at writing games in Go, rather than using Go as a replacement for Java on Android.

答案2

得分: 7

请看一下我对https://stackoverflow.com/questions/10095698/android-app-from-go-programming-language/18616207#18616207的回答。goandroid项目允许您通过NDK在Go中创建Android应用程序。

(免责声明:我是goandroid的作者)

编辑:如评论中所提到的,Go 1.5增加了对纯Go或Java和Go混合的Android应用程序的官方支持。预计iOS也将在最终的1.5版本发布时到来。有关更多详细信息,请参阅https://github.com/golang/mobile。

英文:

Take a look at my answer to https://stackoverflow.com/questions/10095698/android-app-from-go-programming-language/18616207#18616207. The goandroid project allows you to create Android apps in Go through the NDK.

(Disclaimer: I'm the author of goandroid)

Edit: As mentioned in the comments, Go 1.5 adds official support for Android apps in pure Go or as a mix of Java and Go. iOS is also expected to arrive in time for the final 1.5 release. See https://github.com/golang/mobile for more details.

答案3

得分: 2

GO 1.4 doc说,"Go 1.4可以构建在运行Android操作系统的ARM处理器上运行的二进制文件。它还可以构建一个.so库,可以使用mobile子存储库中的支持包由Android应用程序加载。"

在"golang.org/x/mobile/app"库中有一个package app选项,可以让您编写适用于Android(以及最终适用于iOS)的应用程序。

英文:

GO 1.4 doc says, "Go 1.4 can build binaries for ARM processors running the Android operating system. It can also build a .so library that can be loaded by an Android application using the supporting packages in the mobile subrepository"

There is package app option in "golang.org/x/mobile/app" library that lets you write Apps for Android (and eventually, iOS).

答案4

得分: 1

步骤1:使用Golang创建一个平台无关的GUI库,该库使用OpenGL进行绘制,并使用智能事件和数据绑定系统编写应用程序。使用OpenGL的任何软件都可以更或多或少地进行移植。基本上,用Golang重新编写Kivy。

步骤2:创建基于内省/反射的包装器,用于使用Java类,类似于PyJNIus(也是Kivy项目)。

步骤3:还有很多艰苦的工作要做,因为要达到Kivy的水平还有很多事情要做。

步骤4:获利。

英文:

Step 1: Create a platform independent GUI library using Golang that uses OpenGL to draw and an intelligent event and data-binding system to write the apps in. Any software using OpenGL is going to be more-or-less portable. Essentially, re-write Kivy in Golang.

Step 2: Create introspection/reflection based wrapper for using Java classes similar to PyJNIus (also a Kivy project).

Step 3: Lots more hard work, because there is a lot to do to get to the level of Kivy

Step 4: Profit

huangapple
  • 本文由 发表于 2012年7月14日 13:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/11481362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定