How to add external library in IntelliJ IDEA?

Intellij Idea

Intellij Idea Problem Overview


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.

Intellij Idea Solutions


Solution 1 - Intellij Idea

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

Solution 2 - Intellij Idea

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.

Solution 3 - Intellij Idea

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

Solution 4 - Intellij Idea

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

Solution 5 - Intellij Idea

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsandaloneView Question on Stackoverflow
Solution 1 - Intellij IdeaGermanView Answer on Stackoverflow
Solution 2 - Intellij IdeaTim RichardView Answer on Stackoverflow
Solution 3 - Intellij IdeamontieView Answer on Stackoverflow
Solution 4 - Intellij IdeaAliRView Answer on Stackoverflow
Solution 5 - Intellij IdeaCrabimeView Answer on Stackoverflow