Reputation: 3997
I am new to Android Studio.
How can I add a few jar files in the External Libraries
below the < JDK > folder?
Upvotes: 255
Views: 424155
Reputation: 915
For build.gradle.kts use the following format. while the rest of the process is same as described in other answers which is to either add libs folder to your module(app) folder and use the following or add complete path to your files in the following
implementation (fileTree("libs") { include("*.jar") })
implementation (files("libs/opus.aar") )
Upvotes: 1
Reputation: 12572
Put your JAR in app/libs, and in app/build.gradle add in the dependencies
section:
implementation fileTree(dir: 'libs', include: ['*.jar'])
Upvotes: 112
Reputation: 5063
In Android Studio version 3.0 or more I have used bellow like :
Project
view in upper left cornerIn build.gradle add these lines
// Add this line if was not added before.
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/com.ibm.icu_3.4.4.1.jar')
Upvotes: 18
Reputation: 34461
Manually:
libs
folder in your project(Where build.gradle
is located). E.g. applibs
implementation files('libs/<name>.jar')
into build.gradle
. It is the same as UI Add as library
doUpvotes: 1
Reputation: 81
libs
folder under app folder and copy jar file into it.build.gradle
file:implementation fileTree(include: '*.jar', dir: 'libs')
Upvotes: 4
Reputation: 10661
A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.
STEPS
Now you can start using the library in your project.
Upvotes: 453
Reputation: 1066
Create "libs" folder in app directory copy your jar file in libs folder right click on your jar file in Android Studio and Add As library... Then open build.gradle and add this:
dependencies {
implementation files('libs/your jar file.jar')
}
Upvotes: 58
Reputation: 1799
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.
Upvotes: 2
Reputation: 21
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".
Upvotes: 2
Reputation: 1316
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:
Upvotes: 28
Reputation: 1236
The "official way" to add a jar into your android project as an external library, is to add the jar in dependencies { } section in build.gradle.
If you've done all of the above, and none of the above works, then there are two other possibilities:
package a.b.c;
should be matching to folder a > folder b > folder c.However, if you are going with cordova, here are some tips of adding external jars.
"build-extras.gradle" is the better way to manage your gradle file.
Here are the steps to manage extra settings in cordova-based android project:
//Other settings go here, e.g.: buildscript { ... } ext.postBuildExtras = { // you may have some other settings, e.g.: android { ... } dependencies { compile files('libs/abc.jar') } }
(More detailed steps here: Extending the cordova gradle file to include google services )
cordova build android
Upvotes: 1
Reputation: 4246
Add your jar file to the folder app/libs
. Then right click the jar file and click "add as library".
If there is no libs
folder you can create it. Click on the combo box that says "Android", and change it to "Project"
From here, you can right click on "apps" in the directory tree and go to "New" => "Directory"
Upvotes: 148
Reputation: 166
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'])
[...]
}
Upvotes: 4
Reputation: 6179
A simple way to add Jar file Android Studio Steps:
Upvotes: 4
Reputation: 1653
This is how you can add .jar
file in Android Studio 2.1.3.
Upvotes: 14
Reputation: 171
Example with Parse jar...
Add jars to libs folder from Project view … create lib folder if not exists
Copy all jars there...
Add libs to gradle.... in build.gradle file :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:percent:23.0.0'
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar’)
}
For add all jars of lib folder... change Parse-*.jar to *.jar
Upvotes: 15
Reputation: 691
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.
Upvotes: 4