如何向Android Studio添加一个库项目?

时间:2022-10-30 13:20:23

How do I add a library project (such as Sherlock ABS) to Android Studio?

如何在Android Studio中添加一个库项目(比如夏洛克ABS) ?

(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)

(不是到老的ADT eclipse的bundle,而是到新的Android Studio。)

30 个解决方案

#1


681  

Update for Android Studio 1.0

Android Studio 1.0的更新

Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.

自从Android Studio 1.0发布以来(在v1.0和我之前回答的第一个版本之间有很多版本),一些事情发生了变化。

My description is focused on adding external library project by hand via Gradle files (for better understanding the process). If you want to add a library via Android Studio creator just check the answer below with visual guide (there are some differences between Android Studio 1.0 and those from screenshots, but the process is very similar).

我的描述侧重于通过Gradle文件手工添加外部库项目(以便更好地理解这个过程)。如果您想通过Android Studio creator添加一个库,只需使用visual guide检查下面的答案(Android Studio 1.0和截图中的有所不同,但是过程非常相似)。

Before you start adding a library to your project by hand, consider adding the external dependency. It won’t mess in your project structure. Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle file:

在开始手工向项目添加库之前,考虑添加外部依赖项。它不会打乱你的项目结构。几乎所有知名的Android库都可以在Maven存储库中使用,安装时在应用程序/构建中只需要一行代码。gradle文件:

dependencies {
     compile 'com.jakewharton:butterknife:6.0.0'
}

Adding the library

添加库

Here is the full process of adding external Android library to our project:

以下是在我们的项目中添加外部Android库的完整过程:

  1. Create a new project via Android Studio creator. I named it HelloWorld.
  2. 通过Android Studio创建者创建一个新项目。我把它命名为HelloWorld。
  3. Here is the original project structure created by Android Studio:
  4. 以下是Android Studio创建的原始项目结构:
HelloWorld/
      app/
           - build.gradle  // local Gradle configuration (for app only)
           ...
      - build.gradle // Global Gradle configuration (for whole project)
      - settings.gradle
      - gradle.properties
      ...
  1. In the root directory (HelloWorld/), create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure).
  2. 在根目录(HelloWorld/)中,创建新的文件夹:/libs,我们将在其中放置外部库(这一步不是必需的——只是为了保持更清晰的项目结构)。
  3. Paste your library in the newly created /libs folder. In this example I used PagerSlidingTabStrip library (just download ZIP from GitHub, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:
  4. 将库粘贴到新创建的/libs文件夹中。在这个例子中,我使用PagerSlidingTabStrip库(刚从GitHub下载ZIP,重命名库目录„PagerSlidingTabStrip”并将其复制)。以下是我们项目的新结构:
HelloWorld/
      app/
           - build.gradle  // Local Gradle configuration (for app only)
           ...
      libs/
           PagerSlidingTabStrip/
                - build.gradle // Local Gradle configuration (for library only)
      - build.gradle // Global Gradle configuration (for whole project)
      - settings.gradle
      - gradle.properties
      ...
  1. Edit settings.gradle by adding your library to include. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:

    编辑设置。通过增加你的图书馆包括。如果您像我一样使用自定义路径,您还必须为我们的库定义项目目录。一个整体的设置。等级如下:

    include ':app', ':PagerSlidingTabStrip'
    project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')
    

5.1 If you face "Default Configuration" error, then try this instead of step 5,

5.1如果您面临“默认配置”错误,那么请尝试此步骤而不是步骤5,

    include ':app'
    include ':libs:PagerSlidingTabStrip'
  1. In app/build.gradle add our library project as an dependency:

    在app /构建。渐变添加了我们的图书馆项目作为一个依赖:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile project(":PagerSlidingTabStrip")
    }
    

6.1. If you followed step 5.1, then follow this instead of 6,

6.1。如果你遵循了第5.1步,那么遵循这个而不是6步,

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'

        compile project(":libs:PagerSlidingTabStrip")
    }
  1. If your library project doesn’t have build.gradle file you have to create it manually. Here is example of that file:

    如果你的库项目没有构建。渐变文件必须手动创建。下面是这个文件的例子:

        apply plugin: 'com.android.library'
    
        dependencies {
            compile 'com.android.support:support-v4:21.0.3'
        }
    
        android {
            compileSdkVersion 21
            buildToolsVersion "21.1.2"
    
            defaultConfig {
                minSdkVersion 14
                targetSdkVersion 21
            }
    
            sourceSets {
                main {
                    manifest.srcFile 'AndroidManifest.xml'
                    java.srcDirs = ['src']
                    res.srcDirs = ['res']
                }
            }
        }
    
  2. Additionally you can create a global configuration for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit gradle.properties file and add lines:

    此外,您可以为您的项目创建一个全局配置,该配置将包含SDK版本,并为每个模块构建工具版本,以保持一致性。只是编辑它。属性文件和添加行:

    ANDROID_BUILD_MIN_SDK_VERSION=14
    ANDROID_BUILD_TARGET_SDK_VERSION=21
    ANDROID_BUILD_TOOLS_VERSION=21.1.3
    ANDROID_BUILD_SDK_VERSION=21
    

    Now you can use it in your build.gradle files (in app and libraries modules) like below:

    现在您可以在构建中使用它。gradle文件(应用程序和库模块)如下:

    //...
    android {
        compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
        buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    
        defaultConfig {
            minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
            targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        }
    }
    //...
    
  3. That’s all. Just click‚ synchronise the project with the Gradle’ icon 如何向Android Studio添加一个库项目?. Your library should be available in your project.

    这是所有。点击‚同步项目Gradle”图标。您的库应该在项目中可用。

Google I/O 2013 - The New Android SDK Build System is a great presentation about building Android apps with Gradle Build System: As Xavier Ducrohet said:

谷歌I/O 2013——新的Android SDK构建系统是关于使用Gradle构建系统构建Android应用的一个很好的演示:正如Xavier Ducrohet所说:

Android Studio is all about editing, and debugging and profiling. It's not about building any more.

Android Studio关注的是编辑、调试和分析。它不再是关于建造。

At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ), but at the end Gradle gives us some great opportunities and it worth to learn this build system.

一开始可能会有点混乱(特别是对于那些使用Eclipse并且从未见过ant的人——比如我;),但是最后,Gradle给了我们一些很好的机会,值得我们学习这个构建系统。

#2


263  

Here is the visual guide:

以下是视觉指南:

Update for Android Studio 0.8.2:

Android Studio 0.8.2更新:

In Android Studio 0.8.2, go to Project Structure -> under Modules just hit the plus button and select Import Existing Project and import actionbarsherlock. Then synchronise your Gradle files.

在Android Studio 0.8.2中,进入项目结构——模块下的>,点击加号按钮,选择导入现有项目,导入actionbarsherlock。然后同步你的等级文件。

If you face the error

如果你面对错误

Error: The SDK Build Tools revision (xx.x.x) is too low. Minimum required is yy.y.y

错误:SDK构建工具修订(xx.x.x)太低了。最低要求是yy.y.y

just open the build.gradle file in actionbarsherlock directory and update the buildToolsVersion to the suggested one.

只需要打开一个构建。将actionbarsherlock目录中的渐变文件更新为建议的构建工具版本。

android {
  compileSdkVersion 19
  buildToolsVersion 'yy.y.y'

如何向Android Studio添加一个库项目?


Menu File -> Project Structure...:

菜单文件->项目结构…:

如何向Android Studio添加一个库项目?

Module -> Import Module

模块- >导入模块

如何向Android Studio添加一个库项目?

After importing the library module, select your project module and add the dependency:

导入库模块后,选择项目模块,添加依赖项:

如何向Android Studio添加一个库项目?

And then select the imported module:

然后选择导入模块:

如何向Android Studio添加一个库项目?

#3


106  

Use menu File -> Project Structure -> Modules.

使用菜单文件->项目结构->模块。

I started using it today. It is a bit different.

我今天开始用它。这有点不同。

For Sherlock, maybe you want to delete their test directory, or add the junit.jar file to the classpath.

对于夏洛克,也许您想删除他们的测试目录,或者添加junit。jar文件到类路径。

To import the library using gradle, you can have to add it to the dependencies section of your build.gradle (the module's one).

要使用gradle导入库,您必须将其添加到构建的依赖项部分。gradle(模块的一个)。

E.g.

如。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
}

Android Studio is changing.

Android工作室正在改变。

There exist a section named "Open module settings" if you right-click on a module folder in the project section of Android Studio (I'm using the version 0.2.10).

如果您右键单击Android Studio的project部分(我使用的是0.2.10版本)中的一个模块文件夹,就会有一个名为“打开模块设置”的部分。

#4


49  

I would consider Dependencies, Android Libraries and Multi-project setup necessary reading. Please take a few minutes to do so.

我将考虑依赖性、Android库和多项目设置。请花几分钟时间。

Particularly, in the case of a non-jar library project, read the following snippet from above source:

特别是,对于非jar库项目,请阅读上面源代码中的以下代码片段:

Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project.

通过使用多项目设置,Gradle项目也可以依赖于其他的Gradle项目。多项目设置通常是将所有项目作为给定根项目的子文件夹。

For instance, given to following structure:

例如,给出以下结构:

MyProject/
 + app/
 + libraries/
    + lib1/
    + lib2/

We can identify 3 projects. Gradle will reference them with the following name:

我们可以确定三个项目。Gradle将以以下名称引用它们:

:app
:libraries:lib1
:libraries:lib2

Each projects will have its own build.gradle declaring how it gets built. Additionally, there will be a file called settings.gradle at the root declaring the projects. This gives the following structure:

每个项目都有自己的构建。葛莱蒂宣布它是如何建造的。此外,还会有一个名为settings的文件。在项目声明的根上的渐变。这就形成了以下结构:

MyProject/
 | settings.gradle
 + app/
    | build.gradle
 + libraries/
    + lib1/
       | build.gradle
    + lib2/
       | build.gradle

The content of settings.gradle is very simple:

设置的内容。它很简单:

include ':app', ':libraries:lib1', ':libraries:lib2'

This defines which folder is actually a Gradle project.

这定义了哪个文件夹实际上是一个Gradle项目。

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

app项目可能依赖于库,通过声明以下依赖项来实现:

dependencies {
    compile project(':libraries:lib1')
}

Kindly note that there was little or no use of Android Studio GUI to make this happen.

请注意,Android Studio GUI很少或根本没有使用它来实现这一点。

I am currently using git submodules to link the nested library to the actual library git repo to avoid a dependency mess.

我目前正在使用git子模块将嵌套库链接到实际的git repo库,以避免依赖性混乱。

#5


23  

I have just found an easier way (rather than writing directly into the .gradle files).

我找到了一个更简单的方法(而不是直接写入.gradle文件)。

This is for Android Studio 1.1.0.

这是针对Android Studio 1.1.0的。

  1. Menu File -> New Module...:

    菜单文件->新模块…:

    如何向Android Studio添加一个库项目?

    Click on "Import Existing Project".

    点击“导入现有项目”。

  2. Select the desired library and the desired module.

    选择所需的库和所需的模块。

  3. Click finish. Android Studio will import the library into your project. It will sync gradle files.

    单击finish。Android Studio将把库导入到您的项目中。它会同步等级文件。

  4. Add the imported module to your project's dependencies.

    将导入的模块添加到项目的依赖项中。

    Right click on the app folder -> Open Module settings -> go to the dependencies tab -> Click on the '+' button -> click on Module Dependency.

    右键单击app文件夹->打开模块设置->到dependencies选项卡->单击“+”按钮->单击模块依赖项。

    The library module will be then added to the project's dependencies.

    然后,库模块将被添加到项目的依赖项中。

    如何向Android Studio添加一个库项目?

  5. ???

    ? ? ?

  6. Profit

    利润

#6


15  

The easiest way I found to include external library project is (for example to include a Facebook library which is stored one directory up in the dependencies folder):

我发现包含外部库项目的最简单的方法是(例如包含一个Facebook库,它在dependencies文件夹中存储了一个目录):

  1. In settings.gradle add

    在设置。gradle添加

    include ':facebook'
    
    project(':facebook').projectDir = new File(settingsDir, '../dependencies/FacebookSDK')
    
  2. In build.gradle dependencies section, add

    在构建。gradle依赖性部分,添加

    compile project ('facebook')
    

All left to do is synchronise the project with gradle files.

所有剩下要做的是与gradle文件同步项目。

#7


13  

A simple way to add a JAR file as a library to your Android Studio project:

将JAR文件作为库添加到Android Studio项目的简单方法是:

a) Copy your *.jar files

复制你的*。jar文件

b) Paste into the libs directory under your projects:

b)粘贴到项目下的libs目录:

如何向Android Studio添加一个库项目?

c) Add to build.gradle:

build.gradle c)添加:

dependencies {
    ...
    compile files('libs/ScanAPIAndroid.jar', 'libs/ScanAPIFactoryAndroid.jar', .., ..)
}

b) If your project from example com.example.MYProject and libraries com.example.ScanAPI has the same namespace com.example, Android Studio will check your build and create all necessary changes in your project. After that you can review these settings in menu File -> Project Structure.

b)如果您的项目来自示例com.example。com . example。MYProject和库ScanAPI有相同的名称空间com。例如,Android Studio将检查您的构建并在您的项目中创建所有必要的更改。之后,您可以在菜单文件>项目结构中查看这些设置。

c) If your project and libraries have a different namespace you have to right click on the library and select option "Add as Library" and select the type what you need.

c)如果您的项目和库有不同的名称空间,您必须右键单击库并选择“Add as library”选项,并选择所需的类型。

Remember the "Project structure" option is not doing any auto changes in "build.gradle" in the current version of Android Studio (0.2.3). Maybe this feature will be available in the next versions.

记住,“项目结构”选项不是在“构建”中执行任何自动更改。Android Studio的当前版本(0.2.3)。也许在下一个版本中可以使用这个特性。

#8


11  

Option 1: Drop Files Into Project's libs/directory

The relevant build.gradle file will then update automatically.

相关的构建。渐变文件将自动更新。

Option 2: Modify build.gradle File Manually

Open your build.gradle file and add a new build rule to the dependencies closure. For example, if you wanted to add Google Play Services, your project's dependencies section would look something like this:

打开您的构建。渐变文件,并向依赖项闭包添加新的构建规则。例如,如果您想添加谷歌Play服务,您的项目依赖项部分将如下所示:

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.google.android.gms:play-services:6.5.+'
   }

Option 3: Use Android Studio's User Interface

In the Project panel, Control + click the module you want to add the dependency to and select Open Module Settings.

在项目面板中,Control +单击要添加依赖项的模块,并选择Open module设置。

如何向Android Studio添加一个库项目?

Select the Dependencies tab, followed by the + button in the bottom-left corner. You can choose from the following list of options:

选择Dependencies选项卡,然后是左下角的+按钮。你可以从以下选项列表中选择:

  • Library Dependency
  • 库的依赖
  • File Dependency
  • 文件的依赖
  • Module Dependency
  • 模块依赖关系

You can then enter more information about the dependency you want to add to your project. For example, if you choose Library Dependency, Android Studio displays a list of libraries for you to choose from.

然后,您可以输入关于要添加到项目中的依赖项的更多信息。例如,如果您选择库依赖项,Android Studio将显示一个库列表供您选择。

Once you've added your dependency, check your module-level build.gradle file. It should have automatically updated to include the new dependency.

一旦添加了依赖项,请检查模块级的构建。gradle文件。它应该自动更新以包含新的依赖项。

Source

#9


8  

If you need access to the resources of a library project (as you do with ABS) ensure that you add the library project/module as a "Module Dependency" instead of a "Library".

如果您需要访问库项目的资源(就像您使用ABS一样),请确保将库项目/模块添加为“模块依赖项”,而不是“库”。

#10


8  

This is how it works for me in Android Studio 1.5

In the project where you want to add external library project, go to menu File -> New -> *Import new Module**, navigate to the library project which you want to add to your project, select to add 'library' module in your project. You will get settings.gradle in your projects, beside app, included library, something like this:

在要添加外部库项目的项目中,转到菜单文件> New -> *导入新模块**,导航到要添加到项目的库项目,选择在项目中添加“library”模块。你会得到设置。在你的项目中,除了应用程序,包括图书馆,类似这样的东西:

include ':app', ':library'

Add in build.gradle(Module :app) in the dependencies section:

添加在build。依赖项部分的等级(模块:app):

Compile project(':library')

编译项目(“:库”)

Rebuild the project, and that's it.

重建项目,就是这样。

*You can add as many libraries (modules) as you want. In that case in settings.gradle you will have:

您可以添加任意数量的库(模块)。在这种情况下,在设置中。gradle你会:

 include ':app', ':lib1', ':lib2', ...

And in build.gradle, you'll need to have:

在构建。格蕾尔,你需要:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Some other dependencies...

    compile project(':lib1')
    compile project(':lib2')
    ...
}

#11


6  

  1. Press F4 to show Project Structure, click libraries or Global libraries, and click + to add the JAR file.
  2. 按F4显示项目结构,点击库或全局库,点击+添加JAR文件。
  3. Click Modules what you want add jar, select the Dependencies tab, click +, and add Library.
  4. 单击要添加jar的模块,选择Dependencies选项卡,单击+,并添加库。

#12


6  

You can do this easily. Go to menu File -> New -> Import Module...:

这很容易做到。进入菜单文件->新->导入模块…:

如何向Android Studio添加一个库项目?

Browse for the directory which contains the module. Click Finish:

浏览包含模块的目录。单击Finish。

如何向Android Studio添加一个库项目?

Go to Project Structure and add Module Dependency:

进入项目结构并添加模块依赖项:

如何向Android Studio添加一个库项目?

Note: If you receive an SDK error, update that one.

注意:如果您收到一个SDK错误,请更新它。

#13


5  

To add to the answer: If the IDE doesn't show any error, but when you try to compile, you get something like:

为了补充答案:如果IDE没有显示任何错误,但是当您尝试编译时,您将得到如下内容:

No resource found that matches the given name 'Theme.Sherlock.Light'

Your library project is probably compiled as an application project. To change this, go to:

您的库项目可能被编译为应用程序项目。要改变这一点,可以去:

Menu File -> Project structure -> Facets -> [Library name] -> Check "Library module".

菜单文件->项目结构-> facet ->[库名]->检查“库模块”。

#14


5  

Editing library dependencies through the GUI is not advisable as that doesn't write those changes to your build.gradle file. So your project will not build from the command-line. We should edit the build.gradle file directly as follows.

通过GUI编辑库依赖性是不可取的,因为它不会将这些更改写入您的构建。gradle文件。因此,您的项目不会从命令行构建。我们应该编辑构建。等级文件直接如下。

For instance, given to following structure:

例如,给出以下结构:

MyProject/

MyProject的/

  • app/
  • app /
  • libraries/
    • lib1/
    • lib1 /
    • lib2/
    • lib2 /
  • 库/ lib1 / lib2 /

We can identify three projects. Gradle will reference them with the following names:

我们可以确定三个项目。Gradle将会用以下的名字引用它们:

  1. :app
  2. :应用程序
  3. :libraries:lib1
  4. 库:lib1
  5. :libraries:lib2
  6. 库:lib2

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

app项目可能依赖于库,通过声明以下依赖项来实现:

dependencies {
    compile project(':libraries:lib1')
}

#15


3  

After importing the ABS Module (from File > Project Structure) and making sure it has Android 2.2 and Support Library v4 as dependencies, I was still getting the following error as you @Alex

在导入ABS模块(来自文件>项目结构)并确保它有Android 2.2并支持Library v4作为依赖后,我仍然会得到以下错误,如您@Alex

Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.DarkActionBar'

I added the newly imported module as a dependency to my main app module and that fixed the problem.

我将新导入的模块作为主应用程序模块的依赖项添加到主应用程序模块中,解决了这个问题。

#16


3  

To resolve this problem, you just need to add the abs resource path to your project build file, just like below:

要解决这个问题,只需将abs资源路径添加到项目构建文件中,如下所示:

sourceSets {
    main {
        res.srcDirs = ['src/main/res','../../ActionBarSherlock/actionbarsherlock/res']
    }
}

So, I again compile without any errors.

因此,我再次编译,没有任何错误。

#17


3  

First Way This is working for MacBook.

第一种方法是在MacBook上运行。

First select your builder.gradle file as given screen:

首先选择您的建设者。给定屏幕的渐变文件:

如何向Android Studio添加一个库项目?

Add dependencies like as on the selected screen:

添加依赖项,如在选定的屏幕上:

如何向Android Studio添加一个库项目?

Select sync project.

选择同步的项目。

If you are getting an error like "Project with path':signature-pad' could not be found in project ':app'", then please use the second way:

如果你有一个类似于“path”的错误:在项目“app”中找不到“签字人”,那么请使用第二种方式:

Select menu File -> New -> Import Module...:

选择菜单文件->新->导入模块…:

如何向Android Studio添加一个库项目?

After clicking on Import Module,

点击导入模块后,

如何向Android Studio添加一个库项目?

give the path of library like as my MacBook path:

给出像我的MacBook path这样的库路径:

如何向Android Studio添加一个库项目?

Click on Finish. Now your library are added.

点击Finish。现在添加了库。

#18


2  

If you have Android Studio .0.4.0, you can create a new folder in your build path, YourApp/libraries. Copy the JAR file. There in, right click on it and "Add As Library". Now you have a popup. Just select your directory and press OK, and that's it.

如果你有Android Studio .0.4.0,你可以在你的构建路径,app /库中创建一个新的文件夹。复制JAR文件。在那里,右键单击它并“添加为库”。现在你有一个弹出窗口。选择你的目录并按下OK,就这样了。

#19


2  

https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0 is the Dropbox link of how to add a JAR file and library project in the latest version of Android Studio 1.0.1.

https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0是如何在最新版本的Android Studio 1.0.1中添加JAR文件和库项目的Dropbox链接。

Please see the documentation with screenshots. It's very easy for a new user.

请查看带有屏幕截图的文档。对于一个新用户来说很容易。

#20


1  

I found the solution. It's so simple. Follow froger_mcs instructions.

我找到了解决方案。它是如此简单。遵循froger_mcs指令。

Make sure that you make the src folder a Source folder in Project Structure -> Modules (Sources).

确保将src文件夹设置为项目结构中的源文件夹——>模块(Source)。

如何向Android Studio添加一个库项目?

#21


1  

An example of succesfully adding another library (PullToRefresh). Also works for ABS libproject.

成功添加另一个库(PullToRefresh)的示例。同样适用于ABS libproject。

This SO Question

这样的问题

This post

这篇文章

#22


1  

Basically, you can include your JAR files in three different ways. The last one is remote library that is using https://bintray.com/ jcenter online repository. But, if you do it in one of the two other ways, the JAR file will be included physically in your project. Please read this link https://*.com/a/35369267/5475941 for more information. In this post I explained how to import your JAR file in Android studio and I explained all possible ways.

基本上,您可以用三种不同的方式包含JAR文件。最后一个是使用https://bintray.com/ jcenter在线存储库的远程库。但是,如果您在其他两种方法中使用它,JAR文件将会包含在您的项目中。请阅读这个链接https://*.com/a/35369267/5475941了解更多信息。在这篇文章中,我解释了如何在Android studio中导入JAR文件,并解释了所有可能的方法。

In summary, if it is like this (local address), they are downloaded and these JAR files are physically in the project:

总之,如果是这样(本地地址),就会被下载,这些JAR文件在项目中是物理的:

如何向Android Studio添加一个库项目?

But, if it is an internet address like this, they are remote libraries (bintray.com jcenter part) and they will be used remotely:

但是,如果它是像这样的互联网地址,它们是远程库(bintray.com jcenter部分),它们将被远程使用:

如何向Android Studio添加一个库项目?

I hope it helps.

我希望它有帮助。

#23


1  

Open the build gradle module app file and add your dependency. If you download the library, just import and build as gradle.

打开build gradle模块应用程序文件并添加您的依赖项。如果您下载了库,只需导入并构建为level。

Otherwise add repositories in side gradle module app:

另外,在侧级模块app中添加存储库:

repositories {
        maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}

The first repositories will download the library for you.

第一个存储库将为您下载该库。

And compile the downloaded library:

并编译下载的库:

 compile ('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT@aar') {
        transitive = true
    }

If you are creating a library, you just need to import the project as import new module.

如果您正在创建一个库,您只需要将项目作为导入新模块导入即可。

#24


1  

In Android Studio, go to inside app folder, and open build.gradle file. Here you will see dependencies{}. Inside it you can add the library project and synchronise. Now after synchronising the library it will be added to your project, and you can use its functions and classes in your project.

在Android Studio中,进入应用程序文件夹,打开build。gradle文件。这里您将看到dependencies{}。在其中可以添加库项目并进行同步。现在,在同步库之后,它将被添加到项目中,您可以在项目中使用它的函数和类。

#25


1  

For Android Studio:

为Android工作室:

如何向Android Studio添加一个库项目?

Click on Build.gradle (module: app).

点击构建。gradle(模块:应用程序)。

And add for

和添加

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/commons-io-2.4.jar')
}

and in your directory "app", create a directory, "libs". Add the file yourfile.jar:

在你的“app”目录中,创建一个“libs”目录。添加文件yourfile.jar:

如何向Android Studio添加一个库项目?

Finally, compile the Gradle Files:

最后,编写等级文件:

如何向Android Studio添加一个库项目?

#26


1  

Simply import the Android library project as a module and in Build.gradle.

简单地将Android库项目作为一个模块导入到Build.gradle中。

Apply plugin: 'com.android.library'

应用插件:“com.android.library”

After that, follow these steps:

然后,遵循以下步骤:

  1. Right click on Module & select open Module settings
  2. 右键单击模块并选择打开模块设置
  3. Select dependencies, click on +, select library dependencies, and add the previously imported module.
  4. 选择依赖项,单击+,选择库依赖项,并添加先前导入的模块。

#27


1  

I had a different cause of the problem so for people:

我有不同的原因导致这个问题

repositories {
    mavenCentral()
}

change mavenCentral() to jcenter() and add

将mavenCentral()更改为jcenter()并添加

allprojects {
    repositories {
        jcenter()
    }
}

#28


1  

Android Studio 3.0

Just add the library name to the dependencies block of your app's build.gradle file.

只需将库名添加到应用程序构建的依赖项块中。gradle文件。

dependencies {
    // ...
    implementation 'com.example:some-library:1.0.0'
}

Note that you should use implementation rather than compile now. This is new with Android Studio 3.0. See this Q&A for an explanation of the difference.

注意,您应该使用实现而不是编译。这是Android Studio 3.0的新版本。请参看这个问答,以解释其中的不同之处。

#29


0  

I also encountered the same problem then I did following things.

我也遇到了同样的问题,然后我做了以下的事情。

  1. I import the library project into my AndroidStudio IDE as a module using menu File -> Import module menus

    我使用菜单文件—>导入模块菜单将库项目导入到AndroidStudio IDE中

  2. Then I went to my main module in which I want the library project as a dependent project

    然后我进入了我的主要模块,我希望将库项目作为一个独立的项目。

  3. Right click on the main module (in my case its name is app) -> open module setting -> go into dependencies tab -> click on + button (you will get it on right side of window) -> click on module dependency -> select your library project from list

    右键单击主模块(在我的例子中,它的名字是app) ->打开模块设置->进入dependencies选项卡->单击+按钮(在窗口的右边)->单击模块依赖项->从列表中选择您的库项目

Apply the changes and click the OK button.

应用更改并单击OK按钮。

It worked for me. I hope it will help others too.

它为我工作。我希望它也能帮助别人。

#30


-2  

Indeed as versions are changing, so is changing the user interface and options available on the menu. After reading most of the answers to these questions I had to guess what would work for Android Studio 1.1.0.

实际上,随着版本的变化,用户界面和菜单上的选项也在变化。在阅读了这些问题的大部分答案后,我不得不猜测Android Studio 1.1.0会有什么效果。

  1. With your mouse, select the project at the main level (this is where it shows the name of your app).

    使用鼠标,在主级别上选择项目(这是显示应用程序名称的地方)。

  2. Right click, and select the menu options New, Folder, Assets Folder.

    右键单击,并选择菜单选项New、Folder、Assets文件夹。

  3. After creating the assets folder, paste or copy in it, whatever JAR file you need for your library.

    在创建了assets文件夹之后,在其中粘贴或复制任何您需要的JAR文件。

  4. From Android Studio's main menu (top of the screen) select File -> Project Structure.

    从Android Studio的主菜单(屏幕顶部)选择File ->项目结构。

  5. Then select your project name and go to the Dependencies tab.

    然后选择项目名称并转到Dependencies选项卡。

  6. Click on the plus sign (+) on the lower left of the dialog box and select file dependency.

    单击对话框左下方的加号(+)并选择file dependency。

  7. Finally open the recently created assets folder, select the JAR files that you copied, and then click apply and OK.

    最后打开最近创建的assets文件夹,选择复制的JAR文件,然后单击apply和OK。

Clean and rebuild your project.

清理和重建你的项目。

#1


681  

Update for Android Studio 1.0

Android Studio 1.0的更新

Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.

自从Android Studio 1.0发布以来(在v1.0和我之前回答的第一个版本之间有很多版本),一些事情发生了变化。

My description is focused on adding external library project by hand via Gradle files (for better understanding the process). If you want to add a library via Android Studio creator just check the answer below with visual guide (there are some differences between Android Studio 1.0 and those from screenshots, but the process is very similar).

我的描述侧重于通过Gradle文件手工添加外部库项目(以便更好地理解这个过程)。如果您想通过Android Studio creator添加一个库,只需使用visual guide检查下面的答案(Android Studio 1.0和截图中的有所不同,但是过程非常相似)。

Before you start adding a library to your project by hand, consider adding the external dependency. It won’t mess in your project structure. Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle file:

在开始手工向项目添加库之前,考虑添加外部依赖项。它不会打乱你的项目结构。几乎所有知名的Android库都可以在Maven存储库中使用,安装时在应用程序/构建中只需要一行代码。gradle文件:

dependencies {
     compile 'com.jakewharton:butterknife:6.0.0'
}

Adding the library

添加库

Here is the full process of adding external Android library to our project:

以下是在我们的项目中添加外部Android库的完整过程:

  1. Create a new project via Android Studio creator. I named it HelloWorld.
  2. 通过Android Studio创建者创建一个新项目。我把它命名为HelloWorld。
  3. Here is the original project structure created by Android Studio:
  4. 以下是Android Studio创建的原始项目结构:
HelloWorld/
      app/
           - build.gradle  // local Gradle configuration (for app only)
           ...
      - build.gradle // Global Gradle configuration (for whole project)
      - settings.gradle
      - gradle.properties
      ...
  1. In the root directory (HelloWorld/), create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure).
  2. 在根目录(HelloWorld/)中,创建新的文件夹:/libs,我们将在其中放置外部库(这一步不是必需的——只是为了保持更清晰的项目结构)。
  3. Paste your library in the newly created /libs folder. In this example I used PagerSlidingTabStrip library (just download ZIP from GitHub, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:
  4. 将库粘贴到新创建的/libs文件夹中。在这个例子中,我使用PagerSlidingTabStrip库(刚从GitHub下载ZIP,重命名库目录„PagerSlidingTabStrip”并将其复制)。以下是我们项目的新结构:
HelloWorld/
      app/
           - build.gradle  // Local Gradle configuration (for app only)
           ...
      libs/
           PagerSlidingTabStrip/
                - build.gradle // Local Gradle configuration (for library only)
      - build.gradle // Global Gradle configuration (for whole project)
      - settings.gradle
      - gradle.properties
      ...
  1. Edit settings.gradle by adding your library to include. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:

    编辑设置。通过增加你的图书馆包括。如果您像我一样使用自定义路径,您还必须为我们的库定义项目目录。一个整体的设置。等级如下:

    include ':app', ':PagerSlidingTabStrip'
    project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')
    

5.1 If you face "Default Configuration" error, then try this instead of step 5,

5.1如果您面临“默认配置”错误,那么请尝试此步骤而不是步骤5,

    include ':app'
    include ':libs:PagerSlidingTabStrip'
  1. In app/build.gradle add our library project as an dependency:

    在app /构建。渐变添加了我们的图书馆项目作为一个依赖:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile project(":PagerSlidingTabStrip")
    }
    

6.1. If you followed step 5.1, then follow this instead of 6,

6.1。如果你遵循了第5.1步,那么遵循这个而不是6步,

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'

        compile project(":libs:PagerSlidingTabStrip")
    }
  1. If your library project doesn’t have build.gradle file you have to create it manually. Here is example of that file:

    如果你的库项目没有构建。渐变文件必须手动创建。下面是这个文件的例子:

        apply plugin: 'com.android.library'
    
        dependencies {
            compile 'com.android.support:support-v4:21.0.3'
        }
    
        android {
            compileSdkVersion 21
            buildToolsVersion "21.1.2"
    
            defaultConfig {
                minSdkVersion 14
                targetSdkVersion 21
            }
    
            sourceSets {
                main {
                    manifest.srcFile 'AndroidManifest.xml'
                    java.srcDirs = ['src']
                    res.srcDirs = ['res']
                }
            }
        }
    
  2. Additionally you can create a global configuration for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit gradle.properties file and add lines:

    此外,您可以为您的项目创建一个全局配置,该配置将包含SDK版本,并为每个模块构建工具版本,以保持一致性。只是编辑它。属性文件和添加行:

    ANDROID_BUILD_MIN_SDK_VERSION=14
    ANDROID_BUILD_TARGET_SDK_VERSION=21
    ANDROID_BUILD_TOOLS_VERSION=21.1.3
    ANDROID_BUILD_SDK_VERSION=21
    

    Now you can use it in your build.gradle files (in app and libraries modules) like below:

    现在您可以在构建中使用它。gradle文件(应用程序和库模块)如下:

    //...
    android {
        compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
        buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    
        defaultConfig {
            minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
            targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        }
    }
    //...
    
  3. That’s all. Just click‚ synchronise the project with the Gradle’ icon 如何向Android Studio添加一个库项目?. Your library should be available in your project.

    这是所有。点击‚同步项目Gradle”图标。您的库应该在项目中可用。

Google I/O 2013 - The New Android SDK Build System is a great presentation about building Android apps with Gradle Build System: As Xavier Ducrohet said:

谷歌I/O 2013——新的Android SDK构建系统是关于使用Gradle构建系统构建Android应用的一个很好的演示:正如Xavier Ducrohet所说:

Android Studio is all about editing, and debugging and profiling. It's not about building any more.

Android Studio关注的是编辑、调试和分析。它不再是关于建造。

At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ), but at the end Gradle gives us some great opportunities and it worth to learn this build system.

一开始可能会有点混乱(特别是对于那些使用Eclipse并且从未见过ant的人——比如我;),但是最后,Gradle给了我们一些很好的机会,值得我们学习这个构建系统。

#2


263  

Here is the visual guide:

以下是视觉指南:

Update for Android Studio 0.8.2:

Android Studio 0.8.2更新:

In Android Studio 0.8.2, go to Project Structure -> under Modules just hit the plus button and select Import Existing Project and import actionbarsherlock. Then synchronise your Gradle files.

在Android Studio 0.8.2中,进入项目结构——模块下的>,点击加号按钮,选择导入现有项目,导入actionbarsherlock。然后同步你的等级文件。

If you face the error

如果你面对错误

Error: The SDK Build Tools revision (xx.x.x) is too low. Minimum required is yy.y.y

错误:SDK构建工具修订(xx.x.x)太低了。最低要求是yy.y.y

just open the build.gradle file in actionbarsherlock directory and update the buildToolsVersion to the suggested one.

只需要打开一个构建。将actionbarsherlock目录中的渐变文件更新为建议的构建工具版本。

