如何在库模块中添加.aar依赖?

时间:2016-01-13 11:14:00

标签: android android-studio gradle android-gradle build.gradle

我有一个库模块的.aar个文件 我想在我的其他项目的库模块中将它用作库或依赖项 我该怎么做?

我尝试了以下链接提供的选项:
http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/

仅当我在项目的应用程序模块中添加.aar引用时,它才有效。但不在图书馆模块中工作。

感谢。

5 个答案:

答案 0 :(得分:40)

按照此设置,您可以将.aar依赖项添加到库模块

build.gradle(项目:....)

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        flatDir {
            dirs 'libs'
            dirs project(':library_module').file('libs')
        }
    }
}

build.gradle(模块:应用)

dependencies {
    ...
    compile project(':library_module')
}

build.gradle(Module:library_module)

dependencies {
    ...
    compile(name:'aar_file_name', ext:'aar')
}

settings.gradle(项目设置)

include ':app', ':library_module'

答案 1 :(得分:12)

在您需要aar文件的所有模块(库或应用程序)中,您必须在build.gradle存储库中添加:

repositories {
    flatDir {
        dirs 'libs'
    }
}

并添加依赖项:

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
 }

您可以使用顶级文件添加存储库,但不能在顶级文件中添加依赖项。
请注意您在模块中使用的libs文件夹的相对路径。

答案 2 :(得分:2)

  1. 文件 - >新模块 - >导入.JAR / .AAR
  2. 导入.AAR文件。
  3. 在库模块build.gradle中添加依赖项 依赖项{compile project(&#39 ;: Name-Of-Module-aar')}
  4. http://tools.android.com/tech-docs/new-build-system/tips#TOC-Handling-transitive-dependencies-for-local-artifacts-jars-and-aar-

答案 3 :(得分:1)

我所做的与其他人在此处所做的有所不同...

我的主要目标是创建一个库模块,其中包含我需要的所有jar和aar。然后,我的主项目将依赖于此库模块-我只希望主项目的build.gradle中的一行可以链接此依赖项。

  1. 向项目添加新的库模块:File-> New-> New Module-> Android Library

  2. 在库模块中,打开build.gradle并添加:

// so that the library project can locate the aar in /libs
repositories {
    flatDir {
        dirs 'libs'
    }
}

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

现在您可以将所有jar和aar弹出到库模块的/ libs文件夹中。 奖励:无论库名是什么,都将自动发现

答案 4 :(得分:1)

抱歉的过程涉及10个步骤,但这些步骤超级简单,容易。

1-新建->模块

enter image description here

2-选择导入.jar / aar包

enter image description here

3-从位置选择.aar文件

enter image description here

4-我们可以看到模块已添加,但尚未配置应用模块。

enter image description here

5-转到项目结构

enter image description here

6-当前的项目结构就是这样

enter image description here

7-转到应用程序模块,然后按“ +”图标

enter image description here

8-选择第3个选项模块依赖性

enter image description here

9-选择新添加的.arr模块

enter image description here

10-,您可以看到与应用程序模块一起附加的新模块。现在,单击“应用”。

enter image description here

您可以看到我们很好。  enter image description here

相关问题