How to Import Create Module in Expo or React-native?

After doing ‘expo init’, I entered ‘expo eject’ and separated it.

I made a basic ‘jar’ file to test application to App.js using ‘jar’ file.

enter image description here

The implementation is simple.

CreateAJarFile.class in jar:

class CreateAJarFile {
    public static void main(String args[]) {
        System.out.println("We got test");
    }
}

bild.gradle(app):

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation files('libs/myJarFile.jar')

After applying Android, I made a module to write modules on ‘Apps.js’.

android/app/src/main/java/com/myjarfile

enter image description here

CreateAJarFile.java:

package com.myjarfile;

import android.widget.Toast;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.util.Map;
import java.util.HashMap;

public class CreateAJarFile extends ReactContextBaseJavaModule {

  private static final String DURATION_SHORT_KEY = "SHORT";
  private static final String DURATION_LONG_KEY = "LONG";

  public CreateAJarFile(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "CreateAJar";
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = new HashMap<>();
    constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
    constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
    return constants;
  }

  @ReactMethod
  public void show() {
    System.out.println("We got test");
  }

}

CreateAJarFilePackage.java:

package com.myjarfile;

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;

public class CreateAJarFilePackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();

        modules.add(new CreateAJarFile(reactContext));

        return modules;
    }

}

CreateAJar.js:

import { NativeModules } from "react-native";

const { CreateAJar } = NativeModules;

export default CreateAJar;

android/app/src/main/java/host/exp/exponent/MainApplication.java

import com.myjarfile.CreateAJarFilePackage;
...
public List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        // Add your own packages here!
        // TODO: add native modules!

        // Needed for `react-native link`
        // new MainReactPackage()
        new CreateAJarFilePackage());
  }

I made a module like this, added it, and tested it on App.js.

import CreateAJar from "./android/app/src/main/java/com/myjarfile/CreateAJar";

export default class App extends React.Component {
...
this.jarfunc = this.jarfunc.bind(this);
...
jarfunc() {
    CreateAJar.show();
  }
...
return (
<View style={styles.container}>
        <Text>Test</Text>
        <Text>{this.jarfunc}</Text>
      </View>

However, both the module and the function could not be read.

How do I apply the module I created to ‘App.js’?

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.