如何使用新的Gradle Android Build System手动包含外部aar包

这篇博客讨论了如何在新的Gradle Android Build System中手动包含aar包。作者遇到的问题是,不能直接将aar文件加入到项目依赖中,导致编译时找不到符号。通过在build.gradle中设置,如添加本地Maven仓库或使用特定语法,可以解决这个问题。文中提供了不同解决方案,包括设置目录仓库和创建子项目来管理aar文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How to manually include external aar package using new Gradle Android Build System

I've been experimenting with the new android build system and I've run into a small issue. 我一直在尝试新的Android构建系统,我遇到了一个小问题。 I've compiled my own aar package of ActionBarSherlock which I've called 'actionbarsherlock.aar'. 我已经编译了自己的ActionBarSherlock的aar包,我称之为'actionbarsherlock.aar'。 What I'm trying to do is actually use this aar to build my final APK. 我正在尝试做的实际上是使用这个aar来构建我的最终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. 如果我使用编译项目(':actionbarsherlock')将整个ActionBarSherlock库作为android-library模块包含到我的主项目中,我可以成功构建而没有任何问题。

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. 但我的问题是,我想将该依赖项作为aar文件包手动提供,如果我想要一个JAR,那么我似乎无法弄清楚如何将它正确地包含在我的项目中。 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. 我继续在编译期间找不到符号,这告诉我来自aar包的classes.jar没有包含在类路径中。

Does anyone know of the syntax to manually include an aar package as a file? 有谁知道手动将aar包作为文件包含的语法?

build.gradle 的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 编辑:目前仍然没有直接支持,最好的替代方案似乎是来自@RanWakshlak的建议解决方案

EDIT: Also simpler by using the syntax proposed by @VipulShah 编辑:使用@VipulShah提出的语法也更简单


#1楼

参考:https://stackoom.com/question/17zyB/如何使用新的Gradle-Android-Build-System手动包含外部aar包


#2楼

I've also had this problem. 我也遇到过这个问题。 This issue report: https://code.google.com/p/android/issues/detail?id=55863 seems to suggest that directly referencing the .AAR file is not supported. 此问题报告: https//code.google.com/p/android/issues/detail? id = 55863似乎建议不支持直接引用.AAR文件。

Perhaps the alternative for now is to define the actionbarsherlock library as a Gradle library under the parent directory of your project and reference accordingly. 也许现在的替代方法是将actionbarsherlock库定义为项目的父目录下的Gradle库并相应地引用。

The syntax is defined here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Referencing-a-Library 语法在这里定义http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Referencing-a-Library


#3楼

Currently referencing a local .aar file is not supported (as confirmed by Xavier Ducrochet) 目前不支持引用本地.aar文件(由Xavier Ducrochet确认)

What you can do instead is set up a local Maven repository (much more simple than it sounds) and reference the .aar from there. 您可以做的是设置一个本地Maven存储库(比听起来简单得多)并从那里引用.aar。

I've written a blogpost detailing how to get it working here: 我写了一篇博客文章详细介绍了如何让它在这里工作:

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


#4楼

You can reference an aar file from a repository. 您可以从存储库引用aar文件。 A maven is an option, but there is a simpler solution: put the aar file in your libs directory and add a directory repository. maven是一个选项,但有一个更简单的解决方案:将aar文件放在libs目录中并添加目录存储库。

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

Then reference the library in the dependency section: 然后在依赖项部分中引用库:

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

You can check out Min'an blog post for more info. 您可以查看Min'an博客文章了解更多信息。


#5楼

I found this workaround in the Android issue tracker: https://code.google.com/p/android/issues/detail?id=55863#c21 我在Android问题跟踪器中找到了此解决方法: https//code.google.com/p/android/issues/detail? id = 55863# c21

The trick (not a fix) is to isolating your .aar files into a subproject and adding your libs as artifacts: 技巧(不是修复)是将.aar文件隔离到子项目中并将libs添加为工件:

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

More info: Handling-transitive-dependencies-for-local-artifacts-jars-and-aar 更多信息: 处理 - 传递 - 依赖 - 为本地 - 工件 - jar-and-aar


#6楼

Please follow below steps to get it working ( I have tested it up to Android Studio 2.2) 请按照以下步骤使其正常运行(我已将其测试到Android Studio 2.2)

Lets say you have kept aar file in libs folder. 让我们说你已经在libs文件夹中保存了aar文件。 ( assume file name is cards.aar ) (假设文件名是cards.aar

then in app build.gradle specify following and click sync project with Gradle files. 然后在app build.gradle指定以下内容并单击与Gradle文件同步项目。 Open Project level build.gradle and add flatDir{dirs 'libs'} like did below 打开Project level build.gradle并添加flatDir{dirs 'libs'} ,如下所示

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

and now open app level build.grdle file and add .aar file 现在打开app level build.grdle文件并添加.aar文件

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

If everything goes well you will see library entry is made in build -> exploded-aar 如果一切顺利,您将看到库条目在build - > explosion-aar中进行

Also note that if you are importing a .aar file from another project that has dependencies you'll need to include these in your build.gradle, too. 另请注意,如果要从具有依赖项的其他项目导入.aar文件,则还需要在build.gradle中包含这些文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值