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.
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
Create an Android application
Create a new Android project using Android Studio. Select the "Empty Activity" module in the "New Project" dialog.
Integrate Go
go
directory under the app/src/main/jni
directory. main.go
file in the go
directory, containing the following code: package main // 此函数在 Android 应用程序启动时调用 import "C" func main() {}
app Create a subdirectory corresponding to the application ABI in the /src/main/jnilibs
directory (for example, arm64-v8a
). libgo.so
in the created subdirectory, pointing to the library file generated by Go compilation. Compile the Go code
go build -buildmode=c-shared -o libgo.so main.go
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"); } }
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 {} }
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!