Convert Figma logo to code with AI

xlab logoandroid-go

The android-go project provides a platform for writing native Android apps in Go programming language.

1,064
79
1,064
15

Top Related Projects

5,804

[mirror] Go on Mobile

123,517

The Go programming language

7,344

[mirror] Go Tools

15,712

Application Kernel for Containers

Quick Overview

The xlab/android-go project is a Go binding for the Android platform, allowing developers to create Android applications using the Go programming language. This project aims to provide a seamless integration between the Android ecosystem and the Go language, enabling developers to leverage the strengths of both platforms.

Pros

  • Cross-Platform Compatibility: The project allows developers to write Android applications using Go, which can be compiled for multiple platforms, including Windows, macOS, and Linux.
  • Native Performance: Go's compiled nature and efficient runtime provide native-like performance for Android applications, potentially outperforming Java or Kotlin-based implementations.
  • Simplified Development: By using Go, developers can leverage its strong type system, concurrency primitives, and extensive standard library, potentially simplifying the development process.
  • Ecosystem Integration: The project integrates with the existing Android ecosystem, allowing developers to access Android-specific APIs and libraries within their Go-based applications.

Cons

  • Limited Adoption: As a relatively new and niche project, xlab/android-go may have a smaller developer community and ecosystem compared to more established Android development frameworks like Java and Kotlin.
  • Potential Compatibility Issues: The project may face challenges in keeping up with the rapid evolution of the Android platform, potentially leading to compatibility issues or delays in supporting new Android features.
  • Learning Curve: Developers who are unfamiliar with Go may need to invest time in learning the language and its ecosystem, which could be a barrier to entry for some Android developers.
  • Tooling and Ecosystem Maturity: The tooling and ecosystem around xlab/android-go may not be as mature as those for Java or Kotlin-based Android development, potentially leading to a less streamlined development experience.

Code Examples

Here are a few code examples demonstrating the usage of xlab/android-go:

  1. Creating a Simple Android Activity:
package main

import (
    "github.com/xlab/android-go/app"
    "github.com/xlab/android-go/view"
)

func main() {
    app.Run(func(ctx *app.Context) {
        activity := ctx.NewActivity()
        activity.SetContentView(view.NewTextView(ctx).
            SetText("Hello, Android-Go!").
            SetGravity(view.Gravity_CENTER))
    })
}

This code creates a simple Android activity with a centered text view displaying the message "Hello, Android-Go!".

  1. Handling Button Clicks:
package main

import (
    "github.com/xlab/android-go/app"
    "github.com/xlab/android-go/view"
)

func main() {
    app.Run(func(ctx *app.Context) {
        activity := ctx.NewActivity()
        button := view.NewButton(ctx).SetText("Click me!")
        button.SetOnClickListener(func(_ *view.View) {
            // Handle button click event
        })
        activity.SetContentView(button)
    })
}

This code creates an Android activity with a button that can be clicked. The SetOnClickListener method is used to attach a click event handler to the button.

  1. Accessing Android Sensors:
package main

import (
    "github.com/xlab/android-go/app"
    "github.com/xlab/android-go/sensor"
)

func main() {
    app.Run(func(ctx *app.Context) {
        activity := ctx.NewActivity()
        sensorManager := sensor.NewSensorManager(activity)
        accelerometer := sensorManager.GetDefaultSensor(sensor.TypeAccelerometer)
        // Register a listener to handle accelerometer events
        sensorManager.RegisterListener(accelerometer, func(event *sensor.Event) {
            // Handle accelerometer data
        })
    })
}

This code demonstrates how to access the Android accelerometer sensor using the xlab/android-go library. It creates a sensor manager, retrieves the default accelerometer sensor, and registers a listener to handle sensor events.

Getting Started

To get started with xlab/android-go, follow these steps:

  1. Install the necessary dependencies:

Competitor Comparisons

5,804

[mirror] Go on Mobile

Pros of golang/mobile

  • Officially maintained by the Go team, ensuring reliable and up-to-date support.
  • Provides a more comprehensive set of tools and APIs for building mobile apps with Go.
  • Offers better integration with the Go ecosystem, including access to a wider range of Go libraries and tools.

Cons of golang/mobile

  • Requires more setup and configuration compared to xlab/android-go, which may be more beginner-friendly.
  • May have a steeper learning curve for developers more familiar with traditional mobile development frameworks.

Code Comparison

xlab/android-go

package main

import (
    "fmt"
    "os"

    "github.com/xlab/android-go/app"
)

func main() {
    app.Run(func(a app.App) {
        fmt.Fprintln(os.Stdout, "Hello, Android!")
    })
}

golang/mobile

package main

import (
    "fmt"
    "log"

    "golang.org/x/mobile/app"
    "golang.org/x/mobile/event/lifecycle"
    "golang.org/x/mobile/event/paint"
    "golang.org/x/mobile/event/size"
    "golang.org/x/mobile/event/touch"
)

func main() {
    app.Main(func(a app.App) {
        for e := range a.Events() {
            switch e := a.Filter(e).(type) {
            case lifecycle.Event:
                // ...
            }
        }
    })
}
123,517

The Go programming language

Pros of Go

  • Go is a statically typed, compiled language, providing better performance and type safety compared to the dynamic nature of the Android-Go project.
  • The Go standard library is extensive, offering a wide range of built-in packages and tools for various tasks, reducing the need for external dependencies.
  • Go has a strong focus on concurrency, with features like goroutines and channels, making it well-suited for building scalable and concurrent applications.

Cons of Go

  • Go's syntax and language features may have a steeper learning curve compared to the more familiar Java/Kotlin used in the Android-Go project.
  • The Go ecosystem, while growing, may not have the same level of community support and third-party libraries as the Android ecosystem.

Code Comparison

Go (from golang/go):

func main() {
    fmt.Println("Hello, World!")
}

Android-Go (from xlab/android-go):

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
7,344

[mirror] Go Tools

Pros of golang/tools

  • Actively maintained and updated by the Go team, ensuring compatibility with the latest Go versions.
  • Comprehensive set of tools and utilities for Go development, including the popular go fmt and go vet commands.
  • Extensive documentation and community support.

Cons of golang/tools

  • Primarily focused on the Go language, with limited support for other languages or platforms.
  • May have a steeper learning curve for developers not familiar with the Go ecosystem.
  • Potential compatibility issues with non-standard Go setups or custom toolchains.

Code Comparison

Here's a brief comparison of the code structure between the two repositories:

xlab/android-go

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("Hello, Android!")
    os.Exit(0)
}

golang/tools

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
)

func main() {
    fset := token.NewFileSet()
    f, err := parser.ParseFile(fset, "example.go", nil, 0)
    if err != nil {
        fmt.Println(err)
        return
    }
    ast.Print(fset, f)
}

The key differences are:

  • The xlab/android-go repository focuses on providing a simple example of running Go on Android, while the golang/tools repository contains a more complex set of tools and utilities for Go development.
  • The xlab/android-go code is a basic "Hello, Android!" program, while the golang/tools code demonstrates the use of the Go parser and AST (Abstract Syntax Tree) package.
15,712

Application Kernel for Containers

Pros of gvisor

  • gvisor provides a comprehensive container runtime environment, offering a high level of security and isolation for containerized applications.
  • gvisor is actively maintained and developed by Google, ensuring regular updates and improvements.
  • gvisor supports a wide range of Linux distributions and can be integrated with popular container orchestration platforms like Kubernetes.

Cons of gvisor

  • gvisor has a larger codebase and may have a steeper learning curve compared to Android-Go.
  • gvisor's focus on security and isolation may come at the cost of performance, especially for certain workloads.
  • gvisor's integration with other container tools and platforms may be more complex compared to the simplicity of Android-Go.

Code Comparison

Android-Go:

func main() {
    app := android.NewApp()
    app.Run()
}

gvisor:

