Integrating Go with Android: A Practical Guide

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

Integrating Go with Android: A Practical Guide

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?

Integrating Go with Android: A Practical Guide

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?
Yes, if you have a strong Java team, that’s not a problem, but not all teams are like that. And what I want to emphasize here is the integration of Android and Go, that is, using Go in Android programs rather than writing Android programs entirely in Go.
Integrating Go with Android: A Practical Guide
Some reasons I can think of for using Go in Android:
  • 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.
Being able to use Go code on Android benefits from Go’s powerful cross-compilation capabilities. So how can we use our Go library on Android? Next, I will explain through a simple example.

Example Tutorial

This example uses a simple dynamic library compiled from Go in an Android program to achieve a simple website speed test.
Idea:
  • Cross-compile Go to create a so file supported by the Android platform.
  • Use JNA to call the so file in Android.
Dependencies:
  • Go
  • NDK r20
  • JNA 5.4.0
Note: The demonstration environment is Mac.

Writing Go Test Code

Write the core code of speedtester to detect the access speed of any website:
Integrating Go with Android: A Practical Guide
Write CGO code to expose a Perform API function:
Integrating Go with Android: A Practical Guide

Cross-Compiling Dynamic Library

In the previous article on how to cross-compile CGO programs for ARM architecture on Ubuntu, I mentioned how to cross-compile CGO code, only that the platform was Linux at that time. Now we will use a similar method to compile the Android version.
Cross-compiling the Android version of the dynamic library not only requires specifying GCC but also specifying NDK_TOOLCHAIN, so we first need to download the corresponding NDK.
Step 1: Download ndk r20
Integrating Go with Android: A Practical Guide
After decompression, you can see the ndk directory as follows:
Integrating Go with Android: A Practical Guide
Step 2: Compile toolchain
We can use the make-standalone-toolchain.sh script that comes with the NDK to compile a specific toolchain, using the following command:
Integrating Go with Android: A Practical Guide
Parameter explanation:
  1. The toolchain parameter indicates the corresponding Android ARCH, arm32 uses arm-linux-androideabi-4.9, arm64 uses aarch64-linux-android-4.9.
  2. 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.
  3. 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.
Step 3: Execute cross-compilation
Use the toolchain just compiled to cross-compile:
Integrating Go with Android: A Practical Guide
At this point, a libspeedtester.so file will be generated in the directory. You can check its information with the file command as follows:
Integrating Go with Android: A Practical Guide

Android Code Integration

Step 1: Create a new project named goandroid using Android Studio.
Integrating Go with Android: A Practical Guide
Step 2: Add jna dependency
Add the dependency in the build.gradle file:
Integrating Go with Android: A Practical Guide
Integrating Go with Android: A Practical Guide
Step 3: Add jna Android platform dependency
Create a new directory called arm64-v8a in the app/libs directory, store libjnidispatch.so and our dynamic library libspeedtester.so files in it, and modify the build.gradle file to add sourceSets:
Integrating Go with Android: A Practical Guide
Note:
  1. 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.
  2. The names of the newly created directories vary by phone architecture. For example, my arm64 phone is arm64-v8a, which varies by situation.
Step 4: Add a SpeedTester interface to use our libspeedtester.so library
Integrating Go with Android: A Practical Guide
Step 5: Modify the program homepage to call SpeedTester**
Modify the activity_main.xml file to add the following content:
Integrating Go with Android: A Practical Guide
Modify the MainActivity code to use SpeedTester for speed testing.
Integrating Go with Android: A Practical Guide
Step 6: Modify AndroidManifest.xml, enable network permissions
Integrating Go with Android: A Practical Guide
Step 7: Run on a real machine simulation
Program homepage:
Integrating Go with Android: A Practical Guide
Input http://jd.com, click test, and you can see the output results in Logcat:
Integrating Go with Android: A Practical Guide
You can see that the speed test for the website http://jd.com has been printed in the Logcat log, indicating that our Go code has been successfully called in the Android real machine.

Conclusion

Today we demonstrated how to use Go in Android through a simple example of calling a dynamically linked library compiled by Go in an Android program. The process is roughly as follows:
  • 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!

Integrating Go with Android: A Practical Guide

Integrating Go with Android: A Practical Guide

If you think it’s good, support it ☟ ☟ ☟

Leave a Comment

×