如何在aar库中指定依赖关系? [英] How to specify dependencies in aar library?

查看:314
本文介绍了如何在aar库中指定依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个依赖于maven repo上可用的其他库(如 gson 改装等)的Android库(称为 MyLib )。 )。

I created an Android library (called MyLib) which depends on other library available on maven repo (like gson, retrofit, etc.).

MyLib
 |
 |-- Retrofit
 |-- Gson
 |-- ...

MyLib打包成一个 aar 文件。

MyLib is packaged to an aar file.

目标是发布一个aar库可以包含在Android应用程序(称为 MyApp )中,而不再指定 MyLib 使用的依赖项。

The goal is to publish an aar library which can be included into an Android app (called MyApp) without specifying a second time the dependencies that MyLib uses.

MyApp
 |
 |-- MyLib
 |    |-- Retrofit
 |    |-- gson
 |    |-- ...

这是我的 build.gradle 文件 MyLib

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:2.3.1'
}

现在,如果我要构建并运行MyApp 而没有依赖问题,我不得不使用以下的build.gradle for MyApp (如果我没有指定改装 gson 作为deps,则抛出运行时异常因为deps不可用)

Now, if I want to build and run MyApp without dependency issue, I had to use the following build.gradle for MyApp (if I don't specify retrofit and gson as deps, a runtime exception is thrown because the deps are not available).

dependencies {
    compile('MyLib@aar')

    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:2.3.1'
}

我不想在MyApp中指定 MyLib em>,我该怎么写我的build.gradle文件?

I don't want to specify in MyApp the dependencies that are used inside MyLib, How should I write my build.gradle files?

提前Thansk

推荐答案

发布 aar 到maven仓库(本地或远程),并使用 compile(... @ aar)传递依赖关系关闭。

When publishing an aar to a maven repository (local or remote) and including it using compile (...@aar) transitive dependencies are turned off.

要为 aar 库启用传递依赖关系:

To enable transitive dependencies for an aar library:

compile ('com.mylib:mylib:1.0.0@aar'){
       transitive=true
}

您可以在这里阅读更多信息:

You can read more about this here:

AAR可以包括传递依赖关系吗?

aar 发布到远程或本地maven存储库的库,在您的情况下,它似乎不会被发布到甚至本地的maven存储库。我找不到任何明确的信息,如果它会在你的情况下工作,但你应该给它一个镜头。

This works with aar libraries that are published to a remote or local maven repository, In your case it sounds like the library will not be published to even a local maven repository. I can't find any definitive information as to if it will work in your circumstances, but you should give it a shot.

这篇关于如何在aar库中指定依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