func main() {
    ctx := context.Background()
    sandbox, err := sandbox.New(ctx, &sandbox.Config{})
    if err != nil {
        // Handle error
    }
    defer sandbox.Destroy(ctx)
    // Use sandbox to run containers
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

android-go Go Report Card Lines of Code

The android-go project aims to provide a platform (namely an SDK) for writing native Android apps in Go programming language. All things here were designed to be less coupled as possible, thus maintaining a great level of flexibility while keeping things simple and easy to use. The approach and tools are different from gomobile, please distinguish them apart.

The project was one of the best Go Newsletter items of 2016. In this final issue of the year, they look back at the most popular Go news and links of 2016. 🌟

❗️Important! Please see cmd/android-project utility that replaces the original android from SDK that has been stripped in latest SDK releases. All example Makefiles were updated, I advise you to do the same rather than stick to old SDK versions.

Project structure

android GoDoc

Package android provides Go bindings for the Android NDK API. They've been automatically generated by c-for-go using the official NDK headers from android-23 platform. Keep in mind that different NDK platforms may implement different sets of API available, thus some of features used by this binding may not be available in older versions of platform. But I tested with the android-21 toolchain and got no issues. Some files, like android/sensors.go for example, have been written by hand to expose some features that CGO does not handle well.

This package allows to write code that works directly with NDK API, bypassing all the CGO bookkeeping and boilerplate bloat in your code. But safety is strictly advised. There is also a JNI calling mechanism that allows easy interactions with Java VM and exposes the full potential of Android SDK! See android/jni_util.go for example of toggling Android keyboard and other JNI-based utils.

Example usages: app/queue.go, example, example-egl, nk-android.

cmd/android-project

Tool android-project is a simple replacement for infamous android util from Android SDK, prior to Android SDK Tools Revision 25.3.0 (March 2017) release when they dropped that util abruptly and got back a stripped version later. Needs to be installed first:

go get github.com/xlab/android-go/cmd/android-project

app GoDoc

Package app implements a NativeActivity glue layer required to properly handle the startup process and the native activity events. Import this package into your Go application to make it Android-compatible. Some pieces required for a proper main.main trampoline have been borrowed from gomobile, the absolute minimum to get this stuff invokable as a native activity. Most of the code in this package provides wrappers around NativeActivity event callbacks.

Example usages: example, example-egl, nk-android.

egl GoDoc

Package egl provides Go bindings for EGL API. They've been automatically generated by c-for-go using the official NDK headers from android-23 platform. All functions have their reference to the offical Khronos documentation. Some files, like egl/errors.go for example, have been written by hand to expose some features that would make it more idiomatic in the Go world.

Examples of usage in conjuction with the android package: example-egl, nk-android.

gles GoDoc

Package gles provides Go bindings for the OpenGL ES v1 API. They've been automatically generated by c-for-go using the official NDK headers from android-23 platform. All functions have their reference to the offical documentation.

Example of usage in conjuction with the android package: example-egl.

gles2 GoDoc

Package gles2 provides Go bindings for the OpenGL ES v2 API. They've been automatically generated by c-for-go using the official NDK headers from android-23 platform. All functions have their reference to the offical documentation.

Example of usage in conjuction with the android package: nk-android.

gles3 GoDoc

Package gles3 provides Go bindings for the OpenGL ES v3 API. They've been automatically generated by c-for-go using the official NDK headers from android-23 platform. All functions have their reference to the offical documentation.

Example of usage in conjuction with the android package: nk-android.

gles31 GoDoc

Package gles31 provides Go bindings for the OpenGL ES v3.1 API. They've been automatically generated by c-for-go using the official NDK headers from android-23 platform. All functions have their reference to the offical documentation. The OpenGL ES computing API is supported.

Examples

Refer the example links to get more info about them.

There are three examples. The first example is a template app showing how to create and build an Android application using the absolute minimum of code and boilerplate. It also shows the primitives of an activity and how to handle activity events, there is no visual part, so be ready to read the lines from the ADB logcat. If this one works correctly on your device, my congratulations. If not, please open an issue.

The example-egl leverages all three packages together: android, egl and of course gles (OpenGL ES 1.0) to create an visual app that animates its color based on the accelerometer values. It also reads input events such as key events and multitouch motion events (with pressure, if supported by the device), you can check these events in the ADB logcat. Please see the video of the expected behaviour:

Golang + EGL/GLES App on Android

And recently I took nuklear package and implemented a few backends in Go, including two for Android that initialize OpenGL ES 2 or ES 3 context using android, egl, gles2 and gles3 packages. They also responsible for handling touch and other input events. So now it is possible to create GUI apps for Android, see nk-android for an example Nuklear GUI app.

Nuklear GUI App written in Golang runs on Android

Android keyboard toggling video.

On existing tools or why not just use Gomobile

TL;DR it's a "three wheel bicycle" in terms of hacking flexibility. Conversely, this project tries to fill the niche by providing a platform that is minimal as possible and respects the bleeding-edge features such as OpenGL ES 3.1, ES 3.2 and of course Vulkan API.

GoMobile has different aims and motivation with a lot of implications. First of all, it has been grown as a bootstrap-script. That's it: a tool with a lot of hardcoded logic that helped to popularize Go on mobile platforms. Still provides a simple way to start building Android and iOS apps in no time, without diving deeply into any of the platform-specific complications. It provides a lot of wrappers and helpers that abstract all the platform-related boilerplate and logic away, making your apps look the same on any platform: Android, iOS, PC (Linux/OS X/Windows).

Also, considering that binding feature of GoMobile, it's a good option to use when you already have lots of Java/Obj-C code and want to integrate some of Go libs into the project. The tool is mostly for experienced mobile developers who just want to try Go sometimes. They provide a framework that hides platform details, but they also may be to restrictive and limited due to this and can't be used for crazy experimenting stuff.

An example: for Android developent, gomobile defaults to r10 SDK with android-15 stripped-down NDK, there is a lot of code written to automate this process and there is no way to override this environment, even by rewriting a lot of gomobile bootstrapping code. It's a great mess. I wasted too much time trying to switch to the r12-beta1 SDK and the android-23 NDK platform. Instead of providing a good document how to build the Go-based apps and incorporate them into any of the existing development process, GoMobile project introduces tons of hardcoded scripts.

Contributing

Feel free to share bugs, I expect a lot of weird cases. Please also share this project in social networks so more people would know how to write Go apps for Android that are not limited.

License

All the code except when stated otherwise is licensed under the MIT license.