如何使用新的 Gradle Android 构建系统手动包含外部 aar 包 [英] How to manually include external aar package using new Gradle Android Build System

查看:30
本文介绍了如何使用新的 Gradle 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 Android 构建系统手动包含外部 aar 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