在 Android Studio 中添加外部库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25610727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:34:13  来源:igfitidea点击:

Adding external library in Android studio

androidandroid-studioandroid-library

提问by Marian Pa?dzioch

I want to add external library https://github.com/foursquare/foursquare-android-oauthto my Android application (I use Android Studio, the instructions provided by lib author for Eclipse didn't work for Android Studio).

我想将外部库https://github.com/foursquare/foursquare-android-oauth添加到我的 Android 应用程序中(我使用 Android Studio,lib 作者为 Eclipse 提供的说明不适用于 Android Studio)。

I've tried to do it with maven, so in File->Project Structure->Dependencies I've added com.foursquare:foursquare-android-nativeoauth-lib:1.0.0but Gradle Sync fails:

我试过用 maven 来做,所以在 File->Project Structure->Dependencies 我添加了com.foursquare:foursquare-android-nativeoauth-lib:1.0.0但 Gradle Sync 失败:

Error:Failed to find: com.foursquare:foursquare-android-nativeoauth-lib:1.0.0

When I try to build my app (without fixing above error becaus I don't know how) I get:

当我尝试构建我的应用程序时(没有修复上述错误,因为我不知道如何)我得到:

Error:A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not resolve com.foursquare:foursquare-android-nativeoauth-lib:1.0.0.
 Required by:
    ForSquaresOnly:app:unspecified
   > Could not parse POM http://jcenter.bintray.com/com/foursquare/foursquare-android-nativeoauth-lib/1.0.0/foursquare-android-nativeoauth-lib-1.0.0.pom
     > Could not find any version that matches com.foursquare:parent:1.0.0.

Any other way to import this lib? I can simply copy-paste source code into my source or create JAR out of it?

还有其他方法可以导入这个库吗?我可以简单地将源代码复制粘贴到我的源代码中或从中创建 JAR?

BTW: if you run into problems see this question (I had this issue after importing): Manifest merger failed : uses-sdk:minSdkVersion 14

顺便说一句:如果遇到问题,请参阅此问题(导入后遇到此问题):清单合并失败:使用-sdk:minSdkVersion 14

采纳答案by malcubierre

Try this:

尝试这个:

File> Project Structure> Dependencies Tab> Add module dependency(scope = compile)

File> Project Structure> Dependencies Tab> Add module dependency( scope = compile)

Where the module dependency is the project library Android folder.

其中模块依赖项是项目库 Android 文件夹。

回答by Mohammad

Try one of these approaches:

尝试以下方法之一:

Approach 1)

方法 1)

1- Choose project view

1- 选择项目视图

enter image description here

在此处输入图片说明

2- Copy your JAR file in app -> lib folder

2- 在 app -> lib 文件夹中复制您的 JAR 文件

enter image description here

在此处输入图片说明

3- Right click on your JAR file and choose add as library

3-右键单击您的 JAR 文件并选择添加为库

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

4- Check it in build.gradle

4- 在 build.gradle 中检查它

enter image description here

在此处输入图片说明



Approach 2)

方法 2)

1- File -> New -> New Module

1-文件->新建->新模块

enter image description here

在此处输入图片说明

2- Import .JAR/.AAR Package

2- 导入 .JAR/.AAR 包

enter image description here

在此处输入图片说明

3- Browse your JAR File

3- 浏览您的 JAR 文件

enter image description here

在此处输入图片说明

4- Finish

4-完成

5- File -> Project Structure -> Dependencies

5-文件->项目结构->依赖

enter image description here

在此处输入图片说明

6- You should click on + button and then click on Module Dependency

6- 您应该单击 + 按钮,然后单击 Module Dependency

enter image description here

在此处输入图片说明

7- You will see your library here

7- 你会在这里看到你的图书馆

enter image description here

在此处输入图片说明

8- choose your library and click ok

8-选择您的图书馆并单击确定

enter image description here

在此处输入图片说明

9- Then, you will see that your library is added.

9- 然后,您将看到您的库已添加。

enter image description here

在此处输入图片说明



For first two approaches, you need a JAR file. You can search http://search.maven.org/to find JAR files that are related to Android. For example, this is the search result for jdom in this link

对于前两种方法,您需要一个 JAR 文件。您可以搜索http://search.maven.org/以查找与 Android 相关的 JAR 文件。比如这个链接中jdom的搜索结果

Search result for jdom

jdom 的搜索结果



Approach 3)Android is using http://jcenter.bintray.com/as remote library. For example, this is the search result for jdom in the link.

方法 3)Android 使用http://jcenter.bintray.com/作为远程库。例如,这是链接中 jdom 的搜索结果。

jcenter jdom

jcenter jdom

To add a library in this approach, please follow these steps:

要以这种方式添加库,请按照以下步骤操作:

1- File -> Project Structure -> Dependencies

1-文件->项目结构->依赖

enter image description here

在此处输入图片说明

2- Click on + button and choose library dependency

2- 单击 + 按钮并选择库依赖项

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

3- find your library and select it, then click OK.

3- 找到您的库并选择它,然后单击确定。



I hope it helps.

我希望它有帮助。

回答by Pedro Santos

To reference an external lib project without copy, just do this: - Insert this 2 lines on setting.gradle:

要在不复制的情况下引用外部 lib 项目,只需执行以下操作: - 在以下位置插入 2 行setting.gradle

  include ':your-lib-name'
  project(':your-lib-name').projectDir = new File('/path-to-your-lib/your-lib-name)

Insert this line on on dependencies part of build.gradlefile:

build.gradle文件的依赖项部分插入这一行:

compile project(':your-lib-name')

Sync project

同步项目

回答by Sudhanshu Gaur

There are two simplest ways if one does not work please try the other one.

有两种最简单的方法,如果一种方法不起作用,请尝试另一种。

  1. Add dependency of the library inside dependency inside build.gradlefile of the library you are using, and paste your library in External Libraries.
  1. build.gradle在您正在使用的库的文件内的依赖项内添加库的依赖项,并将您的库粘贴到外部库中。

OR

或者

  1. Just Go to your libsfolder inside appfolder and paste all your .jare.g Library files there, Now the trick here is that now go inside settings.gradlefile now add this line include ':app:libs'after include ':app'it will definitely work.
  1. 只需转到libs文件夹内的app文件夹并将所有.jar例如库文件粘贴到那里,现在这里的技巧是现在进入settings.gradle文件内部,include ':app:libs'include ':app'它肯定会工作之后添加这一行。

回答by KnowIT

Any other way to import this lib? I can simply copy-paste source code into my source or create JAR out of it?

还有其他方法可以导入这个库吗?我可以简单地将源代码复制粘贴到我的源代码中或从中创建 JAR?

Complete Steps for importing a library in Android Studio 1.1

在 Android Studio 1.1 中导入库的完整步骤

  1. Goto File -> Import Module.
  2. Source Directory -> Browse the project path.
  3. Specify the Module Name
  4. Open build.gradle (Module:app) file
  5. Add the following line with your module name

    compile project(':internal_project_name')

  1. 转到文件 -> 导入模块。
  2. 源目录 -> 浏览项目路径。
  3. 指定模块名称
  4. 打开 build.gradle (Module:app) 文件
  5. 添加以下行与您的模块名称

    编译项目(':internal_project_name')

Taken from: how to add library in Android Studio

摘自:如何在 Android Studio 中添加库

回答by Ratna Halder

I had also faced this problem. Those time I followed some steps like:

我也遇到过这个问题。那些时候我遵循了一些步骤,例如:

  1. File> New> Import module> select your library_project. Then include 'library_project'will be added in settings.gradlefile.

  2. File> Project Structure> App> Dependencies Tab> select library_project. If library_projectnot displaying then, Click on +button then select your library_project.

  3. Clean and build your project. The following lines will be added in your app module build.gradle(hint: this is not the one where classpathis defined).

  1. File> New> Import module> select your library_project。然后include 'library_project'将被添加到settings.gradle文件中。

  2. File> Project Structure> App> Dependencies Tab> select library_project。如果library_project没有显示,请单击+按钮,然后选择您的library_project.

  3. 清理并构建您的项目。以下几行将添加到您的应用程序模块中build.gradle提示:这不是classpath定义的地方)。

 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile project(':library_project')
 }
  1. If these lines are not present, you must add them manually and clean and rebuild your project again (Ctrl + F9).

  2. A folder named library_projectwill be created in your app folder.

  3. If any icon or task merging error is created, go to AndroidManifestfile and add <application tools:replace="icon,label,theme">

  1. 如果这些行不存在,您必须手动添加它们并再次清理和重建您的项目 ( Ctrl + F9)。

  2. library_project将在您的应用程序文件夹中创建一个名为的文件夹。

  3. 如果创建任何图标或任务合并错误,请转到AndroidManifest文件并添加<application tools:replace="icon,label,theme">

回答by capt.swag

A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.

一个迟到的答案,虽然我想对这个问题给出一个深入的答案。此方法适用于Android Studio 1.0.0 及以上版本。

STEPS

脚步

  1. First switch your folder structure from Android to Project.
  1. 首先将您的文件夹结构从 Android 切换到Project

