![](/img/trans.png)
[英]How to use javascript functions written in external file, in react-native application?
[英]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
实际上,我有现有的 SDK,我想在 React Native 应用程序中使用 SDK。
对于 android
我尝试将 jar 文件添加到 /android/app/ 的 libs 文件夹中
将依赖项添加到文件 /android/app/build.gradle
实现 fileTree(include: ['*.jar'], dir: 'libs')
但是我不知道如何在我的 js 文件中使用这些 jar 文件。 如何创建 object 并调用方法?
主要问题是如何在我的 React Native 应用程序中使用外部 java 库?
你应该使用原生模块,它们是 JS 和原生之间的桥梁
https://reactnative.dev/docs/native-modules-android
例子
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();
}
}
}
在您的 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();
有用的问题以及从 java 发送 hashmao 以反应本机
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.