So I am fairly new to Java, and I am trying to add this library to my project. The problem is that Android studio 0.3.6 doesn\'t have a simple way of doing that and all the
Running Android Studio 0.4.0 Solved the problem of importing jar by
Project Structure > Modules > Dependencies > Add Files
Browse to the location of jar file and select it
For those like manual editing Open app/build.gradle
dependencies {
compile files('src/main/libs/xxx.jar')
}
I posted the same to importing jar libraries into android-studio putting a duplicate here just in case you stumble into this post instead
Using Android Studio 0.8.2, I had to do the following (supposing the library you're trying to add is called MyExternalLib):
compile project('libs:MyExternalLib')
include ':app:libs:MyExternalLib'
I had the same issue. The new version of Android Studio (0.3.6) removed some necessary features to add an existing library to a project using the IDE. So you have to do this manually.
Adding the library into the build folder "<project>\App\build\libs\" will break the project on "menu > build > clear project / rebuild project".
Updated solution
My solution is to generate a new folder inside "<project>\<app name>\src\main\libs\" and add the library here. Now you have to change your "<project>\<app name>\build.gradle" by adding the following (my example shows the value for android-support library:
dependencies {
compile 'com.android.support:support-v4:13.0.0'
compile 'com.android.support:support-v13:13.0.0'
compile files('libs/gson-2.2.4.jar')
}
Now select the library in "project View" by right click and select "Add as library... > level > Global library". This will fix an import com.google.gson.Gson;
issue.
Maybe you still cannot build. In this case you shall check you project module settings and see if there is an error for Gson dependency. I let Android Studio fix this issue by hitting a "small red bulb icon > add dependency" in the lower right corner of module settings dialog. Now it does not show me no errors anymore on build.
Now we have only one remaining problem: The project does lose the library reference on project close. So we have to add the library on open again. Maybe this is an issue of Android Studio 0.3.6. Mario filed a bug report.
BTW: I upvoted this question because I searched without success for a working solution in the internet. I think beginners will always fail to work with the Android developer tutorials of Google when they are forced to deal with the support library.
Unfortunately I did not get AS 0.3.6 working properly. There are to many issues - at least when adding another module with different namespace. So I switched to the origin IDE: IntelliJ IDEA 12 community Edition. It's free and works for me. I did all the stuff in 2 hours which need days using broken Android Studio. I have no idea what forces Google to build its own IDE based on IntelliJ IDEA without additional benefits / noticeable features when the latter works like a charm.
Click on ProjectName->Libs folder.Paste that jar file into that folder. Just refresh the project.You are done.
Since the GSON library is available in MavenCentral, there's an easy way to add it that avoids having to download an archive file and save it in your project.
Go to Project Structure > Modules > Your module name > Dependencies and click on the + button to add a new dependency. Choose Maven dependency from the list:
You'll get a dialog box where you can enter search terms or the fully-qualified Maven coordinate string. Since GSON is a common library for Android developers to use, it's actually given in this dialog as an example, with the fully-qualified name. You can type it in:
Hit OK on both dialogs and you should be good to go.
With these Maven dependencies, the build system will automatically download the library and cache it if hasn't done so already; it takes care of that for you.
If you had a library that wasn't available on MavenCentral, you could save the archive in a libs
folder in your project, and from that module dependencies dialog, add a File dependency instead of a Maven dependency to take care of it.
If you edit your build.gradle file by hand, you need to click on the "Sync Project with Gradle Files" button in the toolbar to force Android Studio to pick up the changes and update your project. If you go through the Project Structure dialog, that's unnecessary.
There are lots of conflicting answers to this issue in Stack Overflow because the functionality for this is in flux as the necessary features are implemented; it has been really broken before. These instructions should work properly for 0.3.6, and things will get a little easier in 0.3.7 and later.
Create a new library module
It is good development practice to group functionality that you may reuse in other apps inside a library module. To create a library module inside the BuildSystemExample project:
Click File and select New Module.
On the window that appears, select Android Library and click Next.
Leave the default module name (lib) unchanged and click Next.
Select Blank Activity and click Next.
Type "LibActivity1" on the Activity Name field and click Finish.
The project now contains two modules, app and lib, with one activity in each module. https://developer.android.com/sdk/installing/studio-build.html