简体   繁体   中英

How can I add the external jar file into react native application and use as a plugin into the javascript file for both android and iOS

Actually, I have existing SDKs and I wanted to use that SDK in the react native app.

For android

  1. I tried adding the jar file into the libs folder of /android/app/

  2. Added dependencies into file /android/app/build.gradle

    implementation fileTree(include: ['*.jar'], dir: 'libs')

But I did not get how can I use these jar files in my js file. How can I create the object and call the methods?

The main concern is how can I use the external java libraries in my react native app?

You should use native modules them are bridge between JS and native

https://reactnative.dev/docs/native-modules-android

Example

public class DummyModule extends ReactContextBaseJavaModule {
MyDummyClass dummy // this context

public DummyModule(final ReactApplicationContext reactContext){
super(reactContext);
}

@Override
    // getName is required to define the name of the module represented in
    // JavaScript
    public String getName() {
        return "DummyModule";
    }
@ReactMethod
    public void startMyClass() {
       this.dummy = new MyDummyClass();
    }

@ReactMethod
    public void fooActionClass() {
       if(this.dummy != null){
           this.dummy.fooAction();
       }
    }
}

In your javascript code

import { NativeModules } from 'react-native';
const dummyModule = NativeModules.DummyModule;

dummyModule.startMyClass();
// Make sure that u call the action when the class is instanciated.
dummyModule.fooActionClass();

Usefull question as well Sending hashmao from java to react native

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM