
The development of mobile applications is always a compromise between what one wants to do and what the device platform allows to do. This article describes how to increase the capabilities of the Android application with Golang.
Here you will not find the leakage of secret api android. The mechanisms used are the standard (or almost standard) tools android and golang, which are described on official sites and specialized forums. But in the form of a single action plan, and even in Russian - this is published for the first time and exclusively for Habr.
Prehistory
According to numerous requests from users of one of the applications, text predictions have been developed. Predictions served algorithm
radix tree , implemented in a separate android-library. The algorithm showed good performance along with an economical consumption of resources - gave the result for tens of milliseconds with a consumption of 1.5 MB of memory. It is working performance. But this was only the case with isolated testing of the library on the JVM.
')
Problems began when connecting the library to the application. On android, the prediction algorithm began to slow down, and the output of the results dragged on for up to 7 seconds. Wow! Yes, in 7 seconds you can manually type a word, erase and type it correctly. Such predictions are no good.
The analysis did not reveal any code problems. The algorithm worked flawlessly, consumed memory in strictly allocated amounts, was not distracted by extraneous processes, everything was asynchronous, there were plenty of hardware resources. Only one conclusion suggested itself - the android dalvik virtual machine has a performance different from that of the JVM, and within the framework of dalvik this code is doomed to a brake.
Then it was decided that the prediction algorithm should be taken out of dalvik. There is a
JNI - Java Native Interface for this . A mechanism that allows calling methods from libraries written in C / C ++ from java. And back, from the C / C ++ library, call java methods.
Decision
To create a native library, Go was selected. Just because there is experience with it, unlike C / C ++. Golang is adorable, but this path has its difficulties, as we get an extra level of difficulty. For pure C / C ++, it is enough to use the
NDK and follow the instructions described on the
android developers website. For Golang, you will have to study the compilation of golang for android, you can start
here .
Ahead lay a path full of dangers and adventures. But now this path has already been passed, and I will be happy to share his plan.
Plan
Development environment android:- OS: Windows 7.
- IDE: Eclipse with ADT. But this plan is suitable for Android Studio.
Golang development environment:- OS: To compile golang under android, use linux. I am using Ubuntu 14.04 running on VirtualBox.
- Android SDK.
- Download the archive from the official android website
- Unpack, for example, in
$HOME/android/android-sdk-linux
- Java JDK.
$ apt-get default-jdk
- Go 1.5.
The current release version of Go is 1.4. But I didn’t manage to compile the library with it, I made a mistake somewhere when assembling the toolchain. Therefore, I used the gomobile package from the development version of Go 1.5, the release of which is still planned. There is no release yet, the installation of Go 1.5 is described here :
- Install Go 1.4 golang.org/doc/install
- Clone the current golang repository:
$ git clone https://go.googlesource.com/go $HOME/go
- Compile the current version of Go using Go 1.4
$ export GOROOT_BOOTSTRAP=/usr/local/go $ cd $HOME/go/src && ./make.bash $ export PATH=$PATH:$HOME/go/bin
- Install and initialize the gomobile package.
$ go get golang.org/x/mobile/cmd/gomobile $ gomobile init
Package initialization takes a decent amount of time.
Library development golang.- The usual golang package is being developed, no additional recommendations. Export methods will be available from android.
Compiling golang library to android * .aar library.Connecting the * .aar library to the application:First of all, transfer the * .aar library to the android development machine. Further depending on the IDE.
for Eclipse:
- Unpack * .aar library to a temporary directory
- In Eclipse, create a new project from the existing code, specifying a temporary directory from item 1
- Note that this project is an android library.
- Connect the jni directory and the classes.jar file to the Build Path of the new project
- Connect a new project as a library to an application project.
- Copy the contents of the jni folder to the libs folder of the application project.
- Copy the contents of the proguard.txt library into the proguard.txt of the application project (protecting the go * classes from obfuscation)
- With subsequent updates of the * .aar library, it is enough to update the files in the jni, libs folders and the classes.jar file
for Android Studio:
- Connect the ready * .aar library to the project using standard Android Studio tools. Unfortunately, I have no such experience, so I leave it as a homework.
Calling native library methods from android.- Before using the native library in the application, you need to perform its initialization. A suitable place for this is in the onCreate method:
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Go.init(getContext()); } }
- The methods of the golang library are available through the package, of the same name as those defined in golang. For example:
golang:
package radix … func Suggest(params string) string { … }
java:
suggestions = Radix.Suggest(params);
Result
After transferring the algorithm to the native library, the predictions began to work even faster than in the JVM - the result is given in less than 10 ms. The amount of garbage collection has also decreased, because some resources were transferred to the side of the native code.
The effect of the JNI implementation exceeded expectations. With such indicators, the keyboard and went to the release.
Conclusion
To speed up the android application with the help of golang is not a theory at all, but a real opportunity that is already used in market applications.
Of course, this complicates the development of applications, the zoo of tools is doubled. But this makes it possible to implement interesting solutions.
With the release of Go 1.5 it is expected that the integration of android-golang will become even more accessible, due to the gomobile toolkit, which reduces the entire process to 2 teams.