简体   繁体   English

如何将外部 jar 文件添加到本机应用程序中,并将其用作 javascript 文件中的插件,用于 android 和 iOS

[英]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

  1. I tried adding the jar file into the libs folder of /android/app/我尝试将 jar 文件添加到 /android/app/ 的 libs 文件夹中

  2. 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.

相关问题 如何在 react-native 应用程序中使用在外部文件中编写的 javascript 函数? - How to use javascript functions written in external file, in react-native application? 如何在反应应用程序中添加外部js文件? - How to add external js file in react application? 如何在iOS本机反应中移植iOS索引文件 - How do I port an iOS index file in react native to android 如何将外部 javascript 文件添加到反应组件中 - How to add an external javascript file into a react component 如何从iOS UI自动化JavaScript对外部jar文件进行API调用 - How can make API call to an external jar file from iOS UI Automation JavaScript 如何在 React Native 中检查 ios 和 android 的推送通知权限? - How can i check push notification permission for both ios and android in react native? 如何在具有 twig 元素的外部文件中使用 javascript 代码? - How can I use javascript code in an external file with twig elements? 如何在 javascript 中运行 jar 文件? - How can i run a jar file in javascript? 如何修改Android和iOS项目,以使条目文件在React Native中成为常见的js文件? - How do I modify android & iOS project to make the entry file as a common js file in react native? 如何在 React 中加载和运行外部 Javascript 代码,这些代码在应用程序中而不是在导入的文件中定义? - How do I load and run external Javascript code in React that have their definitions in the application and not in the imported file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM