如何在 IntelliJIDEA 中添加外部库?

我尝试将外部库(放在/libs 目录中)添加到我的项目中。当我尝试使用该库中的方法时,我看到它们是红色的,这意味着不能识别该库。当我单击 Ctrl + B 时,会得到一条消息“ Can not find Declaration to go to”。

我使用这些步骤来添加一个库(JAR 文件) : 1. 将库文件复制到/libs 目录中 2. 打开项目结构-> 全局库-> 附加类 找到图书馆,点击确定

这样做合适吗?

另外,我正在尝试以这种方式添加 AdMob.jar 文件。

190460 次浏览

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.

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

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

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

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.