Skip to main content
Android native modules allow you to write Java or Kotlin code that can be called from your React Native JavaScript code.

Basic Module Structure

An Android native module consists of:
  1. A module class extending ReactContextBaseJavaModule
  2. Methods annotated with @ReactMethod
  3. A ReactPackage to register the module

Creating Your First Module

Module Methods

Method Annotations

All methods exposed to JavaScript must use the @ReactMethod annotation:

Supported Parameter Types

Native modules automatically convert between JavaScript and Java/Kotlin types:

Using Callbacks

From JavaScript:

Using Promises

Promises provide a cleaner API for async operations:
From JavaScript:

Working with Complex Types

Reading Arrays

Reading Maps

Creating Return Values

Accessing Android Context

The ReactApplicationContext provides access to Android system services:

Constants Export

Expose constants to JavaScript that are available immediately without async calls:
From JavaScript:

Lifecycle Methods

Module Initialization

Module Cleanup

Threading

Running on UI Thread

Example: Image Loader Module

Here’s a real-world example based on React Native’s source code (packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt:78-118):

Best Practices

  • Always handle errors and reject promises appropriately
  • Use meaningful error codes for promise rejections
  • Validate input parameters before processing
  • Clean up resources in invalidate()
  • Use promises for single async operations
  • Document your module’s API
  • Consider thread safety when accessing shared resources