Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding external library in Android studio 0.3.6

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 answers I searched either reference an older version of Android Studio, or describe how to import an external project (source code, not jar file).

After reading a little, I got to the conclusion that manually adding the jar file would be the best way (manual copy/paste and gradle edits) but as I said, I'm fairly new to this technology and don't know where to place the file nor what lines I need to add to the gradle files.

Can someone help me?

UPDATE 1:
I finally made the IDE recognize the .jar file (I get autocomplete and class recognition). The new problem is that I get the following error when compiling: Gradle: package com.google.gson does not exist. Here are the steps I took to import the library:

  1. Creat a folder called libs in the main directory (src/main/libs should be the result)
  2. Copy the .jar file in that directory
  3. add the following line to the dependencies section in the build.gradle file in your project: compile files('libs/gson-2.2.4.jar'). It should look something like this now:

    dependencies { compile 'com.android.support:support-v13:+' compile files('libs/gson-2.2.4.jar') }

  4. Recompile the project (not sure if necessary, but I did it)
  5. Right click on the libs folder and select "Add as Library"
like image 694
Mario Stoilov Avatar asked Nov 27 '13 11:11

Mario Stoilov


People also ask

How do I add a library project to Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

What is external libraries in Android Studio?

You are developing an Android app on Android Studio, sometimes you want to use an external library for your project, such as a jar file. Common langs is an java library with open source code which is provided by the Apache, it has utility methods for working with String, numbers, concurrency ...

How do I add a library to Android Studio?

SIMPLE STEPS TO ADD ANY LIBRARY IN ANDROID STUDIO: Copy the file in question from any folder in your computer (eg. C:/documents/xyz.jar"), then go to Android Studio and right-click the Project Library folder and select paste. Now, right click on the newly added Library file and select the option "add as Library".

How do I add Android Studio libraries to my Gradle project?

There are plenty of Android Studio libraries available through remote repositories. You can use these simply by adding their location and telling Gradle to include them in your code. The great thing about this method is that you can easily switch to a newer update for a given library by changing a single line in your Gradle build file.

How do I add a library to a project?

For the simplest way just follow these steps. Go to File -> New -> Import Module -> <choose library or project folder>. Add library to include section in settings.gradle file and sync the project (After that you can see new folder with library name is added in project structure) include ':mylibraryName'.

How do I add a JAR file to an android project?

This method is suitable for Android Studio 1.0.0 and above. STEPS. First switch your folder structure from Android to Project. Now search for the libs folder inside app - build folder. Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library.


3 Answers

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:

Adding a Maven dependency

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:

Adding GSON dependency

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.

like image 179
Scott Barta Avatar answered Sep 30 '22 08:09

Scott Barta


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.

Update / Recommendation

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.

like image 29
12 revs Avatar answered Sep 30 '22 06:09

12 revs


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

like image 22
ken Avatar answered Sep 30 '22 07:09

ken