Android 使用“flatDirs”将本地 .aar 文件添加到 Gradle 构建不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24506648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding local .aar files to Gradle build using "flatDirs" is not working
提问by Alexey Dmitriev
I'm aware of this question: Adding local .aar files to my gradle buildbut the solution does not work for me.
我知道这个问题:将本地 .aar 文件添加到我的 gradle 构建中,但该解决方案对我不起作用。
I tried adding this statement to the top level of my build.gradle
file:
我尝试将此语句添加到build.gradle
文件的顶层:
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
I've also put the slidingmenu.aar
file into /libs
and referenced it in the dependencies
section: compile 'com.slidingmenu.lib:slidingmenu:1.0.0@aar'
but it did not work at all.
我也将slidingmenu.aar
文件放入/libs
并在dependencies
部分中引用了它:compile 'com.slidingmenu.lib:slidingmenu:1.0.0@aar'
但它根本不起作用。
I tried compile files('libs/slidingmenu.aar')
as well but with no luck.
我也尝试过compile files('libs/slidingmenu.aar')
,但没有运气。
What am I missing? Any ideas?
我错过了什么?有任何想法吗?
P.S. Android Studio 0.8.2
PS安卓工作室0.8.2
回答by Ashton Engberg
Building upon Josiah's answer, here's how I got it to work.
基于 Josiah 的回答,这就是我让它工作的方式。
Following his instructions (under edit) (File -> New-> New Module -> Import .JAR/.AAR) and import your .AAR.
按照他的指示(在编辑下)(文件 -> 新建 -> 新模块 -> 导入 .JAR/.AAR)并导入您的 .AAR。
Then in your project build.gradle (not the top level one, the one under 'app') add the following (in the dependencies section):
然后在你的项目 build.gradle(不是顶级的,在“app”下的)添加以下内容(在依赖项部分):
dependencies {
compile project(':Name-Of-Your-Project')
}
Note Name-Of-Your-Project should match the name of the folder that was added after you imported the AAR file (at the same level as app/.idea
under the top most level folder). Or to put it another way...
注意 Name-Of-Your-Project 应与导入 AAR 文件后添加的文件夹的名称匹配(与app/.idea
最顶层文件夹下的级别相同)。或者换一种说法...
MyApplication .idea app build.gradle (here's where to add compile project(':ProjectName') to dependency section) ProjectName (added automatically after importing, matching the name of your aar file) build gradle etc
This worked for me running Android Studio 0.8.0. Don't forget to synchronize gradle (using toolbar button or in File->Synchronize) after you do this.
这对我运行 Android Studio 0.8.0 很有用。执行此操作后,不要忘记同步 gradle(使用工具栏按钮或在File->Synchronize 中)。
(Thanks to Josiah for getting me going in the right direction)
(感谢 Josiah 让我朝着正确的方向前进)
(Note: prior to this I tried adding it to the libs folder, trying to manipulate the top level build.gradle
and the app level build.gradle
, but none of that worked for my aars files--jar's will work fine, but not the aar files)
(注意:在此之前,我尝试将它添加到 libs 文件夹,尝试操作顶级build.gradle
和应用程序级别build.gradle
,但这些都不适用于我的 aars 文件——jar 可以正常工作,但不适用于 aar 文件)
回答by Nishant.
Update: As @amram99 mentioned, the issuehas been fixed as of the release of Android Studio v1.3.
更新:正如@amram99 所提到的,该问题已在 Android Studio v1.3 发布时得到修复。
Tested and verified with below specifications
通过以下规格测试和验证
- Android Studio v1.3
- gradle plugin v1.2.3
- Gradle v2.4
- 安卓工作室 v1.3
- gradle 插件 v1.2.3
- 摇篮 v2.4
What works now
现在什么工作
Now you can import a local aar file via the File>New>New Module>Import .JAR/.AAR Packageoption in Android Studio v1.3
However the below answer holds true and effective irrespective of the Android Studio changes as this is based of gradle scripting.
现在您可以通过Android Studio v1.3 中的File>New>New Module>Import .JAR/.AAR Package选项导入本地 aar 文件
然而,无论 Android Studio 发生什么变化,以下答案都是真实有效的,因为这是基于 gradle 脚本编写的。
Old Answer :In a recent update the people at android broke the inclusion of local aar files via the Android Studio's add new module menu option. Check the Issuelisted here. Irrespective of anything that goes in and out of IDE's feature list , the below method works when it comes to working with local aar files.(Tested it today):
旧答案:在最近的更新中,android 人员通过 Android Studio 的添加新模块菜单选项破坏了本地 aar 文件的包含。检查此处列出的问题。无论进出 IDE 功能列表的任何内容,以下方法在处理本地 aar 文件时都有效。(今天测试):
Put the aar file in the libs directory (create it if needed), then, add the following code in your build.gradle :
将 aar 文件放在 libs 目录中(如果需要,请创建它),然后在 build.gradle 中添加以下代码:
dependencies {
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
repositories{
flatDir{
dirs 'libs'
}
}
回答by Josiah
Edit:The correct way (currently) to use a local AAR file as a build dependency is to use the module import wizard (File | New Module | Import .JAR or .AAR package) which will automatically add the .aar as a library module in your project.
编辑:使用本地 AAR 文件作为构建依赖项的正确方法(当前)是使用模块导入向导(文件 | 新建模块 | 导入 .JAR 或 .AAR 包),它将自动将 .aar 添加为库模块在你的项目中。
Old Answer
旧答案
Try this:
尝试这个:
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
...
compile(name:'slidingmenu', ext:'aar')
回答by Richard
I got this working on Android Studio 2.1. I have a module called "Native_Ads" which is shared across multiple projects.
我在 Android Studio 2.1 上得到了这个。我有一个名为“Native_Ads”的模块,它在多个项目之间共享。
First, I created a directory in my Native_ads module with the name 'aars' and then put the aar file in there.
首先,我在 Native_ads 模块中创建了一个名为“aars”的目录,然后将 aar 文件放在那里。
Directory structure:
目录结构:
libs/
aars/ <-- newly created
src/
build.gradle
etc
Then, the other changes:
然后,其他变化:
Top level Gradle file:
顶级 Gradle 文件:
allprojects {
repositories {
jcenter()
// For module with aar file in it
flatDir {
dirs project(':Native_Ads').file('aars')
}
}
}
App module's build.gradle file: - no changes
应用模块的 build.gradle 文件: - 没有变化
Settings.gradle file (to include the module):
Settings.gradle 文件(包含模块):
include ':app'
include 'Native_Ads'
project(':Native_Ads').projectDir = new File(rootProject.projectDir, '../path/to/Native_Ads')
Gradle file for the Native_Ads module:
Native_Ads 模块的 Gradle 文件:
repositories {
jcenter()
flatDir {
dirs 'aars'
}
}
dependencies {
compile(name:'aar_file_name_without_aar_extension', ext:'aar')
}
That's it. Clean and build.
就是这样。清洁和建造。
回答by Marian Klühspies
回答by lucasddaniel
This is my structure, and how I solve this:
这是我的结构,以及我如何解决这个问题:
MyProject/app/libs/mylib-1.0.0.aar
MyProject/app/myModulesFolder/myLibXYZ
MyProject/app/libs/ mylib-1.0.0.aar
我的项目/应用程序/myModulesFolder/myLibXYZ
On build.gradle
在build.gradle 上
from Project/app/myModulesFolder/myLibXYZ
来自项目/应用程序/myModulesFolder/myLibXYZ
I have put this:
我已经把这个:
repositories {
flatDir {
dirs 'libs', '../../libs'
}
}
compile (name: 'mylib-1.0.0', ext: 'aar')
Done and working fine, my submodule XYZ depends on somelibrary from main module.
完成并且工作正常,我的子模块 XYZ 依赖于主模块中的某个库。
回答by agrosner
You can do it this way. It needs to go in the maven format:
你可以这样做。它需要采用 maven 格式:
repositories {
maven { url uri('folderName')}
}
And then your AAR needs to go in a folder structure for a group id "com.example":
然后您的 AAR 需要进入组 ID“com.example”的文件夹结构:
folderName/
com/
example/
verion/
myaar-version.aar
Then reference as a dependency:
然后作为依赖引用:
compile 'com.example:myaar:version@aar'
compile 'com.example:myaar:version@aar'
Where version
is the version of your aar file (ie, 3.0, etc)
version
您的 aar 文件的版本在哪里(即 3.0 等)
回答by Victor Ude
For anyone who has this problem as of Android Studio 1.4, I got it to work by creating a module within the project that contains 2 things.
对于从 Android Studio 1.4 开始遇到此问题的任何人,我通过在项目中创建一个包含 2 项内容的模块来使其工作。
build.gradle with the following contents:
configurations.create("default")
artifacts.add("default", file('facebook-android-sdk-4.7.0.aar'))
the aar file (in this example 'facebook-android-sdk-4.7.0.aar')
build.gradle 包含以下内容:
configurations.create("default")
artifacts.add("default", file('facebook-android-sdk-4.7.0.aar'))
aar 文件(在本例中为“facebook-android-sdk-4.7.0.aar”)
Then include the new library as a module dependency. Now you can use a built aar without including the sources within the project.
然后将新库包含为模块依赖项。现在,您可以使用构建的 aar,而无需在项目中包含源代码。
Credit to Facebook for this hack. I found the solution while integrating the Android SDK into a project.
将此归功于 Facebook。我在将 Android SDK 集成到项目中时找到了解决方案。
回答by MohammadReza
In my case the none of the answers above worked! since I had different productFlavors just adding repositories{
flatDir{
dirs 'libs'
}
}
就我而言,上述答案均无效!因为我有不同的 productFlavors 只是添加repositories{
flatDir{
dirs 'libs'
}
}
did not work! I ended up with specifying exact location of libsdirectory:
不工作!我最终指定了libs目录的确切位置:
repositories{
flatDir{
dirs 'src/main/libs'
}
}
}
Guess one should introduce flatDirs like this when there's different productFlavors in build.gradle
猜猜当 build.gradle 中有不同的 productFlavors 时,应该像这样引入flatDirs
回答by Glorin
In my case, I just put the aar file in libs, and add
就我而言,我只是将 aar 文件放在 libs 中,然后添加
dependencies {
...
api fileTree(dir: 'libs', include: ['*.aar'])
...
}
dependencies {
...
api fileTree(dir: 'libs', include: ['*.aar'])
...
}
in build.gradle and it works. I think it is similar with default generated dependency:
在 build.gradle 中,它可以工作。我认为它与默认生成的依赖项类似:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(dir: 'libs', include: ['*.jar'])