Android Studio导入aar包时,分为两种情况:
1.直接在当前的module中使用:
导入test.aar包需要在build.gradle中加入:
repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
compile(name:'test', ext:'aar')
}
2.在引用的module中使用:
导入test.aar包需要在引用module的build.gradle中加入:
repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
compile(name:'test', ext:'aar')
}
还需在当前module的build.gradle中加入:
//使用aar必须要在module里说明
repositories {
flatDir {
dirs project(':moduleName').file('libs')
}
}
moduleName为引用module的名称
否则会报出Error:Failed to resolve: 的错误。
具体原因的解释可以参考:
stackOverFlow
以下是摘录片段:
The other projects are seeing that the :ThirdPartyLibraryWrapper project depends on an artifact called ThirdPartyLibrary-0.1.0:aar. Java (and Android) libraries do not bundle their own dependencies together - instead, they simply publish a list of their dependencies. The consuming project is then responsible for loading not only the library it directly depends on, but all of the libraries that library depends on.
The net effect of this is that :MyAppFree is loading in :ThirdPartyLibraryWrapper, then seeing that :ThirdPartyLibraryWrapper depends on ThirdPartyLibrary-0.1.0:aar and so thus trying to load that in as well. However, :MyAppFree doesn’t know where ThirdPartyLibrary-0.1.0:aar lives.. and so it fails.
The solution will be to place similar repositories blocks in all your other projects. Try this:
repositories {
flatDir {
dirs project(':ThirdPartyLibraryWrapper').file('libs')
}
}
Using the project(…).file(…) method will free you from having to hardcode paths, and will instead use the Gradle DSL to resolve the filesystem path by looking up the project and having it do the resolution dynamically.