在Android studio中添加外部库

时间:2014-09-01 17:39:57

标签: android android-studio android-library

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

我已尝试使用maven,因此在File->项目结构 - >依赖关系中我已添加com.foursquare:foursquare-android-nativeoauth-lib:1.0.0但Gradle Sync失败:

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

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

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.

导入此lib的其他任何方式?我可以简单地将源代码复制粘贴到我的源代码中或从中创建JAR吗?

顺便说一句:如果遇到问题,请看这个问题(导入后我遇到了这个问题):Manifest merger failed : uses-sdk:minSdkVersion 14

20 个答案:

答案 0 :(得分:184)

尝试以下方法之一:

方法1)

1-选择项目视图

enter image description here

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

enter image description here

3-右键单击您的JAR文件,然后选择“添加为库”

enter image description here enter image description here

4-在build.gradle中检查它

enter image description here

方法2)

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

enter image description here

2-导入.JAR / .AAR包

enter image description here

3-浏览您的JAR文件

enter image description here

4-完成

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

enter image description here

6-您应该单击+按钮,然后单击模块依赖性

enter image description here

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

enter image description here

8-选择你的图书馆并点击确定

enter image description here

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

enter image description here

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

Search result for jdom

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

jcenter jdom

要使用此方法添加库,请按以下步骤操作:

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

enter image description here

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

enter image description here enter image description here

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

我希望它有所帮助。

答案 1 :(得分:85)

试试这个:

档案>项目结构> “依赖关系选项卡”>添加模块依赖项(scope = compile)

模块依赖项是项目库android文件夹

答案 2 :(得分:19)

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

  1. 在您正在使用的库的build.gradle文件中添加库内部依赖关系,并将库粘贴到外部库中。
  2. OR

    1. 只需转到libs文件夹中的app文件夹,然后将所有.jar例如库文件粘贴到那里,现在诀窍就是现在进入settings.gradle文件在include ':app:libs'之后添加此行include ':app'肯定会有效。

答案 3 :(得分:17)

要引用不带副本的外部lib项目,只需执行以下操作: - 在setting.gradle上插入这2行:

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

将此行插入build.gradle文件的依赖项部分:

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

同步项目

答案 4 :(得分:16)

  

导入此lib的其他任何方式?我可以简单地复制粘贴源代码   进入我的源码或从中创建JAR?

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

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

    编译项目(':internal_project_name')

  6. 取自:how to add library in Android Studio

答案 5 :(得分:8)

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

<强> STEPS

  1. 首先将您的文件夹结构从Android切换到项目
  2. enter image description here

    1. 现在在app - build文件夹中搜索 libs文件夹
    2. enter image description here

      1. 将.jar文件粘贴到libs文件夹中后。右键单击jar文件,最后点击添加为库。这将负责在build.gradle中添加编译文件(&#39; libs / library_name.jar&#39;)[您不必在构建文件中手动输入此内容]。
      2. enter image description here

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

答案 6 :(得分:6)

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

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

    一个。在文件菜单中单击新建,然后选择导入模块选择您的库项目路径并单击确定,库项目自动添加到您的android工作室项目中。

    B中。现在打开您的主模块(如app)gradle文件,并在依赖项部分依赖项{

    中添加项目依赖项

    编译项目(&#39;:图书馆项目名称&#39;)

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

    编译fileTree(dir:&#39; libs&#39;,include:&#39; * .jar&#39;)//从libs文件夹中添加所有jar文件,如果你想从libs添加特定的jar低于依赖性。

    编译文件(&#39; libs / abc.jar&#39;)

  3. 从网址添加依赖关系(推荐)。像

    compile&#39; com.mcxiaoke.volley:library-aar:1.0.0&#39;

答案 7 :(得分:5)

我也遇到过这个问题。那段时间我遵循了一些步骤:
1.文件 - &gt;新 - &gt;导入模块 - &gt;选择你的library_project
     (然后include 'library_project'将添加到settings.gradle文件中。)

2.文件 - &gt;项目结构 - &gt; APP-&GT;选择依赖关系选项卡 - &gt; select library_project,如果library_project没有显示,则点击+按钮,然后选择你的library_project。

3.清理并构建项目
(在那之后,

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

行将添加到项目特定的build.gradle文件中,该文件位于app文件夹或目录中(提示:不在主build.gradle文件中,其中定义了类路径))
4.如果这些线路不存在则手动添加,然后再次清理并重建项目。

5.检查应用程序文件夹中是否由library_project创建了一个文件夹。

6.如果创建了任何图标或任务合并错误,则在AndroidManifest文件中添加<application tools:replace="icon, label,theme">

答案 8 :(得分:4)

如果您需要的库位于GitHub上,那么使用JitPack可以轻松将其添加到Android Studio。

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

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

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

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

JitPack充当maven存储库,可以像Maven Central一样使用。好处是维护者不必上传库。在幕后,JitPack将检查GitHub中的代码并进行编译。因此,为了实现这一点,需要在git存储库中有一个工作的构建文件。

如何发布Android库还有a guide

答案 9 :(得分:3)

Android Studio不使用Maven,它使用Gradle,您可以打开build.gradle路由应该是:/app/build.gradle并添加显示的Gradle依赖项回购:

build.gradle有一个dependencies部分:

dependencies {
    //here add your dependency
}

回购说你需要添加这种依赖:

compile 'com.foursquare:foursquare-android-oauth:1.0.3'

只在dependencies buil.gradle上添加该行,保存文件,android将重建项目,这就是

答案 10 :(得分:3)

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

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

    include ':mylibraryName'

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

  4. 选择模块依赖项->选择库(您的库名称应显示在其中)并放置作用域(编译或实现)
  5. 在依赖项部分的应用程序级别模块的build.gradle中添加此行

    implementation project(':mylibraryName')

答案 11 :(得分:1)

Android studio 2.1

中添加
Just Go to project -> then it has some android,package ,test ,project view

只需将其更改为项目视图

即可

app->lib folder下你可以直接复制粘贴lib并做android同步。 就是这样

答案 12 :(得分:1)

1)从这里获取你的lib http://search.maven.org/

2)在app目录中创建一个libs文件夹

3)粘贴你的库

4)右键单击您的库并单击“添加为库”

5)这就是你需要做的一切!

