Go practices in Android development

WBOY
Release: 2024-04-08 10:33:01
Original
963 people have browsed it

Practice Go in Android development: Set up Go development environment: Install Go and Android NDK. Create an Android app: Create a new project using Android Studio. Integrate Go: Create the go directory and main.go file in the jni directory. Compile Go code: Run the go build command to compile the main.go file. Add Native interface: Declare native method callGo() in MainActivity.java. Load the Go shared library: Use System.loadLibrary("go") in MainActivity.java to load the Go shared library. Practical case: Display a Toast message in main.go and call the MakeToast() method in Android.

Android 开发中的 Go 实践

Go practice in Android development

Introduction

Go is developed by Google A modern programming language known for its simplicity, concurrency, and efficiency. In the world of Android app development, Go is gaining popularity as it offers native performance and cross-platform advantages. This article will guide you how to use Go in Android applications and provide a practical example.

Set up the Go development environment

  1. Install Go: Visit https://go.dev/dl/ to download and install Go.
  2. Configure environment variables: Add the Go installation directory to the PATH environment variable.
  3. Install Android NDK: Follow the instructions at https://developer.android.com/ndk/downloads to install the Android NDK.

Create an Android application

Create a new Android project using Android Studio. Select the "Empty Activity" module in the "New Project" dialog.

Integrate Go

  1. Create the go directory under the app/src/main/jni directory.
  2. Create the main.go file in the go directory, containing the following code:
package main

// 此函数在 Android 应用程序启动时调用
import "C"

func main() {}
Copy after login
  1. In app Create a subdirectory corresponding to the application ABI in the /src/main/jnilibs directory (for example, arm64-v8a).
  2. Create a symbolic link named libgo.so in the created subdirectory, pointing to the library file generated by Go compilation.

Compile the Go code

  1. Open a terminal window and navigate to the project directory.
  2. Run the following command to compile Go code:
go build -buildmode=c-shared -o libgo.so main.go
Copy after login

Add Native interface

In order to call Go code in Android code, you need to add a JNI (Java Native Interface) interface. Create the MainActivity.java file in the app/src/main/java/<PACKAGE_NAME> directory, containing the following code:

import android.app.Activity;
import android.os.Bundle;

// 声明一个 native 方法
public class MainActivity extends Activity {
    // 此方法将调用 Go 代码
    public native void callGo();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 调用 Go 代码
        callGo();
    }

    // 加载 Go 共享库
    static {
        System.loadLibrary("go");
    }
}
Copy after login

Actual case: Displaying a Toast message

Use Go to display a Toast message. Add the following code in the main.go file:

package main

import "C"

// 在 Android 中显示 Toast 消息
import (
    "context"
    "log"

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

func main() {
    ctx := context.Background()
    activity := android.ActivityFromContext(ctx)

    // 创建一个 Toast 消息
    toast := activity.MakeToast()
    toast.SetText("Hello from Go!")

    // 显示 Toast 消息
    toast.Show()

    // 主 Go 程序进入阻塞
    log.Println("native: waiting")
    select {}
}
Copy after login

Run the application

Compile and run the Android application. You should see a Toast message from your Go code on the device or simulator.

Summary

Using Go to develop Android applications can improve performance, simplify concurrency, and realize cross-platform benefits. This article provides a step-by-step guide to help you integrate Go and provides a practical example of displaying Toast messages.

The above is the detailed content of Go practices in Android development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template