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?
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.
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.
In the project right click
Follow the screenshots below:
1:
2:
3:
You will see this:
My answer is basically gathering some of the right but incomplete answers provided above.
Add the following:
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
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: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