如何在 React-native for Android 中包含“.jar"文件?

我的英语很差.

我是一名前端开发人员.

I'm a FRONT-END developer.

现在我们需要一个可以使用蓝牙打印机的应用程序,使用 React-Native for Android 进行编码.

Now we need an App can use Bluetooth Printer, coding with React-Native for Android.

打印机制造商提供了一个 SDK 文件,扩展名为jar".

The Printer's manufacturer provided a SDK file,extension is 'jar'.

请告诉我如何在 React-Native 中使用这个 SDK?那么如何在 JSX 文件中导入呢?

Please tell me how to use this SDK in the React-Native? then how to import in the JSX files?

推荐答案

*.jar 添加到项目中是通过使用 build.gradle 文件完成的:

Adding *.jar to the Project is done by using build.gradle file:

  • 如果您想在项目中添加 jar 文件,Facebook 已经代您完成了!
    只需将 libs 文件夹与您的 jar 文件 一起添加到项目的 android/app 目录中,然后请享用!
  • If you want to add a jar file to the project, Facebook had already done on your behalf!
    Just add a libs folder into the android/app directory of the project with your jar file and enjoy!

  • 如果你想将 jar 文件添加到 native-module 然后添加 compile fileTree(dir: "libs", include: ["*.jar"]) 到原生模块的 build.gradle 文件的 dependencies 部分.
  • If you want to add a jar file to a native-module then add the line of compile fileTree(dir: "libs", include: ["*.jar"]) into the dependencies part of build.gradle file of the native-module.

示例 1:

在我将 okhttp-3.4.1.jar 文件添加到 lib 文件夹后,我还将该包名称添加到依赖项部分,如下所示:

After I added okhttp-3.4.1.jar file into the lib folder, I also add that package name to the dependencies part as the following:

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile 'com.facebook.react:react-native:0.19.+'
}

示例 2:

如果我需要另一个包 - 可以在 Maven 存储库中找到 - 我必须添加到依赖项块中,如下所示(例如,我想添加 fresco):

If I need another package -that is found in Maven repo- I have to add into dependencies block as following (for instance I wanna add fresco):

dependencies {
    compile 'com.facebook.fresco:fresco:1.9.0'
}

然后Gradle会找到并安装依赖库Fresco我.

Then Gradle will find and install dependency library Fresco for me.

通常,每个 Android 项目都已经有 Maven 存储库.build.gradle 文件中的配置,该文件位于项目文件夹的顶部.例如:

Usually, every Android project has already Maven repo. configurations in build.gradle file which is found in top of folder of the project. For example:

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

示例 3:

(我从未尝试过,但它应该可以工作)
如果我有一个 drawee-debug.aar 文件,我可以按照示例 1 中的说明将其放入 lib 文件夹中,然后将其添加到我的项目中,然后我必须更改 fileTree 行如下:

(I have never tried this however it should be worked)
If I have an drawee-debug.aar file I can add it into my project by putting it into lib folder as directed on Example-1 then I have to change fileTree line as following:

compile fileTree(dir: "libs", include: ["*.jar", "*.aar"])  // "*.aar" is added

示例 4:

(示例 3 的替代方式)
如果我有一个 drawee-debug.aar 文件,我也可以按照示例 1 的指示将其放入 lib 文件夹中,然后将其添加到我的项目中,然后我必须更改并添加如下几行:

(alternate way of Example-3)
If I have an drawee-debug.aar file also I can add it into my project by putting it into lib folder as directed on Example-1 then I have to change and add some lines as following:

dependencies {
    compile (name:'drawee-debug', ext:'aar')
}

allprojects {
    repositories {
        ...
        flatDir {
            dirs 'libs', './libs'
        }
        ...
    }
}

这样,libs目录定义在allprojects文件夹和dependencies块中指定的aar文件,像其他例子一样.

In this way, libs directory is defined in allprojects folder and aar file specified in dependencies block, like othe examples.

注意 Gradle v3.0.1 之后使用implementation 代替compile 关键字.

Note that after Gradle v3.0.1 implementation is used instead compile key word.

荣誉:https://stackoverflow.com/a/37092426/3765109

相关文章