I am new to Android Studio. What I need to do is add a few jar files in the External Libraries
below the < JDK > folder.
If you dont see option "Add as Library", make sure you extract (unzip) your file so that you have mail.jar
and not mail.zip
.
Then right click your file, and you can see the option "Add as library".
Please provide the jar file location in build.gradle
implementation fileTree(dir: '<DirName>', include: ['*.jar'])
Example:
implementation fileTree(dir: 'C:\Downloads', include: ['*.jar'])
To add single jar file
implementation files('libs/foo.jar')
Note: compile is deprecated in latest gradle, hence use implementation instead.
The GUI based approach would be to add an additional module in your project.
One final piece of advice. Make sure that the JAR file you include is build with at most JDK 1.7. Many problems relating to error message "com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)" root straight to this :0.
If anyone is looking for another solution without actually copying the jar file(s) into the project directory, e.g. when using a jar in multiple projects:
Open build.gradle and add
def myJarFolder = 'C:/Path/To/My/Jars'
[...]
dependencies {
[...]
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
Note that of course you don't have to use the myJarFolder variable, I find it useful though. The path can also be relative, e.g. ../../Path/To/My/Jars.
Tested with AndroidStudio 3.0
Update: For Gradle Plugin > 3.0 use implementation instead of compile:
dependencies {
[...]
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
A simple way to add Jar file Android Studio Steps:
Step 1: Download any JAR file for your Project.
Step 2: Copy .jar file and past in libs folder.
Step 3: Click on File > Project Structure >Select app > Dependencies
Step 4:
Step 5:
Step 6: After click Ok button then we can see the Dependencies add like this way: