99

I am trying to add external library (places it in the /libs directory) to my project. When I try to use the methods from that library, I see that they are in red color which means that the library is not recognized. When I click Ctrl+B, I get a message "Cannot find declaration to go to".

I use these steps to add a library (JAR file): 1. copy the library file into /libs dir 2. open Project Structure -> Global Libraries -> Attach clases 3. find the library and click ok

Is this proper way to do it?

PS. I am trying to add AdMob .jar file this way.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
sandalone
  • 41,141
  • 63
  • 222
  • 338

5 Answers5

159

Easier procedure on latest versions:

  • Copy jar to libs directory in the app (you can create the directory it if not there)
  • Refresh project so libs show up in the structure (right click on project top level, refresh/synchronize)
  • Expand libs and right click on the jar
  • Select "Add as Library"

Done

German
  • 10,263
  • 4
  • 40
  • 56
  • 1
    Guys this exists only since IDEA 13. I asked question I think when it was IDEA 9. – sandalone Apr 10 '14 at 21:52
  • 3
    @sandalone It doesn't matter at this point. People that land here today are most probably using an IDE that supports this simple method by now – German Jun 23 '15 at 13:47
  • what about the javadoc that comes with that library(provided it's a jar file too) ? – niceman Nov 09 '15 at 18:38
  • http://stackoverflow.com/questions/91307/attaching-additional-javadoc-in-intellij-idea – German Nov 10 '15 at 16:50
  • 4
    "Copy jar to libs directory in the app" - But there was no libs directory in "the app". My app directory had only out and src directories in it. So I created a new directory called "libs", and put the jar in it. But I don't know if this is what the direction intended... Then "Refresh project so libs show up in the structure". I could find no command to "refresh". How does one refresh a project? Beginners have a difficult time. :-( – Rafael_Espericueta Dec 20 '15 at 21:24
  • 1
    This answer is more neat. http://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project – Herupkhart Oct 18 '16 at 08:50
  • @Rafael_Espericueta refer to the answer marked as accepted here: http://stackoverflow.com/a/4599243/4429618 – mccoyLBI May 04 '17 at 13:15
  • However, if you have the library as the output of another project and wish to include it in place, this is worthless. Sorry. – Jerry F Jun 28 '17 at 21:45
  • 1
    "Expand libs and right click on the jar" Is not right. When you have copied your jar in the 'lib' folder you should right click the 'lib' folder and select 'Add as Library...' – Henk Aug 08 '18 at 11:26
  • @gerard-verbeek And if you do what you say and you have added more than one jar in the folder all of them are added as library? If you right click on one jar in the folder the "Add as library" option is not there anymore? – German Aug 10 '18 at 09:50
64

I've used this process to attach a 3rd party Jar to an Android project in IDEA.

  • Copy the Jar to your libs/ directory
  • Open Project Settings (Ctrl Alt Shift S)
  • Under the Project Settings panel on the left, choose Modules
  • On the larger right pane, choose the Dependencies tab
  • Press the Add... button on the far right of the screen (if you have a smaller screen like me, you may have to drag resize to the right in order to see it)
  • From the dropdown of Add options, choose "Library". A "Choose Libraries" dialog will appear.
  • Press "New Library..."
  • Choose a suitable title for the library
  • Press "Attach Classes..."
  • Choose the Jar from your libs/ directory, and press OK to dismiss

The library should now be recognised.

Tim Richard
  • 915
  • 7
  • 7
  • Have you tried attaching AdMob library? After these steps I still have package com.google.ads.AdActivity in the Manifest file. – sandalone Aug 15 '11 at 14:57
  • Forgot to write that the package name in the Manifest is still red (the system does not see it) and Ctrl+B still does not work. – sandalone Aug 15 '11 at 15:53
  • 1
    @bergnam, your problem may be caused by parsing bug for the obfuscated classes, see http://youtrack.jetbrains.net/issue/IDEA-71456. – CrazyCoder Aug 23 '11 at 22:31
  • Hm, may be. The only difference here is that this library works, but it is not recognized by the Manifest autocomplete. It colors it in red and I cannot add packages via Ctrl+Space, but have to manually type them. – sandalone Aug 24 '11 at 12:39
  • After following these steps, I had to run a mvn clean package to get the library to be accessible. – Jenn Apr 14 '15 at 17:16
9

Intellij IDEA 15: File->Project Structure...->Project Settings->Libraries

montie
  • 494
  • 8
  • 14
2

A better way in long run is to integrate Gradle in your project environment. Its a build tool for Java, and now being used a lot in the android development space.

You will need to make a .gradle file and list your library dependencies. Then, all you would need to do is import the project in IntelliJ using Gradle.

Cheers

AliR
  • 2,065
  • 1
  • 27
  • 37
2

This question can also be extended if necessary jar file can be found in global library, how can you configure it into your current project.

Process like these: "project structure"-->"modules"-->"click your current project pane at right"-->"dependencies"-->"click little add(+) button"-->"library"-->"select the library you want".

if you are using maven and you can also configure dependency in your pom.xml, but it your chosen version is not like the global library, you will waste memory on storing another version of the same jar file. so i suggest use the first step.

Crabime
  • 644
  • 11
  • 27