2015年9月24日 星期四

[Golang]Build go library for Android using gomobile step by step

Build go library for Android using gomobile step by step

1. Supposed  \src\gobindtemp  is my go lib source file

2. Build arr file

gomobile bind [directory under gopath]
ie.  gomobile bind -target=android gobindtemp

generate an aar file  ie. gobindtemp.aar

I suggest go to the project directory use 

gomobile bind -target=android

this will generate .aar file in the project directory

otherwise , .aar is generate in the same directory with /src

3. Establish an Android Project in AndroidStudio 

I create a project called gobindtemplate

4. Add a New Module into Project

Go to File -> New -> New Module
Select your arr file (here is gobindtemp.aar) and push finish button


  • (** Please ignore the warning, that is because i have added the module already)

5. Setup gradle file for module

Copy and modify from 
\src\golang.org\x\mobile\example\bind\android\hello\build.gradle to module's build.gradle  and modify gopath

6. Setup gradle for Android App

In dependencies: compile project 
here is compile project(':gobindtemp')

7. Test Build the project 

I simply use gradlew assembleDebug in terminal to test build

8. Import go library


in Activity where you want to use go lib, import golibrary
import go.gobindtemp.Gobindtemp

9. Using Class and Function

String greetings2 = Gobindtemp.Greetings("Android and Gopher");

10. Run Program 


沒有留言:

張貼留言