android {
  compileSdkVersion 19
  buildToolsVersion 'yy.y.y'

如何向Android Studio添加一个库项目?


Menu File -> Project Structure...:

菜单文件->项目结构…:

如何向Android Studio添加一个库项目?

Module -> Import Module

模块- >导入模块

如何向Android Studio添加一个库项目?

After importing the library module, select your project module and add the dependency:

导入库模块后,选择项目模块,添加依赖项:

如何向Android Studio添加一个库项目?

And then select the imported module:

然后选择导入模块:

如何向Android Studio添加一个库项目?

#3


106  

Use menu File -> Project Structure -> Modules.

使用菜单文件->项目结构->模块。

I started using it today. It is a bit different.

我今天开始用它。这有点不同。

For Sherlock, maybe you want to delete their test directory, or add the junit.jar file to the classpath.

对于夏洛克,也许您想删除他们的测试目录,或者添加junit。jar文件到类路径。

To import the library using gradle, you can have to add it to the dependencies section of your build.gradle (the module's one).

要使用gradle导入库,您必须将其添加到构建的依赖项部分。gradle(模块的一个)。

E.g.

如。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
}

Android Studio is changing.

Android工作室正在改变。

There exist a section named "Open module settings" if you right-click on a module folder in the project section of Android Studio (I'm using the version 0.2.10).

如果您右键单击Android Studio的project部分(我使用的是0.2.10版本)中的一个模块文件夹,就会有一个名为“打开模块设置”的部分。

#4


49  

I would consider Dependencies, Android Libraries and Multi-project setup necessary reading. Please take a few minutes to do so.

我将考虑依赖性、Android库和多项目设置。请花几分钟时间。

Particularly, in the case of a non-jar library project, read the following snippet from above source:

特别是,对于非jar库项目,请阅读上面源代码中的以下代码片段:

Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project.

通过使用多项目设置,Gradle项目也可以依赖于其他的Gradle项目。多项目设置通常是将所有项目作为给定根项目的子文件夹。

For instance, given to following structure:

例如,给出以下结构:

MyProject/
 + app/
 + libraries/
    + lib1/
    + lib2/

We can identify 3 projects. Gradle will reference them with the following name:

我们可以确定三个项目。Gradle将以以下名称引用它们:

:app
:libraries:lib1
:libraries:lib2

Each projects will have its own build.gradle declaring how it gets built. Additionally, there will be a file called settings.gradle at the root declaring the projects. This gives the following structure:

每个项目都有自己的构建。葛莱蒂宣布它是如何建造的。此外,还会有一个名为settings的文件。在项目声明的根上的渐变。这就形成了以下结构:

MyProject/
 | settings.gradle
 + app/
    | build.gradle
 + libraries/
    + lib1/
       | build.gradle
    + lib2/
       | build.gradle

The content of settings.gradle is very simple:

设置的内容。它很简单:

include ':app', ':libraries:lib1', ':libraries:lib2'

This defines which folder is actually a Gradle project.

这定义了哪个文件夹实际上是一个Gradle项目。

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

app项目可能依赖于库,通过声明以下依赖项来实现:

dependencies {
    compile project(':libraries:lib1')
}

Kindly note that there was little or no use of Android Studio GUI to make this happen.

请注意,Android Studio GUI很少或根本没有使用它来实现这一点。

I am currently using git submodules to link the nested library to the actual library git repo to avoid a dependency mess.

我目前正在使用git子模块将嵌套库链接到实际的git repo库,以避免依赖性混乱。

#5


23  

I have just found an easier way (rather than writing directly into the .gradle files).

我找到了一个更简单的方法(而不是直接写入.gradle文件)。

This is for Android Studio 1.1.0.

这是针对Android Studio 1.1.0的。

  1. Menu File -> New Module...:

    菜单文件->新模块…:

    如何向Android Studio添加一个库项目?

    Click on "Import Existing Project".

    点击“导入现有项目”。

  2. Select the desired library and the desired module.

    选择所需的库和所需的模块。

  3. Click finish. Android Studio will import the library into your project. It will sync gradle files.

    单击finish。Android Studio将把库导入到您的项目中。它会同步等级文件。

  4. Add the imported module to your project's dependencies.

    将导入的模块添加到项目的依赖项中。

    Right click on the app folder -> Open Module settings -> go to the dependencies tab -> Click on the '+' button -> click on Module Dependency.

    右键单击app文件夹->打开模块设置->到dependencies选项卡->单击“+”按钮->单击模块依赖项。

    The library module will be then added to the project's dependencies.

    然后,库模块将被添加到项目的依赖项中。

    如何向Android Studio添加一个库项目?

  5. ???

    ? ? ?

  6. Profit

    利润

#6


15  

The easiest way I found to include external library project is (for example to include a Facebook library which is stored one directory up in the dependencies folder):

我发现包含外部库项目的最简单的方法是(例如包含一个Facebook库,它在dependencies文件夹中存储了一个目录):

  1. In settings.gradle add

    在设置。gradle添加

    include ':facebook'
    
    project(':facebook').projectDir = new File(settingsDir, '../dependencies/FacebookSDK')
    
  2. In build.gradle dependencies section, add

    在构建。gradle依赖性部分,添加

    compile project ('facebook')
    

All left to do is synchronise the project with gradle files.

所有剩下要做的是与gradle文件同步项目。

#7


13  

A simple way to add a JAR file as a library to your Android Studio project:

将JAR文件作为库添加到Android Studio项目的简单方法是:

a) Copy your *.jar files

复制你的*。jar文件

b) Paste into the libs directory under your projects:

b)粘贴到项目下的libs目录:

如何向Android Studio添加一个库项目?

c) Add to build.gradle:

build.gradle c)添加:

dependencies {
    ...
    compile files('libs/ScanAPIAndroid.jar', 'libs/ScanAPIFactoryAndroid.jar', .., ..)
}

b) If your project from example com.example.MYProject and libraries com.example.ScanAPI has the same namespace com.example, Android Studio will check your build and create all necessary changes in your project. After that you can review these settings in menu File -> Project Structure.

b)如果您的项目来自示例com.example。com . example。MYProject和库ScanAPI有相同的名称空间com。例如,Android Studio将检查您的构建并在您的项目中创建所有必要的更改。之后,您可以在菜单文件>项目结构中查看这些设置。

c) If your project and libraries have a different namespace you have to right click on the library and select option "Add as Library" and select the type what you need.

c)如果您的项目和库有不同的名称空间,您必须右键单击库并选择“Add as library”选项,并选择所需的类型。

Remember the "Project structure" option is not doing any auto changes in "build.gradle" in the current version of Android Studio (0.2.3). Maybe this feature will be available in the next versions.

记住,“项目结构”选项不是在“构建”中执行任何自动更改。Android Studio的当前版本(0.2.3)。也许在下一个版本中可以使用这个特性。

#8


11  

Option 1: Drop Files Into Project's libs/directory

The relevant build.gradle file will then update automatically.

相关的构建。渐变文件将自动更新。

Option 2: Modify build.gradle File Manually

Open your build.gradle file and add a new build rule to the dependencies closure. For example, if you wanted to add Google Play Services, your project's dependencies section would look something like this:

打开您的构建。渐变文件,并向依赖项闭包添加新的构建规则。例如,如果您想添加谷歌Play服务,您的项目依赖项部分将如下所示:

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.google.android.gms:play-services:6.5.+'
   }

Option 3: Use Android Studio's User Interface

In the Project panel, Control + click the module you want to add the dependency to and select Open Module Settings.

在项目面板中,Control +单击要添加依赖项的模块,并选择Open module设置。

如何向Android Studio添加一个库项目?

Select the Dependencies tab, followed by the + button in the bottom-left corner. You can choose from the following list of options:

选择Dependencies选项卡,然后是左下角的+按钮。你可以从以下选项列表中选择:

  • Library Dependency
  • 库的依赖
  • File Dependency
  • 文件的依赖
  • Module Dependency
  • 模块依赖关系

You can then enter more information about the dependency you want to add to your project. For example, if you choose Library Dependency, Android Studio displays a list of libraries for you to choose from.

然后,您可以输入关于要添加到项目中的依赖项的更多信息。例如,如果您选择库依赖项,Android Studio将显示一个库列表供您选择。

Once you've added your dependency, check your module-level build.gradle file. It should have automatically updated to include the new dependency.

一旦添加了依赖项,请检查模块级的构建。gradle文件。它应该自动更新以包含新的依赖项。

Source

#9


8  

If you need access to the resources of a library project (as you do with ABS) ensure that you add the library project/module as a "Module Dependency" instead of a "Library".

如果您需要访问库项目的资源(就像您使用ABS一样),请确保将库项目/模块添加为“模块依赖项”,而不是“库”。

#10


8  

This is how it works for me in Android Studio 1.5

In the project where you want to add external library project, go to menu File -> New -> *Import new Module**, navigate to the library project which you want to add to your project, select to add 'library' module in your project. You will get settings.gradle in your projects, beside app, included library, something like this:

在要添加外部库项目的项目中,转到菜单文件> New -> *导入新模块**,导航到要添加到项目的库项目,选择在项目中添加“library”模块。你会得到设置。在你的项目中,除了应用程序,包括图书馆,类似这样的东西:

include ':app', ':library'

Add in build.gradle(Module :app) in the dependencies section:

添加在build。依赖项部分的等级(模块:app):

Compile project(':library')

编译项目(“:库”)

Rebuild the project, and that's it.

重建项目,就是这样。

*You can add as many libraries (modules) as you want. In that case in settings.gradle you will have:

您可以添加任意数量的库(模块)。在这种情况下,在设置中。gradle你会:

 include ':app', ':lib1', ':lib2', ...

And in build.gradle, you'll need to have:

在构建。格蕾尔,你需要:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Some other dependencies...

    compile project(':lib1')
    compile project(':lib2')
    ...
}

#11


6  

  1. Press F4 to show Project Structure, click libraries or Global libraries, and click + to add the JAR file.
  2. 按F4显示项目结构,点击库或全局库,点击+添加JAR文件。
  3. Click Modules what you want add jar, select the Dependencies tab, click +, and add Library.
  4. 单击要添加jar的模块,选择Dependencies选项卡,单击+,并添加库。

#12


6  

You can do this easily. Go to menu File -> New -> Import Module...:

这很容易做到。进入菜单文件->新->导入模块…:

如何向Android Studio添加一个库项目?

Browse for the directory which contains the module. Click Finish:

浏览包含模块的目录。单击Finish。

如何向Android Studio添加一个库项目?

Go to Project Structure and add Module Dependency:

进入项目结构并添加模块依赖项:

如何向Android Studio添加一个库项目?

Note: If you receive an SDK error, update that one.

注意:如果您收到一个SDK错误,请更新它。

#13


5  

To add to the answer: If the IDE doesn't show any error, but when you try to compile, you get something like:

为了补充答案:如果IDE没有显示任何错误,但是当您尝试编译时,您将得到如下内容:

No resource found that matches the given name 'Theme.Sherlock.Light'

Your library project is probably compiled as an application project. To change this, go to:

您的库项目可能被编译为应用程序项目。要改变这一点,可以去:

Menu File -> Project structure -> Facets -> [Library name] -> Check "Library module".

菜单文件->项目结构-> facet ->[库名]->检查“库模块”。

#14


5  

Editing library dependencies through the GUI is not advisable as that doesn't write those changes to your build.gradle file. So your project will not build from the command-line. We should edit the build.gradle file directly as follows.

通过GUI编辑库依赖性是不可取的,因为它不会将这些更改写入您的构建。gradle文件。因此,您的项目不会从命令行构建。我们应该编辑构建。等级文件直接如下。

For instance, given to following structure:

例如,给出以下结构:

MyProject/

MyProject的/

  • app/
  • app /
  • libraries/
    • lib1/
    • lib1 /
    • lib2/
    • lib2 /
  • 库/ lib1 / lib2 /

We can identify three projects. Gradle will reference them with the following names:

我们可以确定三个项目。Gradle将会用以下的名字引用它们:

  1. :app
  2. :应用程序
  3. :libraries:lib1
  4. 库:lib1
  5. :libraries:lib2
  6. 库:lib2

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

app项目可能依赖于库,通过声明以下依赖项来实现:

dependencies {
    compile project(':libraries:lib1')
}

#15


3  

After importing the ABS Module (from File > Project Structure) and making sure it has Android 2.2 and Support Library v4 as dependencies, I was still getting the following error as you @Alex

在导入ABS模块(来自文件>项目结构)并确保它有Android 2.2并支持Library v4作为依赖后,我仍然会得到以下错误,如您@Alex

Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.DarkActionBar'

I added the newly imported module as a dependency to my main app module and that fixed the problem.

我将新导入的模块作为主应用程序模块的依赖项添加到主应用程序模块中,解决了这个问题。

#16


3  

To resolve this problem, you just need to add the abs resource path to your project build file, just like below:

要解决这个问题,只需将abs资源路径添加到项目构建文件中,如下所示:

sourceSets {
    main {
        res.srcDirs = ['src/main/res','../../ActionBarSherlock/actionbarsherlock/res']
    }
}

So, I again compile without any errors.

因此,我再次编译,没有任何错误。

#17


3  

First Way This is working for MacBook.

第一种方法是在MacBook上运行。

First select your builder.gradle file as given screen:

首先选择您的建设者。给定屏幕的渐变文件:

如何向Android Studio添加一个库项目?

Add dependencies like as on the selected screen:

添加依赖项,如在选定的屏幕上:

如何向Android Studio添加一个库项目?

Select sync project.

选择同步的项目。

If you are getting an error like "Project with path':signature-pad' could not be found in project ':app'", then please use the second way:

如果你有一个类似于“path”的错误:在项目“app”中找不到“签字人”,那么请使用第二种方式:

Select menu File -> New -> Import Module...:

选择菜单文件->新->导入模块…:

如何向Android Studio添加一个库项目?

After clicking on Import Module,

点击导入模块后,

如何向Android Studio添加一个库项目?

give the path of library like as my MacBook path:

给出像我的MacBook path这样的库路径:

如何向Android Studio添加一个库项目?

Click on Finish. Now your library are added.

点击Finish。现在添加了库。

#18


2  

If you have Android Studio .0.4.0, you can create a new folder in your build path, YourApp/libraries. Copy the JAR file. There in, right click on it and "Add As Library". Now you have a popup. Just select your directory and press OK, and that's it.

如果你有Android Studio .0.4.0,你可以在你的构建路径,app /库中创建一个新的文件夹。复制JAR文件。在那里,右键单击它并“添加为库”。现在你有一个弹出窗口。选择你的目录并按下OK,就这样了。

#19


2  

https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0 is the Dropbox link of how to add a JAR file and library project in the latest version of Android Studio 1.0.1.

https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0是如何在最新版本的Android Studio 1.0.1中添加JAR文件和库项目的Dropbox链接。

