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

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

问题描述

我创建了一个 Android 库(称为 MyLib),它依赖于 maven repo 上可用的其他库(如 gsonretrofit 等.).

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

This is my build.gradle file for 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 而没有依赖问题,我必须为 MyApp 使用以下 build.gradle(如果我没有指定 retrofitgson 作为 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 中使用的依赖项,我应该如何编写 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?

提前致谢

推荐答案

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 能否包含传递依赖项?

这适用于发布到远程或本地 maven 存储库的 aar 库,在您的情况下,听起来该库甚至不会发布到本地 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天全站免登陆