我一直在尝试新的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提出的语法也更简单


当前回答

我只是简单地加上这几行:-

在build.gradle

buildscript {
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
}

and

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

之后,点击文件->同步项目与Gradle文件

注意:-最重要的是不要忘记在libs文件夹中导入。aar文件

其他回答


只有一个时间设置&可以添加未来库,无需任何代码更改


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

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

将项目结构从Android改为Project。

导航到app->libs如下所示

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

点击android studio左上角的文件,然后点击“同步项目与Gradle文件”。

就是这样。

在Android Studio 3.4和Gradle 5中,你可以简单地这样做

dependencies {
    implementation files('libs/actionbarsherlock.aar')
}

我也有这个问题。此问题报告: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-Referencing-a-Library

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

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

我写了一篇博文详细介绍了如何让它在这里工作:

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

在我的情况下,我在我的库中有一些依赖,当我从它创建一个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
    ...
}