[英]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.实际上,我有现有的 SDK,我想在 React Native 应用程序中使用 SDK。
For android对于 android
I tried adding the jar file into the libs folder of /android/app/我尝试将 jar 文件添加到 /android/app/ 的 libs 文件夹中
Added dependencies into file /android/app/build.gradle将依赖项添加到文件 /android/app/build.gradle
implementation fileTree(include: ['*.jar'], dir: 'libs')实现 fileTree(include: ['*.jar'], dir: 'libs')
But I did not get how can I use these jar files in my js file.但是我不知道如何在我的 js 文件中使用这些 jar 文件。 How can I create the object and call the methods?如何创建 object 并调用方法?
The main concern is how can I use the external java libraries in my react native app?主要问题是如何在我的 React Native 应用程序中使用外部 java 库?
You should use native modules them are bridge between JS and native你应该使用原生模块,它们是 JS 和原生之间的桥梁
https://reactnative.dev/docs/native-modules-android 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在您的 javascript 代码中
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有用的问题以及从 java 发送 hashmao 以反应本机
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.