如何使用 Gradle for Android 手动包含外部 aar 包

How to manually include external aar package using Gradle for Android(如何使用 Gradle for Android 手动包含外部 aar 包)

本文介绍了如何使用 Gradle for Android 手动包含外部 aar 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验新的 android 构建系统,但遇到了一个小问题.我已经编译了我自己的 ActionBarSherlock 的 aar 包,我称之为actionbarsherlock.aar".我想要做的是实际上使用这个 aar 来构建我的最终 APK.如果我使用 compile project (':actionbarsherlock') 将整个 ActionBarSherlock 库作为 android-library 模块包含到我的主项目中,我就可以成功构建而不会出现任何问题.

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.

但我的问题是我想手动提供该依赖项作为 aar 文件包,就像我想要一个 JAR 一样,然后我似乎无法弄清楚如何正确地将它包含到我的项目中.我尝试使用编译配置,但这似乎不起作用.我一直在编译期间找不到符号,这告诉我 aar 包中的 classes.jar 没有包含在类路径中.

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.

有人知道手动将 aar 包作为文件包含的语法吗?

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

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"
}

所以答案是它目前不受支持,这是 问题如果你想跟踪它.

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

目前仍然不直接支持,最好的选择似乎是@RanWakshlak 提出的解决方案

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

使用@VipulShah 提出的语法也更简单

Also simpler by using the syntax proposed by @VipulShah

推荐答案

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

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

Lets say you have kept aar file in libs folder. ( assume file name is cards.aar )

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

then in app build.gradle specify following and click sync project with Gradle files. Open Project level build.gradle and add flatDir{dirs 'libs'} like did below

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

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

and now open app level build.grdle file and add .aar file

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

如果一切顺利,您将看到库条目是在构建中创建的 ->爆炸-aar

If everything goes well you will see library entry is made in build -> exploded-aar

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

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.

这篇关于如何使用 Gradle for Android 手动包含外部 aar 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 Gradle for Android 手动包含外部 aar 包

基础教程推荐