JAR libs are not added to the apk file - android

I've moved my project from ant build to gradle build, but I can't get it to use the jars from my libs directory.
I have created app/libs directory
I have moved there my jars
I have added compile fileTree(dir: 'libs', include: '*.jar') to the app/build.gradle
Unfortunately, it didn't work, the classes from those jars are not available during runtime.
I have ran the gradle from command line with debug option, to check the whole output, and it's clear to me that they are not processed.
I know I'm doing something wrong, but I have no idea what. I thought that in my other projects I have used it multiple times, but after going through every one of them, I can see that I have never used jars loaded from libs directory.
What am I missing?

I assume you are using Android Studio and you have a .jar file which you want to include in your project : (get rid of any changes you made in that matter- I just opened a new empty project:)
right click on your app name in project structure column and click on 'open Module settings'
go to dependency tab and click on '+' and click on 'Jar dependency' and navigate to your jar library (you can also look for a online library dependency from 'library dependency' if you do not have local library)
that's it , it will add your jar library to your project (automatically will add it to the app/build.gradle)
Edited:
if you are not using Gui then add this to your Gradle under dependencies
compile fileTree(include: '*.jar', dir: 'libs')
and make sure you have libs directory on root(with your jar in it)
finally run a clean build

First you are missing the brackets in this line fileTree(dir: 'libs', include: '*.jar').
You need to write like this
compile fileTree(dir: 'libs', include: ['*.jar'])
Then make sure that you have added your .jar files to Project Structure->app->dependencies->Plus button -> add file dependency.

Related

How do I know that I've imported a jar properly in AndroidStudio project

I'm trying to use a third party library in AndroidStudio and am having some problems, it is the JSch library.
What I did was download the ".jar file
On my app within Module Settings I added it
And then selecting the app module again I added it within the dependencies
Yet when I try to code it into my files through "import com." it does not show.
I am too new at this to know if I added it incorrectly or if it has a problem within the ".jar" file.
Does this ring a bell with anyone who might know what I am doing wrong?
Just follow this steps:
1.- Put the jar into the libs folder
2.- Right click it and select "Add as library"
3.- Check that your build.gradle file contains:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You'll provably need to do a clean build
Add library to yours app dependencies:
dependencies {
// Other libraries...
compile group: 'jsch', name: 'jsch', version: '0.1.29'
}
And synchronize your gradle.

libs folder doesn't work in android studio

I am using the mac version of android studio 1.2 Beta 2, built on April 7, 2015. Gradle version 2.2.1 and Gradle plug-in version 1.1.0.
After I create a new project, The following code exist by default in my app/build.gradle file.
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
It means that any jar file I add to the app/libs directory should be loaded to my project, right? However, when I add my Parse-1.9.0.jar to the app/libs, it is not loaded. I failed to use the code from the API in MainActivity.java.
I got it working by right click Parse-1.9.0.jar -> Add As Library... The app/build.gradle became:
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/Parse-1.9.0.jar')
}
I am confused of why. Shouldn't compile fileTree() include all the jar files? Why do I need to do the additional step?
The Parse-1.9.0.jar file I downloaded is from: https://parse.com/apps/quickstart#parse_data/mobile/android/native/existing
Thanks
(1) Select the Packages view for the project
(2) Drag your library (jar) file into the "libs" folder in Packages view
(3) Right-click the newly added jar file - and select "Add as library" in the dialogue - Then click OK in next dialogue (Gradel Sync will start)
For every change you make to your libs folder (add, remove .jar) you need to sync your gradle files. Jar file libraries are not discovered automatically by AS - as it was with Eclipse.
I don't know 'why' is fileTree option not working. But surprisingly, wildcard approach works.
dependencies {
compile files('libs/*.jar')
}
implementation fileTree(dir: "libs", include: ["*.jar"])

Adding Parse-1.8.0 to Android Studio 1.0.1 (Or any .zip file)

So there seems to be a few threads that attempt to explain how to add .zip files to android studio but I am making no progress. I am migrating from eclipse so perhaps that is why I am so incompetent in figuring out how to perform this task. Can someone please explain how to add external libraries to Android Studio v1.0.1? In eclipse it was simply importing jar/zip and done.
I was in the same situation, trying to integrate Parse 1.8 with Android Studio 1.0.2.
On Parse's instruction page, it simply tells you to import the library into Android studio, which isn't too detailed. Here is how I solved this problem.
Choose to import from a "Non Android Studio Project", right when Android Studio starts.
When it asks you to choose the project, give the path, on Windows e.g.C:/path/to/parsesdk/. On *nix systems, it should be to where you have extracted to it, /home/user/path/to/parsesdk.
After you choose the path, Android Studio will import the project accordingly.
Click on the Application root folder (the top most folder in the folder hierarchy to the left), right click > New > Package > and add it under the src folder, name is libs.
Copy paste the jar to the libs folder (I only copied the jar file, as I didn't need the other extra material)
Right click on the jar, there should be an option labeled as "Add as library" towards the bottom, click on that.
Android Studio will automatically add the following to the build.gradle file.
compile files('src/libs/Parse-1.8.0.jar')
Once the above step is completed, click on File > Project Structure.
On the left, there should be a section called "Modules", click on the "Dependencies" tab on the top.
Click the green "+" sign > Module Dependency
Select the module from the list.
Last thing, in the build.gradle file for the "ParseStarterProject" module, if there is a red line under the classpath, change it to the following
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
After completing this step, I was able to successfully build the app.
Hope this helps :)
If you are using gradle with android studio (which is the preferred way now) you can include jars in a folder using this code snipet from my build.gradle.
buildscript {
repositories {
flatDir { dirs 'c:\\path\\to\\folder' }
mavenCentral()
}
}
Or by including a single file in depencies like below.
dependencies {
compile fileTree(dir: 'a-folder-in-root-of-project', include: 'a_jar.jar')
}
This is the only way it worked for me :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Parse
compile 'com.parse.bolts:bolts-android:1.+'
compile files('libs/Parse-1.9.2/Parse-1.9.2.jar')
compile files('libs/Parse-1.9.2/ParseCrashReporting-1.9.2.jar')
compile files('libs/Parse-1.9.2/ParseFacebookUtilsV3-1.9.2.jar')
compile files('libs/Parse-1.9.2/ParseFacebookUtilsV4-1.9.2.jar')
compile files('libs/Parse-1.9.2/bolts-android-1.2.0-javadoc.jar')
compile files('libs/Parse-1.9.2/bolts-android-1.2.0.jar')
}
OR
//Parse
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs/Parse-1.9.2', include: 'Parse-*.jar')
compile fileTree(dir: 'libs/Parse-1.9.2', include: 'ParseCrashReporting-*.jar')

How to add a jar in External Libraries in Android Studio?

I am new to Android Studio.
How can I add a few jar files in the External Libraries below the < JDK > folder?
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
First switch your folder structure from Android to Project.
Now search for the libs folder inside the app folder.
Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].
Now you can start using the library in your project.
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"
Put your JAR in app/libs, and in app/build.gradle add in the dependencies section:
implementation fileTree(dir: 'libs', include: ['*.jar'])
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')
}
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:
In Android Studio version 3.0 or more I have used bellow like :
Create libs to the app directory if not exist folder like
Set Project view in upper left corner
Go to project
Go to app
click on apps->New->Directory
name the folder libs
paste the jar to the libs folder
directory will look like bellow image
In 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')
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
This is how you can add .jar file in Android Studio 2.1.3.
Copy the .jar file
paste the file in Libs folder and then right click on .jar file and press Add as library
open build.gradle
add lines under dependencies as shown in screenshot
Now press play button and you are done adding .jar file
The GUI based approach would be to add an additional module in your project.
From the File menu select Project Structure and click on the green
plus icon on the top left.
The new Module dialog pops
From the phone and tablet application group select the "Import JAR or AAR package" option and click next.
Follow the steps to create a new module that contains your JAR file.
Click on the entry that corresponds to your main project and select the dependencies tab.
Add a dependency to the module that you created in step 4.
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.
A simple way to add Jar file Android Studio Steps:
Copy and paste your jar file to libs folder of your project.
Click File from File menu -> Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X).
Select Modules at the left panel -> Dependencies tab.
Add... → Project Library → Attach Jar.
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'])
[...]
}
Create libs folder under app folder and copy jar file into it.
Add following line to dependcies in app build.gradle file:
implementation fileTree(include: '*.jar', dir: 'libs')
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 "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:
If Android Studio or some other IDE doesn't give red underlined errors in editor but runs in error in the Java Compiler, CHECK YOUR JAR.
Your jar may not have a proper Manifest included therefore the compiler doesn't know what the jar can provide/don't know it's package name
Maybe it's folder structure is messed up. The folder structure doesn't match with its package name. package a.b.c; should be matching to folder a > folder b > folder c.
The package name has some conflict. (Trust me, this happened to me and it took hours to figure it out.)
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:
Adding your jar into build-extras.gradle:
//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 )
In terminal, do:
cordova build android
Manually:
Add libs folder in your project(Where build.gradle is located). E.g. app
Move .jar into libs
Add implementation files('libs/<name>.jar') into build.gradle. It is the same as UI Add as library do

Android Studio: Add jar as library?

I'm trying to use the new Android Studio but I can't seem to get it working correctly.
I'm using the Gson library to serialize/deserialize JSON-objects. But the library somehow isn't included in the build.
I had created a new project with just a MainActivity.
Copied gson-2.2.3.jar in the /libs folder and added it as a library dependancy(right click->Add as library). This includes the jar in android studio so it can be referenced from the source files.
When I try to run the project it cannot compile so I added:
compile files('libs/gson-2.2.3.jar')
to the dependencies in de .gradle file. After that it compiles correctly but when running the application I get a ClassDefNotFoundException.
Does anyone know what I'm doing wrong?
I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took:
Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files)
Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar')) in Android Studio 3.0+
Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system
After I did the above four, it started working fine. I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.
[Edit - added the build.gradle step which is also necessary as others have pointed out]
Here are the instructions for adding a local jar file as a library to a module:
Create a 'libs' folder in the top level of the module directory (the same directory that contains the 'src' directory)
In the build.gradle file add the following so that your dependencies closure has:
dependencies {
// ... other dependencies
compile files('libs/<your jar's name here>')
}
Android Studio should have already setup a gradlew wrapper. From the command line, navigate to the top level of your project (the directory that has a gradlew file).
Run ./gradlew assemble. This should compile the project with the library. You may need to fix errors in your build.gradle file as necessary.
In order to have Android Studio recognize the local jar files as libraries for support while coding in the IDE, you need to take a few more steps:
4.1. Right click on the module in the left hand panel and choose Open Library Settings.
4.2. On the left panel of the dialog, choose Libraries.
4.3. Click the + sign above the panel second from the left -> Java
4.4. Select your local jar and add it to the project.
You may need to run the above ./gradlew command one more time
In the project right click
-> new -> module
-> import jar/AAR package
-> import select the jar file to import
-> click ok -> done
Follow the screenshots below:
1:
2:
3:
You will see this:
In Android Stuido, I like use Gradle to manage Gson lib.
Add below dependency in your build.gradle file.
repositories {mavenCentral()}
dependencies {compile 'com.google.code.gson:gson:2.2.4'}
Everything is OK.
You can also see this post.
The best way to integrate third party library in Android studio
IIRC, simply using "Add as library" isn't enough for it to compile with the project.
Check Intellij's help about adding libraries to a project
The part that should interest you the most is this:
(In File > Project Structure) Open the module settings and select the Dependencies tab.
On the Dependencies tab, click add and select Library.
In the Choose Libraries dialog, select one or more libraries and click Add Selected.
If the library doesn't show up in the dialog, add it in the Libraries settings, right below Modules.
You shouldn't need to add compile files() anymore, and the library should be properly added to your project.
All these solutions are outdated. It's really easy now in Android Studio:
File > New Module...
The next screen looks weird, like you are selecting some widget or something but keep it
on the first picture and below scroll and find "Import JAR or .AAR Package"
Then take Project Structure from File menu.Select app from the opened window then select dependencies ,then press green plus button ,select module dependency then select module you imported then press OK
Easy steps to add external library in Android Studio
If you are in Android View in project explorer, change it to Project view as below
Right click the desired module where you would like to add the external library, then select New > Directroy and name it as
'libs'
Now copy the blah_blah.jar into the 'libs' folder
Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile
files('libs/blah_blah.jar') and sync the gradle. And you are done
Please Note : If you are using 3rd party libraries then it is better to use dependencies where Gradle script
automatically downloads the JAR and the dependency JAR when gradle
script run.
Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'
Read more about Gradle Dependency Mangement
'compile files...' used to work for me, but not any more. after much pain, I found that using this instead works:
compile fileTree(dir: 'libs', include: '*.jar')
I have no idea why that made a difference, but, at least the damn thing is working now.
Download Library file from website
Copy from windows explore
Paste to lib folder from Project Explorer
Ctrl+Alt+Shift+S open Project Structure
Select Dependencies Tab, add the file by using +
Tool bar Sync project with gradle file by using button
That solved my problem. Try, if anyone want more details let me know.
I made it work by just adding one line to build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
implementation 'com.google.code.gson:gson:2.3.1' ----------> I added this one
}
Do not forget to click Sync now in the top right corner.
I'm using Android Studio 1.0.1.
I found Dependency Manager of Android Studio quite handy and powerful for managing 3rd party dependencies (like gson mentioned here). Providing step by step guide which worked for me (NOTE: These steps are tested for Android Studio 1.6 and onward versions on Windows platform).
Step-1:
Goto "Build > Edit Libraries and Dependencies..." it would open up the dialog "Project Structure"
Step-2:
Select "app" and then select "Dependencies" tab. Then select "Add > 1 Library dependency"
Step-3:
"Choose Library Dependency" dialog would be shown, specify "gson" in search and press the "search button"
Step-4:
The desired dependency would be shown in search list, select com.google.code.gson:gson:2.7 (this is the latest version at the time when I wrote the answer), press OK
Press OK on "Project Structure" dialog. Gradle would update your build scripts accordingly.
Hope this would help :)
1. Put the jar (in my case, gson-2.2.4.jar) into the libs folder.
2. Ensure that compile files (libs/gson-2.2.4.jar) is in your build.gradle file.
3. Now Click on the "Sync Project with Gradle files"(Left to AVD manager Button on the topbar).
After I did the above three, it started working fine.
Download & Copy Your .jar file in libs folder then adding these line to build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.3.1'
}
Do not forget to click "Sync now"
You can do this with two options.
first simple way.
Copy the .jar file to clipboard then add it to libs folder. To see libs folder in the project, choose the project from combobox above the folders.
then right click on the .jar file and click add as a library then choose a module then ok.
You can see the .jar file in build.gradle file within dependencies block.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:21.0.3'
implementation project(':okhttp-2.0.0')
implementation 'com.google.code.gson:gson:2.3.1'
}
Second way is that: We can add a .jar file to a module by importing this .jar file as a .jar module then add this module to any module we want.
import module ---> choose your .jar file --> than import as a .jar --
Then CTRL+ALT+SHIFT+S --> project sturure -->choose the module you want ato add a jar -->Dependencendies --> Module Dependency. build.gradle of the module will updated automatically.
Ive done above 3 steps and its work charm for me.
(I am using Android Studio 2.1.2)
Step 1
Add your jar package name (as an example compile 'com.squareup.okhttp3:okhttp:3.3.1') into gradle build script under build.gradle(Module:app).
Step 2:
Right click on app folder -> New -> Module
Step 3: Click Import JAR/.AAR Package Then browse your package. as an example: OkHttp.jar
With Android Studio 3+:
You should just be able to simply copy the jar file to the libs folder right under the app folder.
... myproject\app\libs\myfile.jar
Then select Project Files from the drop-down on the Projects window, right click on the project, select Synchronize to see the file in Project Files. It will automatically add the dependencies in the gradle file (Module:app).
dependencies {
...
implementation files('libs/myfile.jar')
Here is another solution:
Go to the Project Files view (select Project Files from the dropdown).
Select New... Directory, create a folder named libs right under app.
Open up File Explorer, copy and paste your jar file into the libs folder.
In Android Studio, right click on the jar file, and select Add as a Library... from the popup menu.
You should see the file listed in the dependencies list in the gradle file:
dependencies {
...
implementation files('libs/myfile.jar')
}
Open up your java file, and add the import statement there:
import com.xxx.xxx;
Put the .jar files in libs folder of the Android project.
Then add this line of code in the app's gradle file:
compile fileTree(dir: 'libs', include: ['*.jar'])
For Android gradle plugin 3.0 and later, it is better to use this instead:
implementation fileTree(dir: 'libs', include: ['*.jar'])
Unlike Eclipse we don't need to download jar and put it in /libs folder. Gradle handles these things we only need to add Gradle dependencies, Gradle downloads it and puts in gradle cache.
We need to add dependencies as:
dependencies {implementation 'com.google.code.gson:gson:2.2.4'}
thats it
However we can also download jar & add that as library but the best practice is to add Gradle dependencies.
menu File -> project struct -> module select "app" -> dependencies tab -> + button
-> File dependency -> PATH/myfile.jar
Step 1 : Now under your app folder you should see libs, if you don't see it, then create it .
Step 2 : Drag & Drop the .jar file here, you may be get a prompt "This file does not belong to the project", just click OK Button .
Step 3 : Now you should see the jar file under libs folder, right click on the jar file and select "Add as library", Click OK for prompt "Create Library"
Step 4 : Now this jar has been added.
1) create an 'your_libs' folder inside the Project/app/src folder.
2) Copy your jar file into this 'your_libs' folder
3) In Android Studio, go to File -> Project Structure -> Dependencies -> Add -> File Dependency and navigate to your jar file, which should be under 'src/your_libs'
3) Select your jar file and click 'Ok'
and then you can see on your build.gradle like this : compile
files('src/your_libs/your.jar')
In android Studio 1.1.0 .
I solved this question by following steps:
1: Put jar file into libs directory. (in Finder)
2: Open module settings , go to Dependencies ,at left-bottom corner there is a plus button. Click plus button then choose "File Dependency" .Here you can see you jar file. Select it and it's resolved.
I have read all the answers here and they all seem to cover old versions of Android Studio!
With a project created with Android Studio 2.2.3 I just needed to create a libs directory under app and place my jar there.
I did that with my file manager, no need to click or edit anything in Android Studio.
Why it works? Open Build / Edit Libraries and Dependencies and you will see:
{include=[*.jar], dir=libs}
In your project-name/app folder, find the libs folder. If there is no libs folder, create one. Add your .jar file. Right-click on it and you will find add .jar as a dependency. You can find the dependencies added to your build.gradle file.
On Mac OS X:
Add jar as library (drag jar to libs, right click add as lib)
Add compile statement to build.grade
Install gradle v1.6 (use homebrew)
brew install gradle
gradle -v
if not v1.6, upgrade homebrew
gradle clean (rebuild android did not work)
This sorted me out.
My answer is basically gathering some of the right but incomplete answers provided above.
Open build.gradle
Add the following:
dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.3'
}
This will allow support for two different ways of adding dependencies. The compile fileTree(dir: 'libs', include: ['*.jar']) (as #Binod mentioned) tells the compiler to look under the folder libs for ANY jar. It is a good practice to create such a folder 'libs' which will contain the jar packages that our application needs to use.
But this will also allow support for Maven dependency. The compile 'com.google.code.gson:gson:2.3' (as mentioned by #saneryee) it is another recommended way to add dependencies that are in a central remote repository and not in our /libs "local repository". It is basically telling gradle to look for that version of that package and it's telling the compiler to consider it when compiling the project (having it in the classpath)
PS: I use both
Like many before pointed out you shall add
compile files('libs/gson-2.2.3.jar')
to your build.gradle file.
However I have a project in Android Studio that was migrated from Eclipse and in this case the "libs" folder is named "lib" so for me removing the "s" solved the problem.
Added the libs folder at the level of app.
Added all the jars in this project.
Next, selected all the jars, in the libs folder,
right click on the selected items, and say add library
then you will find the jars expansion option, within the project explorer itself.
I observed CTRL + ALT + SHIFT + S --> project structure --> app-module -->Dependencies" already had an entry as (dir: 'libs', include: '*.jar') under compile-option, initially. And after adding the jar's as per the steps stated above, the build.gradle got the entries for the new added jar's, itself.
In Android Studio 2.1 I follow the this way,
Goto app -> src-> main -> assets folder (If not available create it) -> put your JAR files
In your build.gradle add dependency like this,
implementation files('src/main/assets/jsoup.jar')
implementation files('src/main/assets/org-apache-xmlrpc.jar')
implementation files('src/main/assets/org.apache.commons.httpclient.jar')
implementation files('src/main/assets/ws-commons-util-1.0.2.jar')
Sync now.
Now your JAR files ready to use.
For newer Android 1.0.2 the following is already there in your build.gradle file
implementation fileTree(include: ['*.jar'], dir: 'libs')
Add the library jar to your libs folder -> right click the library -> click add as a library -> it asks you for the project to add it for -> select your project-> click ok
The following line is automatically added to build.gradle
implementation files('libs/android-query.jar')
That did it for me. nothing more was required. i have shown this for android aquery another third party library for android.

Categories

Resources