481

I've been experimenting with the new android build system and I've run into a small issue. I've compiled my own aar package of ActionBarSherlock which I've called 'actionbarsherlock.aar'. What I'm trying to do is actually use this aar to build my final APK. If I include the whole ActionBarSherlock library as an android-library module to my main project using compile project (':actionbarsherlock') I'm able to build successfully without any problems.

But my problem is that I want to provide that dependency as a aar file package MANUALLY just if I would a JAR then I can't seem to figure out how to properly include it into my project. I've attempted to use the compile configuration but this doesn't seem to work. I keep on getting cannot find symbol during compile which tells me that the classes.jar from aar package isn't getting included in the classpath.

Does anyone know of the syntax to manually include an aar package as a file?

build.gradle

buildscript {

 repositories {
     mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.4'
  }
}
apply plugin: 'android'

repositories {
   mavenCentral()
}
dependencies {
    compile files('libs/actionbarsherlock.aar')
}

android {
    compileSdkVersion 15
    buildToolsVersion "17.0"
}

EDIT: So the answer is that it's not currently supported, here's the issue if you want to track it.

EDIT: Currently as this is still not supported directly the best alternative seems to be the proposed solution from @RanWakshlak

EDIT: Also simpler by using the syntax proposed by @VipulShah

4

25 回答 25

752

请按照以下步骤使其正常工作(我已将其测试到 Android Studio 2.2)

