current position:Home>Android + go project

Android + go project

2022-01-26 22:24:20 Yan Long


Android I've been using it JNI call C++ Code , To do cross platform development . I've been in contact with GO Language , Found in fact GO It also supports cross platform development .

Environment building

1、 install go Environmental Science 2、 To configure gomobial Environmental Science

important tips: The development environment needs to access the Internet through science

install go Environmental Science

By installing the package , download newest go Environment installation package :

After installation , Configure environment variables ~/.bash_profile

export GOROOT=/usr/local/go;

export GOPATH=/Users/$(whoami)/go;

export ANDROID_HOME=/Users/$(whoami)/Library/Android/sdk;

export PATH=${PATH}:$GOROOT/bin:$GOPATH/bin:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools;

export ANDROID_NDK="/Users/$(whoami)/Documents/tools/android-ndk-r16b";

export ANDROID_SDK_ROOT="/Users/$(whoami)/Library/Android/sdk";

To configure gomobial Environmental Science

Go Mobile Introduced a new tool gomobile, To help with the build and bind process . Use Go 1.16.x when , We must execute the following command to install / Use gomobile, Then switch the module aware build mode to auto

go env -w GO111MODULE=auto

install gomobile Tools

$ go get
$ gomobile init

Example Demo

go Code compiled into aar The way , Import Android engineering .

Grab the example by running the following command .

$ go get -d

After downloading demo Source path : /Users/ user name /go/src/

compile aar file

cd To /Users/ user name /go/src/ Execute... In the path

$ gomobile bind -o app/hello.aar -target=android

function Android Demo engineering

Android Studio Open the project $GOPATH/src/

  • Copy Compiled before aar File to engineering libs Catalog
  • modify android\app\build.gradle file
+ repositories {
+    flatDir {
+        dirs '.'
+    }
+ }

dependencies {
+    implementation (name:'yourmodulename', ext:'aar')

After running the project , And you can see the result .

Main Activity Display characters in , Namely go Provided in .

Reference article :

copyright notice
author[Yan Long],Please bring the original link to reprint, thank you.

Random recommended