DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Top 13 Mobile App Development Platforms You Need
  • Exploring the Salesforce Mobile SDK Using Android Studio
  • Java Vs. Kotlin: Which One Will Be the Best in 2019?
  • Implementing SOLID Principles in Android Development

Trending

  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Why Documentation Matters More Than You Think
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  • Performance Optimization Techniques for Snowflake on AWS
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Gomobile: Library Development for iOS/Android

Gomobile: Library Development for iOS/Android

Gomobile is a neat cross-platform development for iOS and Android. Check out great getting started tutorial for Gomobile.

By 
Vladislav Chernov user avatar
Vladislav Chernov
·
Mar. 15, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free


Cross-platform development of mobile applications was quite popular back then. This approach was used by most companies in the time of mobile branch establishment. The main reasons for using this approach were simple – lack of professionals in the market, slow development speed, and unreasonable cost. Unfortunately, in most cases this approach did not justify itself. But why not to give that approach the second chance? Technology took a big step forward and theoretically we can get a high-quality product. In this article, we’ll review in practice how to develop Library for iOS/Android in Golang and have a look at the problems and constraints faced in the development process.

Our main task is to develop SDK for log and crash collection from mobile applications. Here the SDK has to connect and work with Android and iOS platforms. At the same time, the library should interact with the main service – LogPacker that aggregates and analyze data.

We decided to use new opportunities of Golang for cross-platform library creation. First of all, our main application is written in Go and it was easier for us to use that lang and not to involve Java/Objective-C developers. Second of all, we saved development time and tried the old approach with improved features.

What is Gomobile?

Gomobile project provides developers with tools for code creation for Android and iOS mobile platforms.

Nowadays there are two ways for Go integration into mobile environment:

  1. Full development of Go-application without Ul.
  2. Java/Objective-C/Swift generation requiring Go.

This function is supported starting with Go 1.5. New Gomobile utility helps to compile Go to mobile applications or to create bindings in Java/Objective-C/Swift code.

For starters, we need to choose one of the integration ways. The first option is not suitable for us because of the wrong problem setting – we need a small library, not a separate application. Although this option is interesting and perspective thanks to Go speed and minimal resources consumption of mobile applications. We stopped our choice on the second option and generated Java/Objective-C/Swift code from Go.

Environment Setting

First, let’s prepare the environment for the development. We need Go 1.5 and higher (the higher – the better, Go-community updates Go Mobile regularly).

Then, let’s install Gomobile utility and Android SDK library. Let’s start with Gomobile installation:

go get golang.org/x/mobile/cmd/gomobile

Note: in OS X you need to have installed Xcode Command Line Tools. Then you need to initialize Gomobile, this can be done one time in any work directory.

gomobile init

Note: this command might take several minutes.

For building Java code you need Android SDK and installed Java (OpenJDK is enough).

Download ⇪  and unpack Android SDK to the home directory, for example, ~/android-SDK, and make the following command for API installation.

~/android-sdk/tools/android sdk

Then you need to set environment variable:

export ANDROID_HOME=$HOME"/android-sdk"

The environment for the library development and building is ready. Let’s move to the code building and see what constraints we will face.

Shared GO-code for Android and iOS

The same code can be used for the future compilation for Android and iOS. The building of such cross-platform code has its own constraints. As for now we can use only a certain set of data types. We need to take it into consideration when developing an application in Go. Let’s review in more detail the supported types:

  • int and float;
  • string and boolean;
  • byte[]. The current implementation doesn’t allow to use []byte as the function argument (https://golang.org/issues/12113);
  • function has to return only supported types, it may not return the result, it may return one or two types wherein the second type should be an error;
  • interfaces could be used if they are exported to files of any supported type;
  • struct type, only in case all fields meet the constraints.

So, if the type is not supported by Gomobile bind command, you’ll see the similar error:

panic: unsupported basic seqType: uint64

It’s obvious that the set of supported types is very limited, but this is enough for the SDK implementation.

Building and Import to Java/Objective-C/Swift

Gobind generates target language (Java, Objective-C or Swift) bindings for each exported symbol in a Go package. Unfortunately, Gomobile doesn’t work for Windows Phone, and we have to consider that at the stage of product planning.

As a rule gobind is not used directly, instead the code is generated automatically and is packed by `gomobile bind` command. More details you can find here  https://golang.org/x/mobile/cmd/gomobile.

Let’s consider several commands and special features of compilation process for every platform.

Let’s start with flag -target that defines a platform for a generation. Here is an example for Android:

gomobile bind --target=android .

This command will generate .aar file from the current code. To import that file to Android Studio is pretty simple:

  • File ➤New ➤New Module ➤Import .JAR or .AAR package
  • File ➤Project Structure ➤app ➤Dependencies ➤Add Module Dependency
  • Add import: import go.logpackermobilesdk.Logpackermobilesdk



Note: In Java the name of the package for import always starts with go.

A similar command is used for building Objective-C/Swift code.

gomobile bind --target=ios .

The folder .framework will be created in the current repository.

This works for both Objective-C and Swift. Transfer .framework folder to Xcode’s file browser and add import to project:

#import "Logpackermobilesdk/Logpackermobilesdk.h"

Note: Go allows you to build not only SDK but also to compile the application to apk/ipa file from main.go file only without native mobile Ul. No doubt, it’s a very interesting experiment of Go community.

Connected Packets Use

Gomobile bind automatically build getSomething(), setSomething().function. More over all exported functions will be available to the view.

As an example here is our library use in Android Studio:

import go.logpackermobilesdk.Logpackermobilesdk;
// ...
try {
    client = Logpackermobilesdk.NewClient("https://logpacker.mywebsite.com", "dev", android.os.Build.MODEL);

    msg = client.NewMessage();
    msg.setMessage("Crash is here!");
    // Use another optional setters for msg object

    client.Send(msg); // Send will return Cluster response
} catch (Exception e) {
    // Cannot connect to Cluster or validation error
}

The same library for Objective-C:

#import "ViewController.h"
#import "Logpackermobilesdk/Logpackermobilesdk.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    GoLogpackermobilesdkClient *client;
    NSError *error;
    GoLogpackermobilesdkNewClient(@"https://logpacker.mywebsite.com", @"dev", [[UIDevice currentDevice] systemVersion], &client, &error);
    GoLogpackermobilesdkMessage *msg;
    msg = client.newMessage;
    msg.message = @"Crash is here!";
    // Use another optional setters for msg object
    GoLogpackermobilesdkResult *result;
    [client send:(msg) ret0_:(&result) error:(&error)];
}

As we can see from the examples above, we got standard libraries at the end. Library configuration of the application is quite simple and familiar to developers.

Conclusion

Everybody understands that separate commands development for every mobile platform – is not a cheap and easy task. But it is essential for creation a high-quality product at this time. Our task we did in terms of cross-platform development and used all its advantages:

  • Minimal development resources.
  • High development speed.
  • Simple decision-support in the future.

The only minus we got was that we couldn’t build a library for Windows Phone, but we knew that from the beginning.

We hope that in the near future there will be a simple way of building full applications and SDK in Golang.

You can get acquainted with our developments by forking our repository.

Library mobile app Cross platform Android (robot) Command (computing) Objective C Android SDK Software development kit

Opinions expressed by DZone contributors are their own.

Related

  • Top 13 Mobile App Development Platforms You Need
  • Exploring the Salesforce Mobile SDK Using Android Studio
  • Java Vs. Kotlin: Which One Will Be the Best in 2019?
  • Implementing SOLID Principles in Android Development

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!