我希望这肯定会帮助你!!!!

答案 13 :(得分:1)

通过此网站将任何github项目转换为单行gradle实现

https://jitpack.io/

例如,我需要这个项目: https://github.com/mik3y/usb-serial-for-android

我所做的就是将其粘贴到我的gradle文件中: 实现'com.github.mik3y:usb-serial-for-android:master-SNAPSHOT'

答案 14 :(得分:1)

1.Goto File -> New -> Import Module
   2.Source Directory -> Browse the project path.
   3.Specify the Module Name – it is used for internal project reference.

打开build.gradle(Module:app)文件。

 implementation project(':library')

答案 15 :(得分:0)

我遇到了同样的问题。这是因为核心库依赖性。我正在使用javax。*。这就是我要解决的问题

在文件 - &gt;项目结构 - &gt;依赖关系中,我将其添加为提供的文件,而不是编译。然后重新构建项目。

此问题在android studio升级后启动。但我认为当您尝试手动编辑构建文件时会发生这种情况。

答案 16 :(得分:0)

  1. 在项目的build.gradle的repositories部分中添加以下内容:
  2. flatDir { dirs 'aars' } 
    
    1. 在build.gradle的依赖项部分中添加以下内容:
    2.   

      编译'[group id]:[artifact id]:[version] @aar'

      用各种lib属性替换[group id]等

      1. 在项目目录

      2. 下创建一个子目录aars
      3. [youlibrary].aar复制到上面3.中创建的子目录。

      4. Build&amp;享受!

答案 17 :(得分:0)

新gradle 4.1中有一些变化

而不是编译我们应该使用实现

实施'com.android.support:appcompat-v7:26.0.0'

答案 18 :(得分:0)

<块引用>

找不到与 com.foursquare:parent:1.0.0 匹配的任何版本。

表示库开发人员在 .pom 文件中犯了错误并指定了错误的文件。

我建议使用标准方法。您可以在 .pom file 中找到 Maven Repository 并使用该版本或要求开发人员解决此问题(通过 github 问题跟踪器)

compile 'com.foursquare:foursquare-android-oauth:1.1.1'

答案 19 :(得分:-1)

在ANDROID STUDIO中添加任何库的简单步骤:

  1. 从计算机中的任何文件夹(例如C:/documents/xyz.jar“)复制有问题的文件,然后转到Android Studio并右键单击“项目库”文件夹,然后选择粘贴。

  2. 现在,右键单击新添加的库文件,然后选择“添加为库”选项

D.O.N.E