To include a *.jar file in your project, you can make use of the build.gradle file:
- If you wish to add a jar file to your project, simply create a libs folder inside the android/app directory and place your jar file there. Facebook has already handled the rest for you!
https://i.sstatic.net/ub6Yt.png
- If you need to add a jar file to a native module, insert the line compile fileTree(dir: "libs", include: ["*.jar"]) into the dependencies section of the build.gradle file for that specific module.
Example-1:
After placing okhttp-3.4.1.jar into the lib folder, I added the package name to the dependencies part like this:
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile 'com.facebook.react:react-native:0.19.+'
}
Example-2:
If another package from Maven needs to be included, such as fresco, it should be added within the dependencies block as shown below:
dependencies {
compile 'com.facebook.fresco:fresco:1.9.0'
}
This way Gradle will handle downloading and installing the necessary dependency library Fresco for me.
Typically, every Android project is configured with Maven repo settings in the top-level build.gradle file of the project. For instance:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Example-3:
(I have not personally tested this, but it should work) If an drawee-debug.aar file needs to be added to the project, follow the instructions from Example-1, then adjust the fileTree line accordingly:
compile fileTree(dir: "libs", include: ["*.jar", "*.aar"]) // "*.aar" is added
Example-4:
(alternative approach to Example-3) If adding a drawee-debug.aar file, also include it in the libs folder following Example-1, then make some adjustments as shown below:
dependencies {
compile (name:'drawee-debug', ext:'aar')
}
allprojects {
repositories {
...
flatDir {
dirs 'libs', './libs'
}
...
}
}
In this method, the libs directory is specified in allprojects, and the aar file is defined in the dependencies block, similar to other examples.
Note: Starting from Gradle v3.0.1, implementation is used instead of the compile keyword.
Credit: