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-o
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)
gradle clean (rebuild android did not work)
This sorted me out.
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:
gson-2.2.4.jar
) into the libs
folderEnsure 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]
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"
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.
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.
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.