我一直在尝试新的android构建系统,我遇到了一个小问题。我已经编译了我自己的ActionBarSherlock的aar包,我称之为' ActionBarSherlock .aar'。我想做的是用这个aar来构建我最终的APK。如果我包括整个ActionBarSherlock库作为一个android-library模块到我的主项目使用编译项目(':ActionBarSherlock '),我能够成功地构建没有任何问题。

但我的问题是,我想手动提供一个aar文件包的依赖,如果我将JAR,那么我似乎不知道如何正确地将它包含到我的项目中。我尝试使用编译配置,但这似乎不工作。在编译期间,我一直无法找到符号,这告诉我aar包中的classes.jar没有包含在类路径中。

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

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

编辑:所以答案是目前不支持,如果你想跟踪它,这是一个问题。

编辑:目前这仍然不直接支持,最好的替代方案似乎是@RanWakshlak提出的解决方案

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


当前回答

有两种方法:

第一种方法

打开Android Studio,通过文件->新建->新建模块导航到创建新模块窗口

选择Import . jar /。AAR Package项并单击Next按钮 在构建中添加依赖项。属于你的app模块的Gradle文件。

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

这是所有。

第二种方法

在libs目录下创建一个文件夹,例如aars。 把你的aar lib放到aars文件夹中。 添加代码片段

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

进入你的构建。Gradle文件属于app模块。

在构建中添加依赖项。属于你的app模块的Gradle文件。

dependencies {
    ...
    implementation (name:'your aar lib name', ext:'aar')
}

这是所有。

If you can read Chinese, you can check the blog. What are AAR files and how can they be used in Android development

其他回答

不幸的是,这里的解决方案都不适合我(我无法解决依赖关系)。最后工作,是最简单的方法IMHO是:突出项目名称从Android工作室然后文件->新模块->导入JAR或AAR包。这篇文章的功劳归于解决方案

请按照下面的步骤让它工作(我测试到Android Studio 2.2)。

假设你在libs文件夹中有一个。aar文件(例如cards.aar)。

然后在应用程序构建中。指定下面的gradle文件,然后点击同步项目。

开放项目级构建。gradle和添加flatDir {dirs("libs")}如下所示:

allprojects {
    repositories {
        jcenter()

        flatDir {
            dirs("libs")
        }
    }
}

开放应用程序级别构建。Gradle文件和添加。aar文件:

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

如果你正在使用Kotlin并且有一个build.gradle.kts文件:

dependencies {
    implementation(name = "cards", ext = "aar")
}

如果一切顺利,您将在build -> explosion -aar中看到库条目。

还要注意,如果您从另一个具有依赖关系的项目导入.aar文件,则需要在构建中包含这些依赖关系。gradle。

在我的情况下,我在我的库中有一些依赖,当我从它创建一个aar时,我失败了,因为错过了依赖,所以我的解决方案是用一个arr文件从我的库中添加所有依赖。

我的项目级构建。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
}

构建。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
    ...
}

更新android studio 3.4

进入文件->项目结构

模块,然后单击+

选择Import .aar Package

找到.aar路由

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

现在在app模块中,点击+和module Dependency

检查库包,确定

验证添加的依赖项

项目结构是这样的

右键单击你的项目,选择“打开模块设置”。

点击窗口左上角的“+”按钮可以添加一个新的模块。

选择“导入。jar或。aar包”,然后单击“下一步”按钮。

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

保持应用程序的模块被选中,并单击Dependencies窗格将新模块添加为依赖项。

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

选择模块并单击“确定”。

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

dependencies {
    implementation project(':your_module')
}

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