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?
compile fileTree(dir: 'libs', include: '*.jar')
works fine but notcompile files(...)
have tested with Studio Beta 0.8.1'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.
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 thelibs
folderEnsure that
compile files('libs/gson-2.2.4.jar')
is in yourbuild.gradle
file (orcompile fileTree(dir: 'libs', include: '*.jar')
if you are using many jar files)Edit : Use
implementation files('libs/gson-2.2.4.jar')
(orimplementation 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 systemAfter 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]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.Selectapp
from the opened window then selectdependencies
,then pressgreen plus button
,selectmodule dependency
then select module you imported then pressOK
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 :)
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.
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.