Click on the Terminal R&D Department, select “Set as Starred”
Every day at 8:30 AM, technical articles are delivered on time
Reply “Learn” in the public account backend to get exclusive quality materials from the author
Previous Articles
This View is a bit cool, fill-in-the-blank View effects go!
Latest 2019 Vue Development Guide, worth collecting!
An unforgettable Baidu phone interview
Programmer dating illustration! Girlfriend treated as a private variable, lover as a pointer
Breaking news! After the source code of Vue 3.0 was released, what changes have occurred?

Author: Xiaoyu Station
Source: http://www.songjiayang.com/posts/go-he-andji-shi-li?from=timeline
Introduction
Seeing this title, you may ask, why run Go on Android, isn’t it good to use Java directly?

-
The team is familiar with Go and knows little about Java/Android. -
There is already existing Go core code, such as open-source libraries: libp2p, turn/stun libraries, etc. -
The core logic of the SDK of the service is complex and tedious, involving a lot of network or concurrency operations.
Example Tutorial
-
Cross-compile Go to create a so file supported by the Android platform. -
Use JNA to call the so file in Android.
-
Go -
NDK r20 -
JNA 5.4.0
Writing Go Test Code


Cross-Compiling Dynamic Library



-
The toolchain parameter indicates the corresponding Android ARCH, arm32 uses arm-linux-androideabi-4.9, arm64 uses aarch64-linux-android-4.9. -
The platform parameter indicates the corresponding Android API version, 25 corresponds to Android 7.1.1, 26 corresponds to Android 8.0, 27 corresponds to Android 8.1, 28 corresponds to Android 9.0, 29 corresponds to Android 10.0. -
The install-dir parameter indicates the location where the compiled target toolchain will be stored, which will be used for cross-compiling Go code later.


Android Code Integration




-
libjnidispatch file needs to be selected based on different CPU architectures. You can click the link jna/dist to download the corresponding platform’s jar package, extract the jar package, and extract the libjnidispatch file. -
The names of the newly created directories vary by phone architecture. For example, my arm64 phone is arm64-v8a, which varies by situation.






Conclusion
-
Select NDK’s make-standalone-toolchain.sh to compile the native environment’s toolchain. -
Use the compiled toolchain to cross-compile the Android system’s dynamic library. -
Use jna in Android to use the dynamic library.
Code reference: https://github.com/songjiayang/go-android
Read more
Summary of programmers’ experience in taking private work
Componentization makes your code more elegant!
Is browsing GitHub too slow? Teach you two tricks
What is the current situation of the game industry in the internet circle?
Besides coding, do you have any side jobs?
As someone from the IT industry, what do you want to say to the younger generation?
Believe in yourself, there is nothing you cannot do, only things you haven’t thought of
What you gain here is not just technology!
If you think it’s good, support it ☟ ☟ ☟