Golang for Android or IOS usage record

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

[TOC]

Environment

    • OSX 10.10.5
    • jdk1.8.0_92
    • Golang 1.7
    • Android SDK 25.1.7
    • andorid-ndk-r12b
    • Andorid Studio 1.5.1
    • Xcode 7.2.1
    • Swift 2.1.1

Type descriptions currently supported by Golang Bind

Https://godoc.org/golang.org/x/mobile/cmd/gobind

Installing Gomobile

go get -u -v golang.org/x/mobile

will report an error

package golang.org/x/mobile: no buildable Go source files in /Users/sinlov/go/src/golang.org/x/mobile

Ignore, this is because this is not a can build project, you need these libraries later, all down

If you encounter a similar problem, you can use the-D parameter to circumvent the failed build problem caused by the test code, but if it is not a directory that can be built, the go get command will attempt to build and will still warn you

    • Install Go Mobile
go get -v golang.org/x/mobile/cmd/gomobile

Then, turning over the wall, went on running

gomobile init

Initialization will wait a few minutes, look at the network speed, after the initialization can be formally used!

Gomobile Introduction

Gomobile allows Golang to be used on mobile devices

    • Native development of BIND dynamic Library mode
    • Build a mobile app directly
    • Install installs the generated app to a device or simulator
    • Clean empty Cache

Generally use bind method development, build mode or experimental

Using the Golang dynamic Connection library in mobile devices

    • Get ready

Gomobile bind view Help documentation

gomobile bind -help

When executing this command, note that go version gopath, which is the basis of go development, is not part of this article

A Golang project package with no errors, note the package name of the package, and use it later

The Golang pseudo-code for the following example ispackage.go

package [GoPackageName]func[GoFunction] (in tpye) type {}

Android use

gomobile bind -target=android [packageName]gomobile bind -target=android -o [output][packageName]

A file named after the package name is generated in the current directory .arr

By default, the output support platform is armeabi-v7a arm64-v8a x86 x86_64

After importing this file using Android Studio arr

Android uses a similar

import go.package.[GoPakcageName];private void (){  [GoPakcageName].[GoFunction]();}

IOS use

gomobilebind-target=ios[packageName]gomobilebind-target=ios-o [output][packageName]

will generate a corresponding [工程包名].framework bundle and drag the bundle to the IOS project

Use the same time

#import"hello/Hello.h"Go[PackageName][GoFunction](@ type);

If you want to use Swift

Swift automatically generates help documents

Https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

[package].Go[PackageName][GoFunction](type)

The meaning of using Golang

This is the easiest place to tear, the meaning of technology is to shut up and do not forget what others say

The Golang for mobile nature is to make go build use arm or x86 instruction set to compile, and mobile native development no area

You can use

gomobile bind-target=android/arm,android/386 [pakcageName]

To specify the corresponding output platform

    • The benefit is that the code can be shared, much more efficient than the development native
    • The downside is that go generates a local package larger than 2MB

In the future, the size of this so file is not a problem, and the current environment may be responsible for some OCD technology

When rumors, Bill Gates said memory only need 640K, this rumor is true and false don't know, only know now mobile phone random ram is 2g,rom start 16G

Use the Strip tool to reduce the size of the android so

Strip tool will remove debug and symbol information

The test can be reduced to the original 60% or so tools in the ANDROID-NDK directory

Similar

cd android-ndk/android-ndk-r12b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/arm-linux-androideabi/bin./strip [yourOSFile]

You need to back up before optimization and test whether you can use it after optimization

Golang operational efficiency problem, native bind efficiency problem

The reason for pulling out alone is that native bind certainly has an efficiency problem, and it's not controversial to see if your function is not efficient

Golang operation efficiency problem, Big Brother, language running efficiency certainly in programmer's hand, no scrap language, only not hard programmer

Related Article

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.