如何使用新的Gradle Android构建系统手动包含外部aar包
我一直在尝试新的android构建系统,但遇到了一个小问题。我已经编译了自己的ActionBarSherlock aar包,我称之为“ActionBarSherlock.aar”。我想做的是实际使用aar来构建我的最终APK。如果我使用compileproject(“:ActionBarSherlock”)将整个ActionBarSherlock库作为一个android库模块包含到我的主项目中,我就能够成功地构建,而不会出现任何问题 但我的问题是,我想手动将依赖项作为aar文件包提供,如果我想要一个JAR,那么我似乎无法找到如何正确地将其包含到我的项目中。我曾尝试使用编译配置,但这似乎不起作用。在编译过程中,我一直无法找到符号,这告诉我aar包中的classes.jar没有包含在类路径中 有人知道将aar包手动包含为文件的语法吗 格雷德尔先生如何使用新的Gradle Android构建系统手动包含外部aar包,android,gradle,android-gradle-plugin,build.gradle,Android,Gradle,Android Gradle Plugin,Build.gradle,我一直在尝试新的android构建系统,但遇到了一个小问题。我已经编译了自己的ActionBarSherlock aar包,我称之为“ActionBarSherlock.aar”。我想做的是实际使用aar来构建我的最终APK。如果我使用compileproject(“:ActionBarSherlock”)将整个ActionBarSherlock库作为一个android库模块包含到我的主项目中,我就能够成功地构建,而不会出现任何问题 但我的问题是,我想手动将依赖项作为aar文件包提供,如果我想要
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文件 也许现在的替代方法是将actionbarsherlock库定义为项目父目录下的Gradle库,并相应地进行引用
此处定义了语法当前不支持引用本地.aar文件(由Xavier Ducrochet确认) 您可以做的是建立一个本地Maven存储库(比听起来简单得多),并从那里引用.aar 我已经写了一篇博客文章,详细介绍了如何让它在这里工作:
您可以从存储库中引用aar文件。 maven是一个选项,但有一个更简单的解决方案:将aar文件放在libs目录中,并添加一个目录存储库
存储库{
mavenCentral()
弗拉蒂尔{
dirs'libs'
}
}
然后在依赖项部分中引用库:
依赖项{
实现'com.actionbarsherlock:actionbarsherlock:4.4。0@aar'
}
您可以查看更多信息。我在Android问题跟踪器中找到了以下解决方法: 诀窍(不是修复)是将.aar文件隔离到子项目中,并将LIB添加为工件:
configurations.create("default")
artifacts.add("default", file('somelib.jar'))
artifacts.add("default", file('someaar.aar'))
更多信息:
请按照以下步骤操作它(我已经在Android Studio 2.2上对它进行了测试) 假设您已将aar文件保存在libs文件夹中。(假设文件名为
cards.aar
)
然后在appbuild.gradle
中指定以下内容,然后单击将项目与gradle文件同步。
打开项目级build.gradle并像下面那样添加flatDir{dirs'libs'}
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
现在打开app-levelbuild.grdle文件并添加.aar
文件
dependencies {
implementation(name:'cards', ext:'aar')
}
如果一切顺利,您将看到库条目是在build->aar中创建的
另外请注意,如果您要从另一个具有依赖项的项目导入.aar文件,您也需要将这些文件包含在build.gradle中。以下方法适用于最新的Android studio(>v0.8.x):
- 将
文件保存在app模块的aar
文件夹下(例如:libs
)//libs/myaar.aar
- 将以下内容添加到“app”模块文件夹的build.gradle(而不是项目根build.gradle)。注意编译行中的名称,它是myaar@aar不是myaar.aar
dependencies { compile 'package.name.of.your.aar:myaar@aar' } repositories{ flatDir{ dirs 'libs' } }
- 单击
Tools->Android->Sync Project with Gradle Files
- 将mylib-0.1.aar文件复制到libs/文件夹中
- 将这些行添加到build.gradle的底部(应该是应用程序,而不是项目):
- 到目前为止还不错。下面是最重要的一点: Gradle需要访问网络以获取依赖项,除非启用了脱机模式 确保通过项目结构/Gradle中的复选框启用了脱机工作 --或者-- 配置代理设置以访问网络 要配置代理设置,您必须修改项目的gradle.properties文件,分别配置http和https,如下所示:
- 我刚刚成功
systemProp.http.proxyHost=proxy.example.com
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=user
systemProp.http.proxyPassword=pass
systemProp.http.nonProxyHosts=localhost
systemProp.http.auth.ntlm.domain=example <for NT auth>
systemProp.https.proxyHost=proxy.example.com
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=user
systemProp.https.proxyPassword=pass
systemProp.https.nonProxyHosts=localhost
systemProp.https.auth.ntlm.domain=example <for NT auth>
systemProp.http.proxyHost=proxy.example.com
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=user
systemProp.http.proxyPassword=pass
systemProp.http.nonProxyHosts=localhost
systemProp.http.auth.ntlm.domain=示例
systemProp.https.proxyHost=proxy.example.com
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=user
systemProp.https.proxyPassword=pass
systemProp.https.nonProxyHosts=localhost
systemProp.https.auth.ntlm.domain=示例
希望这能起作用。不幸的是,这里没有一个解决方案适合我(我有未解决的依赖项)。IMHO最简单的方法是:突出显示Android Studio中的项目名称,然后单击文件->新模块->导入JAR或AAR包。要归功于中的解决方案,您只需几行代码就可以添加多个
aar
依赖项
添加本地flatDir
存储库:
repositories {
flatDir {
dirs 'libs'
}
}
将libs
目录中的每个aar
添加到compile
依赖项配置:
fileTree(dir: 'libs', include: '**/*.aar')
.each { File file ->
dependencies.add("compile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
}
fileTree(目录:“libs”,包括:“***.aar”)
.each{File->
dependencies.add(“compile”,[name:file.name.lastIndexOf('.')。与{it!=-1?file.name[0。。
右键单击项目并选择“打开模块设置”
<
fileTree(dir: 'libs', include: '**/*.aar')
.each { File file ->
dependencies.add("compile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
}
dependencies {
implementation project(':your_module')
}
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
}
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
...
}
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
...
}
dependencies {
...
implementation project(path: ':your aar lib name')
}
repositories {
flatDir {
dirs 'libs/aars'
}
}
dependencies {
...
implementation (name:'your aar lib name', ext:'aar')
}
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
implementation fileTree(include: ['*.jar', '*.aar'], exclude: 'test_aar*', dir: 'libs')
debugImplementation files('libs/test_aar-debug.aar')
releaseImplementation files('libs/test_aar-release.aar')
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
compile project('com.x.x:x:1.0.0')
}
dependencies {
implementation files('libs/actionbarsherlock.aar')
}
implementation(files("libs/someAndroidArchive.aar"))
implementation fileTree(dir: "libs", include: ["*.aar"])