Main.main on Android

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Main.main on Android

Main.main is a Go package ' s main function. This article'll explain you how it's being invoked on Android.

android ' s primary language have been Java and each application was supposed load D Alvik VM and talk to system libraries through Dalvik. For this historical reason, Android operating system have never stabilized its system level libraries. The system libraries is always a moving target and backward compatibility have been handled at the SDK layer. This model is still true as of today. Even though Android supports native applications, they is being loaded by Dalvik or ART and talk through ABI stable (in a Similar fashion to SDK) rather than making any direct system calls.

With Go 1.4, a main package became build-able to a dalvik-or art-loadable so file. So it became possible to generate Android APKs from a Go packaged by generating a boilerplate Android application that load s the generated shared library. As of today, gomobile build subcommand does this cumbersome work for your if you set-target=android.

The runtime story is straightforward. Once the shared library is loaded, we were looking for the exported Main.main symbol by dlsyming and invoke it. At this point we already has go runtime Initialized-each so file contains it own Go runtime.

In order to target Android, we is using the Android NDK ' s toolchain and the system library headers provided in the NDK Distri Bution. Due to the limitation of going through ART, we can ' t simply cross compile to a binary but need to use The-buildmode=c-sha Red mode to output everything into a shared library.

In order to explain how this mechanism work, I'll reverse engineer an APK that have been built by the Gomobile tool.

$ gomobile build-target=android Golang.org/x/mobile/example/basic

It'll generate basic.apk which is a zip archive. Unzip to see the contents.

$ unzip basic.apk
Archive: basic.apk
Extracting:AndroidManifest.xml
Extracting:classes.dex
Extracting:lib/armeabi/libbasic.so
Extracting:meta-inf/manifest. Mf
Extracting:meta-inf/cert. SF
Extracting:meta-inf/cert. Rsa

Classes.dex contains a subclass of nativeactivity that does the basic job of loading libbasic.so during runtime when the Appli cation is starting. Androidmanifest.xml is the bare minimum manifest, which has a org.golang.app.GoNativeActivity as its launcher activity. Libbasic.so contains runtime, the main package and all of its dependencies.

System.loadlibrary ("basic") loads the shared library and invokes Anativeactivity_oncreate. Anativeactivity_oncreate is responsible to find the Go package's main function by looking for main.main.

$ gobjdump-t lib/armeabi/libbasic.so | grep main.main
001bff04 l O. Rodata 00000004 main.main.func1.f
00072208 l F. Text 000003BC main.main.func1
00071d14 g F. Text 00000034 Main.main

OnCreate dlsyms Main.main, calls it and the application code written in Go runs. Since Anativeactivity_oncreate is called each time the activity is recreated, we're ensuring that Main.main won ' t being call Ed again on the later OnCreate events.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.