Please see the documentation with screenshots. It's very easy for a new user.

请查看带有屏幕截图的文档。对于一个新用户来说很容易。

#20


1  

I found the solution. It's so simple. Follow froger_mcs instructions.

我找到了解决方案。它是如此简单。遵循froger_mcs指令。

Make sure that you make the src folder a Source folder in Project Structure -> Modules (Sources).

确保将src文件夹设置为项目结构中的源文件夹——>模块(Source)。

如何向Android Studio添加一个库项目?

#21


1  

An example of succesfully adding another library (PullToRefresh). Also works for ABS libproject.

成功添加另一个库(PullToRefresh)的示例。同样适用于ABS libproject。

This SO Question

这样的问题

This post

这篇文章

#22


1  

Basically, you can include your JAR files in three different ways. The last one is remote library that is using https://bintray.com/ jcenter online repository. But, if you do it in one of the two other ways, the JAR file will be included physically in your project. Please read this link https://*.com/a/35369267/5475941 for more information. In this post I explained how to import your JAR file in Android studio and I explained all possible ways.

基本上,您可以用三种不同的方式包含JAR文件。最后一个是使用https://bintray.com/ jcenter在线存储库的远程库。但是,如果您在其他两种方法中使用它,JAR文件将会包含在您的项目中。请阅读这个链接https://*.com/a/35369267/5475941了解更多信息。在这篇文章中,我解释了如何在Android studio中导入JAR文件,并解释了所有可能的方法。

In summary, if it is like this (local address), they are downloaded and these JAR files are physically in the project:

总之,如果是这样(本地地址),就会被下载,这些JAR文件在项目中是物理的:

如何向Android Studio添加一个库项目?

But, if it is an internet address like this, they are remote libraries (bintray.com jcenter part) and they will be used remotely:

但是,如果它是像这样的互联网地址,它们是远程库(bintray.com jcenter部分),它们将被远程使用:

如何向Android Studio添加一个库项目?

I hope it helps.

我希望它有帮助。

#23


1  

Open the build gradle module app file and add your dependency. If you download the library, just import and build as gradle.

打开build gradle模块应用程序文件并添加您的依赖项。如果您下载了库,只需导入并构建为level。

Otherwise add repositories in side gradle module app:

另外,在侧级模块app中添加存储库:

repositories {
        maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}

The first repositories will download the library for you.

第一个存储库将为您下载该库。

And compile the downloaded library:

并编译下载的库:

 compile ('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT@aar') {
        transitive = true
    }

If you are creating a library, you just need to import the project as import new module.

如果您正在创建一个库,您只需要将项目作为导入新模块导入即可。

#24


1  

In Android Studio, go to inside app folder, and open build.gradle file. Here you will see dependencies{}. Inside it you can add the library project and synchronise. Now after synchronising the library it will be added to your project, and you can use its functions and classes in your project.

在Android Studio中,进入应用程序文件夹,打开build。gradle文件。这里您将看到dependencies{}。在其中可以添加库项目并进行同步。现在,在同步库之后,它将被添加到项目中,您可以在项目中使用它的函数和类。

#25


1  

For Android Studio:

为Android工作室:

如何向Android Studio添加一个库项目?

Click on Build.gradle (module: app).

点击构建。gradle(模块:应用程序)。

And add for

和添加

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/commons-io-2.4.jar')
}

and in your directory "app", create a directory, "libs". Add the file yourfile.jar:

在你的“app”目录中,创建一个“libs”目录。添加文件yourfile.jar:

如何向Android Studio添加一个库项目?

Finally, compile the Gradle Files:

最后,编写等级文件:

如何向Android Studio添加一个库项目?

#26


1  

Simply import the Android library project as a module and in Build.gradle.

简单地将Android库项目作为一个模块导入到Build.gradle中。

Apply plugin: 'com.android.library'

应用插件:“com.android.library”

After that, follow these steps:

然后,遵循以下步骤:

  1. Right click on Module & select open Module settings
  2. 右键单击模块并选择打开模块设置
  3. Select dependencies, click on +, select library dependencies, and add the previously imported module.
  4. 选择依赖项,单击+,选择库依赖项,并添加先前导入的模块。

#27


1  

I had a different cause of the problem so for people:

我有不同的原因导致这个问题

repositories {
    mavenCentral()
}

change mavenCentral() to jcenter() and add

将mavenCentral()更改为jcenter()并添加

allprojects {
    repositories {
        jcenter()
    }
}

#28


1  

Android Studio 3.0

Just add the library name to the dependencies block of your app's build.gradle file.

只需将库名添加到应用程序构建的依赖项块中。gradle文件。

dependencies {
    // ...
    implementation 'com.example:some-library:1.0.0'
}

Note that you should use implementation rather than compile now. This is new with Android Studio 3.0. See this Q&A for an explanation of the difference.

注意,您应该使用实现而不是编译。这是Android Studio 3.0的新版本。请参看这个问答,以解释其中的不同之处。

#29


0  

I also encountered the same problem then I did following things.

我也遇到了同样的问题,然后我做了以下的事情。

  1. I import the library project into my AndroidStudio IDE as a module using menu File -> Import module menus

    我使用菜单文件—>导入模块菜单将库项目导入到AndroidStudio IDE中

  2. Then I went to my main module in which I want the library project as a dependent project

    然后我进入了我的主要模块,我希望将库项目作为一个独立的项目。

  3. Right click on the main module (in my case its name is app) -> open module setting -> go into dependencies tab -> click on + button (you will get it on right side of window) -> click on module dependency -> select your library project from list

    右键单击主模块(在我的例子中,它的名字是app) ->打开模块设置->进入dependencies选项卡->单击+按钮(在窗口的右边)->单击模块依赖项->从列表中选择您的库项目

Apply the changes and click the OK button.

应用更改并单击OK按钮。

It worked for me. I hope it will help others too.

它为我工作。我希望它也能帮助别人。

#30


-2  

Indeed as versions are changing, so is changing the user interface and options available on the menu. After reading most of the answers to these questions I had to guess what would work for Android Studio 1.1.0.

实际上,随着版本的变化,用户界面和菜单上的选项也在变化。在阅读了这些问题的大部分答案后,我不得不猜测Android Studio 1.1.0会有什么效果。

  1. With your mouse, select the project at the main level (this is where it shows the name of your app).

    使用鼠标,在主级别上选择项目(这是显示应用程序名称的地方)。

  2. Right click, and select the menu options New, Folder, Assets Folder.

    右键单击,并选择菜单选项New、Folder、Assets文件夹。

  3. After creating the assets folder, paste or copy in it, whatever JAR file you need for your library.

    在创建了assets文件夹之后,在其中粘贴或复制任何您需要的JAR文件。

  4. From Android Studio's main menu (top of the screen) select File -> Project Structure.

    从Android Studio的主菜单(屏幕顶部)选择File ->项目结构。

  5. Then select your project name and go to the Dependencies tab.

    然后选择项目名称并转到Dependencies选项卡。

  6. Click on the plus sign (+) on the lower left of the dialog box and select file dependency.

    单击对话框左下方的加号(+)并选择file dependency。

  7. Finally open the recently created assets folder, select the JAR files that you copied, and then click apply and OK.

    最后打开最近创建的assets文件夹,选择复制的JAR文件,然后单击apply和OK。

Clean and rebuild your project.

清理和重建你的项目。