📜 ⬆️ ⬇️

Accelerate the Android application with Golang

image

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:

Golang development environment:

Library development golang.

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:

for Android Studio:

Calling native library methods from android.

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.

Source: https://habr.com/ru/post/260609/


All Articles