React Native: Creating a native module
Learn how to build a native Android module for React Native without third-party libraries or overly complex implementations

Editorial translation by Impulso.
Hey, everyone! How’s it going? Today I’m going to write about building native modules for React Native. The idea came to me after I had to build a module to integrate with a payment gateway’s SDK. In this post, we’ll build something simpler. It’ll still give you a starting point if you need to build something more complex or just want to learn something new.
Tip
To get the most out of this tutorial, it helps to have some experience with native Android development. If you’re new to it, I recommend reading up on Activities and their lifecycle first, then coming back to this article.
Step 1
I’ll assume you’re already familiar with React Native, so I’ll skip the project setup. If you’re unsure how to do that, take a look at this article.
STEP 2
Now that we’ve created our project, the fun can begin! Open the android folder in Android Studio. Bye-bye, RAM.
Let’s create our Activity. In this example, it’ll be a screen that shows the React Native logo and a button labeled “voltar ao RN” (“back to RN”), but you could build anything here. Let your imagination run wild!
Create an Activity called HomeActivity, then add the following code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HomeActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btn = (Button)findViewById(R.id.button);
Looking at our code, you can see that we have a class that extends AppCompatActivity. In it, we implement the onCreate method, which loads the XML layout and handles any other necessary initialization. We also have an anonymous method that listens for clicks on our button. All it does is finish the Activity.
Step 3
Next, let’s add our layout code. The layout consists of a component that arranges the image and the button in a horizontal row. Add this code to the activity_home.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
android:padding="16dp"
android:orientation="vertical"
android:fillViewport="true">
<ImageView
android:layout_gravity="center"
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars[5]" />
Step 4
Now let’s create the module that will launch our Activity. There are 3 things to keep in mind:
First, every module has to extend ReactContextBaseJavaModule. That brings us to the second point: you need to implement the getName method and return the name of your module. That’s the name you’ll use to call it from React Native.
The third point is to create the method that will launch our Activity. Notice that the method has the annotation @ReactMethod. This is required to expose the method to JavaScript.
import android.app.Activity;
import android.content.Intent;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import javax.annotation.Nonnull;
public class HomeModule extends ReactContextBaseJavaModule {
public HomeModule(@Nonnull ReactApplicationContext reactContext) {
super(reactContext);
}
@Nonnull
@Override
public String getName() {
return "Home";
}
Step 6
Next, we need to register our module by creating a new class that extends ReactPackage. In the createNativeModules method, add our module and return the list of modules, as shown below:
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
public class HomeReactPackage implements ReactPackage {
@Nonnull
@Override
public List<NativeModule> createNativeModules(@Nonnull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new HomeModule(reactContext));
return modules;
}
Step 7
Finally, let’s add our HomeReactPackage to the list of packages in MainApplication. This is similar to what happens when we install a third-party library and need to run react-native link. Just add the highlighted line manually.
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new HomeReactPackage() // adicione essa linha
);
}
That’s it for the native setup. Next, let’s open our project in VSCode (I Love VsCode S2).
Our App.js file will look like this:
import React, { Component } from "react";
import { Text, View, StyleSheet, TouchableOpacity, NativeModules } from 'react-native'
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
onPress={() => NativeModules.Home.navigateToHome()}
>
<Text style={styles.textColor}>Chamar Módulo Nativo</Text>
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
width: '80%',
height: 40,
backgroundColor: 'green',
justifyContent: 'center',
alignItems: 'center'
},
Notice that we use React Native’s NativeModules to call our native module. The following code snippet calls the method we created in our module:
NativeModules.Home.navigateToHome()
Now just run the app and enjoy! You should see the following screen.

react-native
Conclusion
In this article, we built a simple native Android module. It can serve as a starting point for more complex tasks, such as integrating a payment gateway SDK. That brings us to the end of the tutorial.
Thanks for reading this far. If you enjoyed the article, connect with me on LinkedIn and check out my other articles on Medium.
I’d also like to thank Impulso for giving me a place to share this!


