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


当前回答

如果您使用Gradle Kotlin DSL,您需要在模块目录中添加一个文件。

例如:libs/someAndroidArchive.aar

在依赖块的build.gradle.kts模块中写完这些代码后:

implementation(files("libs/someAndroidArchive.aar"))

其他回答

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

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

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

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

你可以这样做:

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

在我的情况下,只是工作时,我添加“项目”编译:

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


dependencies {
   compile project('com.x.x:x:1.0.0')
}