current position:Home>Android + go project

Android + go project

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

background

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 :https://golang.org/dl/

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 golang.org/x/mobile/cmd/gomobile
$ 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 golang.org/x/mobile/example/bind/...

After downloading demo Source path : /Users/ user name /go/src/golang.org/x/mobile/example/.

compile aar file

cd To /Users/ user name /go/src/golang.org/x/mobile/ Execute... In the path

$ gomobile bind -o app/hello.aar -target=android golang.org/x/mobile/example/bind/hello

function Android Demo engineering

Android Studio Open the project $GOPATH/src/golang.org/x/mobile/example/bind/android

  • 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 :

https://github.com/golang/go/wiki/Mobile

copyright notice
author[Yan Long],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262224175404.html

Random recommended