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?
After I did the above three, it started working fine.
Put the
.jar
files inlibs
folder of the Android project.Then add this line of code in the app's gradle file:
For Android gradle plugin 3.0 and later, it is better to use this instead:
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.
Like many before pointed out you shall add
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.
Step 1 : Now under your
app folder
you should seelibs
, 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 clickOK
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.