enter image description here

在此处输入图片说明

  1. Now search for the libs folderinside app - build folder.
  1. 现在在 app - build 文件夹中搜索libs 文件夹。

enter image description here

在此处输入图片说明

  1. Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].
  1. 将 .jar 文件粘贴到 libs 文件夹中后。右键单击 jar 文件,最后单击Add as library。这将负责在 build.gradle 中添加编译文件('libs/library_name.jar')[您不必在构建文件中手动输入它]。

enter image description here

在此处输入图片说明

Now you can start using the library in your project.

现在您可以开始在您的项目中使用该库。

回答by vinod

Three ways in android studio for adding a external library.

android studio 中添加外部库的三种方式。

  1. if you want to add libarary project dependency in your project :

    A. In file menu click new and choose import module choose your library project path and click ok, library project automatically add in your android studio project .

    B. Now open your main module(like app) gradle file and add project dependency in dependency section dependencies {

    compile project(':library project name')

  2. if you want to add jar file : A. add jar file in libs folder. B. And Add dependency

    compile fileTree(dir: 'libs', include: '*.jar') // add all jar file from libs folder, if you want to add particular jar from libs add below dependency.

    compile files('libs/abc.jar')

  3. Add Dependency from url (recommended). like

    compile 'com.mcxiaoke.volley:library-aar:1.0.0'

  1. 如果要在项目中添加库项目依赖项:

    A. 在文件菜单中单击新建并选择导入模块选择您的库项目路径并单击确定,库项目会自动添加到您的 android studio 项目中。

    B. 现在打开你的主模块(如 app)gradle 文件并在依赖项中添加项目依赖项dependencies {

    编译项目(':库项目名称')

  2. 如果要添加 jar 文件: A. 在 libs 文件夹中添加 jar 文件。B. 并添加依赖

    compile fileTree(dir: 'libs', include: '*.jar') // 从 libs 文件夹添加所有 jar 文件,如果你想从 libs 添加特定的 jar 添加下面的依赖项。

    编译文件('libs/abc.jar')

  3. 从 url 添加依赖项(推荐)。喜欢

    编译'com.mcxiaoke.volley:library-aar:1.0.0'

回答by kunal khedkar

For the simplest way just follow these steps

对于最简单的方法,只需按照以下步骤操作

  1. Go to File -> New -> Import Module -> <choose library or project folder>
  2. Add library to include section in settings.gradlefile and sync the project (After that you can see new folder with library name is added in project structure)

    include ':mylibraryName'

  3. Go to File -> Project Structure -> app -> dependency tab -> click on plus button

  4. Select module dependency -> select library (your library name should appear there) and put scope (compile or implementation)
  5. Add this line in build.gradle in app level module in dependency section

    implementation project(':mylibraryName')

  1. 转到文件 -> 新建 -> 导入模块 -> <选择库或项目文件夹>
  2. 将库添加到settings.gradle文件中的包含部分并同步项目(之后您可以看到在项目结构中添加了带有库名称的新文件夹)

    include ':mylibraryName'

  3. 转到文件 -> 项目结构 -> 应用程序 -> 依赖项选项卡 -> 单击加号按钮

  4. 选择模块依赖 -> 选择库(你的库名应该出现在那里)并放置范围(编译或实现)
  5. 在依赖部分的应用程序级别模块的 build.gradle 中添加此行

    implementation project(':mylibraryName')

回答by Andrejs

If the library you need is on GitHub then adding it to Android Studio is easy with JitPack.

如果您需要的库在 GitHub 上,那么使用JitPack将其添加到 Android Studio 很容易。

Step 1. Add the jitpack repository to build.gradle:

步骤 1. 将 jitpack 存储库添加到 build.gradle:

allprojects { 
  repositories {
    jcenter()
    maven { url "https://jitpack.io" }
  }
}

Step 2. Add the GitHub repository as a dependency:

步骤 2. 添加 GitHub 存储库作为依赖项:

dependencies {
    // ...
    compile 'com.github.Username:LibraryRepo:ReleaseTag'
}

JitPack acts as a maven repository and can be used much like Maven Central. The nice thing is that the maintainers don't have to upload the library. Behind the scenes JitPack will check out the code from GitHub and compile it. Therefore for this to work there needs to be a working build file in the git repository.

JitPack 充当 Maven 存储库,可以像 Maven Central 一样使用。好消息是维护人员不必上传库。在幕后,JitPack 将检查来自 GitHub 的代码并编译它。因此,要使其正常工作,git 存储库中需要有一个工作构建文件。

There is also a guideon how to publish an Android library.

还有一个关于如何发布 Android 库的指南。