假设您将 aar 文件保存在 libs 文件夹中。(假设文件名是cards.aar

然后在应用程序中build.gradle指定以下内容并单击与 Gradle 文件同步项目。打开项目级别 build.gradle 并flatDir{dirs 'libs'}像下面一样添加

allprojects {
   repositories {
      jcenter()
      flatDir {
        dirs 'libs'
      }
   }
}

现在打开应用程序级 build.grdle 文件并添加.aar文件

    dependencies {
       implementation(name:'cards', ext:'aar')
}

如果一切顺利,您将看到在 build -> exploded-aar 中创建了库条目

另请注意,如果您从另一个具有依赖项的项目中导入 .aar 文件,您也需要在 build.gradle 中包含这些文件。

于 2014-04-27T17:11:27.153 回答
268
  1. 右键单击您的项目并选择“打开模块设置”

打开模块设置

  1. 单击窗口左上角的“+”按钮以添加新模块。

添加新模块

  1. 选择“导入 .JAR 或 .AAR 包”并单击“下一步”按钮。

导入 AAR

  1. 使用“文件名”字段旁边的省略号按钮“...”查找 AAR 文件。

查找 AAR 文件

  1. 保持选中应用程序的模块,然后单击“依赖项”窗格以将新模块添加为依赖项。

依赖项窗格

  1. 使用依赖项屏幕的“+”按钮并选择“模块依赖项”

添加模块依赖

  1. 选择模块并单击“确定”

选择模块

编辑:屏幕截图 6 中的模块依赖项已在 Android Studio 4.1 中删除。作为替代方案,将模块依赖项添加到build.gradle

dependencies {
    implementation project(':your_module')
}

编辑:Android Studio 4.2 中的用户界面和工作流程发生了很大变化。现在在官方文档中很好地解释了添加依赖项的过程:使用项目结构对话框添加依赖项

于 2016-01-21T09:28:43.340 回答
130

您可以从存储库引用 aar 文件。maven 是一个选项,但有一个更简单的解决方案:将 aar 文件放在您的 libs 目录中并添加一个目录存储库。

    repositories {
      mavenCentral()
      flatDir {
        dirs 'libs'
      }
    }

然后在依赖部分引用库:

  dependencies {
        implementation 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
}

您可以查看民安博文了解更多信息。

于 2014-01-30T08:04:14.073 回答
71

之前(默认)

implementation fileTree(include: ['*.jar'], dir: 'libs')

只需添加数组'*.aar'include

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

它在 Android Studio 3.x 上运行良好。

如果你想忽略一些图书馆?这样做。

implementation fileTree(include: ['*.jar', '*.aar'], exclude: 'test_aar*', dir: 'libs')
debugImplementation files('libs/test_aar-debug.aar')
releaseImplementation files('libs/test_aar-release.aar')
于 2018-04-05T02:13:57.353 回答
54

以下方法适用于 Android Studio v0.8.x:

  • 将文件保存aar在 app 模块的libs文件夹下(例如<project>/<app>/libs/myaar.aar:)

  • 将以下内容添加到您的“app”模块文件夹的 build.gradle(不是您的项目根 build.gradle)。注意编译行中的名称,它是 myaar@aar 而不是 myaar.aar。

     dependencies {
         compile 'package.name.of.your.aar:myaar@aar'
     }
    
     repositories{
         flatDir{
             dirs 'libs'
         }
     }
    
  • 点击Tools -> Android -> Sync Project with Gradle Files

于 2014-10-07T03:28:11.883 回答
28

使用 Android Studio 3.4 和 Gradle 5,您可以像这样简单地做到这一点

dependencies {
    implementation files('libs/actionbarsherlock.aar')
}
于 2019-05-21T15:35:12.600 回答
24

在应用级 build.gradle 中添加以下行

  implementation fileTree(dir: "libs", include: ["*.aar"])

将项目结构从 Android 更改为 Project。

导航到app-> libs,如下所示

在此处输入图像描述

然后将“aar”粘贴到 libs 文件夹中。

单击android studio 左上角的File ,然后单击“Sync Project with Gradle Files”,如下所示。

在此处输入图像描述

而已。

于 2020-11-13T10:39:01.470 回答
21

我刚刚成功了!

  1. mylib-0.1.aar文件复制到libs/文件夹中

  2. 将这些行添加到build.gradle的底部(应该是应用程序,而不是项目):

    repositories {
       flatDir {
           dirs 'libs'
       }
    }
    dependencies {
        compile 'com.example.lib:mylib:0.1@aar'
    }
    
  3. 到目前为止,一切都很好。最重要的一点来了:

除非启用离线模式,否则 Gradle 需要访问网络以获取依赖项。

确保您已通过Project Structures/Gradle中的复选框启用离线工作

- 或者 -

配置代理设置以访问网络。

要配置代理设置,您必须修改项目的gradle.properties文件,分别配置 http 和 https,如下所示:

systemProp.http.proxyHost=proxy.example.com
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=user
systemProp.http.proxyPassword=pass
systemProp.http.nonProxyHosts=localhost
systemProp.http.auth.ntlm.domain=example <for NT auth>

systemProp.https.proxyHost=proxy.example.com
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=user
systemProp.https.proxyPassword=pass
systemProp.https.nonProxyHosts=localhost
systemProp.https.auth.ntlm.domain=example <for NT auth>

希望这有效。

于 2015-03-02T14:32:59.050 回答
21

有2种方式:

第一种方式

  1. 打开您的 Android Studio 并通过以下方式导航到Create New Module窗口File -> New -> New Module

在此处输入图像描述

  1. 选择Import .JAR/.AAR Package项目并单击Next按钮

  2. build.gradle在属于您的app模块的文件中添加依赖项。

    dependencies {
        ...
        implementation project(path: ':your aar lib name')
    }

就这样。

第二种方式

  1. 在目录中创建一个文件夹libs,例如aars.

  2. 将您的 aar 库放入 aars 文件夹。

  3. 添加代码片段

repositories {
    flatDir {
        dirs 'libs/aars'
    }
}

进入您的build.gradle文件属于应用程序模块。

  1. build.gradle在属于您的app模块的文件中添加依赖项。
dependencies {
    ...
    implementation (name:'your aar lib name', ext:'aar')
}

就这样。

如果你会看中文,可以查看博客什么是AAR文件以及如何在Android开发中使用

于 2018-03-23T08:08:15.713 回答
20

目前不支持引用本地 .aar 文件(由 Xavier Ducrochet 确认)

您可以做的是建立一个本地 Maven 存储库(比听起来简单得多)并从那里引用 .aar。

我写了一篇博文,详细说明了如何让它在这里工作:

http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds

于 2013-06-01T16:56:54.053 回答
19

aar您只需几行代码即可添加多个依赖项。

添加本地flatDir仓库:

repositories {
    flatDir {
        dirs 'libs'
    }
} 

将目录中的每个添加到aar依赖项配置:libscompile

fileTree(dir: 'libs', include: '**/*.aar')
        .each { File file ->
    dependencies.add("compile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
}
于 2015-07-27T19:52:38.557 回答
15

不幸的是,这里没有一个解决方案对我有用(我得到未解决的依赖项)。最终奏效且最简单的方法恕我直言:突出显示 Android Studio 中的项目名称,然后是 File -> New Module -> Import JAR or AAR Package。归功于这篇文章中的解决方案

于 2015-06-08T15:44:57.000 回答
13

如果你使用Gradle Kotlin DSL,你需要在你的模块目录中添加一个文件

例如:libs/someAndroidArchive.aar

只需在依赖块中的模块 build.gradle.kts 中写入以下内容:

implementation(files("libs/someAndroidArchive.aar"))
于 2020-08-19T13:13:22.790 回答
12

更新安卓工作室 3.4

  1. 转到文件->项目结构

在此处输入图像描述

  1. 模块,然后单击+

在此处输入图像描述

  1. 选择导入 .aar 包

在此处输入图像描述

  1. 查找 .aar 路由

在此处输入图像描述

  1. 完成应用,然后验证是否添加了包

在此处输入图像描述

  1. 现在在应用程序模块中,单击+Module Dependency

在此处输入图像描述

  1. 检查库包并确定

在此处输入图像描述

  1. 验证添加的依赖项

在此处输入图像描述

  1. 像这样的项目结构

在此处输入图像描述

于 2019-07-03T16:15:35.247 回答
7

我也遇到过这个问题。此问题报告:https ://code.google.com/p/android/issues/detail? id=55863 似乎暗示不支持直接引用 .AAR 文件。

也许现在的替代方法是将 actionbarsherlock 库定义为项目父目录下的 Gradle 库并相应地引用。

语法在这里定义http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Reference-a-Library

于 2013-05-29T23:07:34.407 回答
7

使用 Android Studio Arctic Fox 手动导入 AAR 文件

步骤 1. 打开项目结构
在此处输入图像描述

步骤 2. 打开项目结构
在此处输入图像描述

步骤 3. 输入文件路径
在此处输入图像描述

完毕!

于 2021-09-09T14:57:03.787 回答
4

在应用程序中导入 AAR 文件的标准方法在https://developer.android.com/studio/projects/android-library.html#AddDependency中给出

单击文件 > 新建 > 新模块。单击导入 .JAR/.AAR 包,然后单击下一步。输入已编译的 AAR 或 JAR 文件的位置,然后单击 Finish。

请参阅上面的链接以了解后续步骤。

于 2017-11-29T14:49:10.497 回答
4

就我而言,我的库中有一些依赖项,当我aar从中创建一个依赖项时,由于缺少依赖项,我失败了,所以我的解决方案是从我的库中添加所有依赖项和一个arr文件。

所以我的项目级别build.gradle看起来是这样的:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

allprojects {
    repositories {
        mavenCentral()
        //add it to be able to add depency to aar-files from libs folder in build.gradle(yoursAppModule)
        flatDir {
            dirs 'libs'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(modile app)所以:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.sampleapp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //your project depencies
    ...
    //add lib via aar-depency
    compile(name: 'aarLibFileNameHere', ext: 'aar')
    //add all its internal depencies, as arr don't have it
    ...
}

和图书馆build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //here goes library projects dependencies, which you must include
    //in yours build.gradle(modile app) too
    ...
}
于 2016-07-26T18:12:10.857 回答
3

我在 Android 问题跟踪器中找到了这个解决方法: https ://code.google.com/p/android/issues/detail?id=55863#c21

诀窍(不是修复)是将您的 .aar 文件隔离到子项目中并将您的库添加为工件:

configurations.create("default")
artifacts.add("default", file('somelib.jar'))
artifacts.add("default", file('someaar.aar'))

更多信息: 处理-传递-依赖-for-local-artifacts-jars-and-aar

于 2014-04-14T15:26:02.930 回答
3

我在这里尝试了所有解决方案,但没有一个有效,然后我意识到我犯了一个错误,我把错误的文件夹放在了.aar错误的文件夹中,如下所示,我认为我应该放在根文件夹中,所以我在libs那里创建了一个文件夹(图片中的 1 ),但是在 app 文件夹中,已经有一个libs,你应该放第二个libs,希望这对那些和我有同样问题的人有帮助:

在此处输入图像描述

于 2017-02-01T02:59:49.627 回答
2

还有另外一种方法可以做到这一点。

通常 .aar 文件不应该像我们使用 .jar 那样直接使用,因此可以避免上面提到的在 libs 文件夹中提到它并在 gradle 中声明的解决方案。

第 1 步:解压缩 .aar 文件(您可以通过将其扩展名从“.aar”重命名为“.zip”来完成此操作)

第 2 步:您很可能会在解压后的文件夹中找到 .jar 文件。复制此 .jar 文件并将其粘贴到您的模块/libs 文件夹中

第 3 步:就是这样,现在同步您的项目,您应该能够从该 .jar 访问所有类/方法/属性。您无需在任何 gradle 文件中提及它的路径/名称/存在,这是因为 gradle 构建系统在构建项目时始终查找 libs 文件夹中存在的文件

于 2021-02-11T14:24:30.220 回答
2

只是为了简化答案

如果 .aar 文件在本地存在,则包含
compile project(':project_directory')在项目的 build.gradle 的依赖项中。

如果远程存在 .aar 文件,则包含 compile 'com.*********.sdk:project_directory:0.0.1@aar'在项目的 build.gradle 的依赖项中。

于 2017-10-09T06:34:38.517 回答
0

你可以这样做:

  1. 将您的本地库(扩展名为:.jar、.aar、...)放入“libs”文件夹(或其他如果需要)。

  2. 在 build.gradle(应用程序级别)中,将此行添加到依赖项中

    实现文件树(包括:['*.jar','*.aar'],目录:'libs')

于 2018-11-29T02:37:25.200 回答
0

对我来说,这是一个关于如何配置 Android Studio 环境的问题。

当我将File-> Project Structure->更新JDK Location为更高的 Java 版本(jdk1.8.0_192.jdk- 对我来说)时,一切都开始工作了。

于 2020-08-10T11:57:13.570 回答
-1

在我的情况下,当我添加“项目”进行编译时工作:

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}


dependencies {
   compile project('com.x.x:x:1.0.0')
}
于 2018-04-20T20:21:19.253 回答