Using native module that built by gomobile on React Native

Yusuke Hatanaka
2 min readFeb 14, 2018

--

React Native — Native Modules

React Native has native module utilization package that is called the NativeModules.
See also the RN documents below:

Thus, RN can use packages that built by gomobile, this is little exciting!
Currently, I succeeded to run a simple package that built by gomobile on the RN for Android (iOS is not yet), indeed, there are a few obstacles in implementation and settings. So, I write them down here.

The process

  • Write a simple Go package
  • Initialize the react-native app
  • Setting up gradle files
  • Make Go package manipulation classes

Write a simple Go package

The first, I wrote a simple Go package in $GOPATH/github.com/hatajoe/hello/hello.go like below:

package helloimport "fmt"func Hello() {
fmt.Println("Hello, world")
}

Initialize the react-native app

Install react-native-cli and initialize app (gone is my project name):

% yarn global add react-native-cli
% react-native init gone

Setting up gradle files

The first warn is here. I faced the same problem this issue:

Don’t use gomobile from gradle plugin. My current workaround is building manually and importing manually and…

Note: Do not upgrade Gradle version that recommended by Android Studio like below, you won’t be able to build the project.

Building Android native package (.jar/.aar):

% cd $GOPATH/github.com/hatajoe/hello
% gomobile bind -x -v -target=android github.com/hatajoe/hello

Importing .jar and .aar to project (Android Studio):

File > New > New Module > import .JAR/.AAR Package

Add hello and hello-sources package as Module Dependency scoped Compile to dependency setting:

File > Project Structure > app > Dependencies

Make Go package manipulation classes

The second warn here. Package import pass had changed since Go 1.7 released.

Before:

import go.hello.Hello

After:

import hello.Hello

Congrats, We got over!
You can find the reference app here and Go package here.

Reference

Thank you for the great post! I could not be realized without this.

--

--