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


当前回答

在我的情况下,我在我的库中有一些依赖,当我从它创建一个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问题跟踪器中找到了这个解决方法: https://code.google.com/p/android/issues/detail?id=55863#c21

诀窍(不是修复)是将你的.aar文件隔离到子项目中,并将你的库作为工件添加:

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

更多信息: Handling-transitive-dependencies-for-local-artifacts-jars-and-aar

对我来说,这是Android Studio环境配置的问题。

当我更新文件->项目结构-> JDK位置到一个更高的Java版本(jdk1.8.0_192。JDK -对我来说),一切都开始工作了。

您可以从存储库引用aar文件。 maven是一种选择,但有一个更简单的解决方案:将aar文件放在libs目录中,并添加一个目录存储库。

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

然后在依赖项部分引用该库:

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

你可以查看闵安的博客文章获取更多信息。


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


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

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

将项目结构从Android改为Project。

导航到app->libs如下所示

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

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

就是这样。

你可以这样做:

将您的本地库(扩展名:.jar, .aar,…)放入“libs”文件夹(如果您愿意,也可以使用其他文件夹)。 在构建。Gradle(应用级别),将这一行添加到依赖项中 实现文件树(包括:['*.jar', '*.jar', '*.jar'。Aar '], dir: 'libs')