12

My English is poor.

I'm a FRONT-END developer.

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

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

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

assert
  • 125
  • 1
  • 1
  • 6

2 Answers2

24

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

  • 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!

enter image description here

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

Example-1:

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.+'
}

Example-2:

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'
}

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

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"
        }
    }
}

Example-3:

(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

Example-4:

(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'
        }
        ...
    }
}

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

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

Kudos: https://stackoverflow.com/a/37092426/3765109

efkan
  • 12,991
  • 6
  • 73
  • 106
  • Where did you come up with `com.squareup.okhttp3:okhttp:3.4.1`? – Tyler Miller Apr 19 '18 at 16:46
  • I guess you asked because okhttp:3.4.1 is already in jar folder. If so you're right it seems wrong. I'll edit the answer. – efkan Apr 19 '18 at 16:57
  • Nope! I should note that my java skills are rusty. I asked purely because I'm trying to find the right string format to compile some libs that I am including. If possible, can you update the example to use that `gson-2.2.2.jar`? – Tyler Miller Apr 19 '18 at 17:01
  • Actually I have to change and have to expand the answer. Let me change in a couple of minutes.. – efkan Apr 19 '18 at 17:08
  • Thank you for quick replies on an old answer! – Tyler Miller Apr 19 '18 at 17:10
  • I guess `Example-2` explains what you asked. Isn't it? – efkan Apr 19 '18 at 17:38
  • Example 2 solves my issue. The note at the end also also saved my butt. Nice answer! – Tyler Miller Apr 19 '18 at 17:45
  • @efkan do we need to do anything else to use the functionality? – androider Mar 31 '20 at 16:54
  • how can i import the module in my react natice components with NativeModules, how can i find the name of the library to import after adding it with implementation in build.gradle – Zero0 Nov 03 '22 at 14:06
1

To integrate jar file to android see here.

In order to access the library functionality you need to create a react-native module for the functionalities which you want to access through javascript. For more info refer this

Community
  • 1
  • 1
Jickson
  • 5,133
  • 2
  • 27
  • 38