Android Studio的aar包导入问题

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值