简体   繁体   English

gradle可以解决Android .aar文件中的依赖关系吗?

[英]Can gradle resolve dependencies from just a Android .aar file?

I'm working on a internal library (let's call it "banana_lib") that will be included in an Android app project. 我正在开发一个内部库(我们称之为“banana_lib”),它将包含在Android应用程序项目中。 I want to provide this library to a client in the form of an .aar file. 我想以.aar文件的形式向客户端提供此库。

My library depends on another library (let's say Gson). 我的图书馆依赖于另一个图书馆(让我们说Gson)。 If I can help it, I want to avoid bundling the classes of this dependency into my library ( the "fat jar", "uber jar" approach ). 如果我能帮助它,我想避免将这种依赖的类捆绑到我的库中(“胖罐”,“超级罐”方法)。

I know that I could ask the user of my library to include the dependencies in the build.gradle file of the app: 我知道我可以要求我的库的用户将依赖项包含在应用程序的build.gradle文件中:

dependencies {
    compile 'com.google.code.gson:gson:2.2.4'
    compile files('libs/banana_lib.aar')

} }

But I don't want to burden the user of the library with including the dependencies of my library in the build.gradle file of the app. 但我不想让我的库的用户在应用程序的build.gradle文件中包含我的库的依赖项给图书馆用户带来负担。

Question: Is there a way to make gradle automatically resolve/include the dependencies of an .aar file? 问题:有没有办法让gradle自动解析/包含.aar文件的依赖项? What's the way to do this so that a user of a .aar library has the minimum effort? 有什么办法可以让.aar库的用户尽量减少工作量? Do I need to provide a pair of .aar and .pom file? 我需要提供一对.aar和.pom文件吗? My main goal here would be to reduce the things that a user of this library would have to do. 我的主要目标是减少这个库的用户必须做的事情。 I feel like a library should ideally define all its dependencies, and gradle should just resolve them in the background. 我觉得图书馆应该理想地定义它的所有依赖项,gradle应该只在后台解决它们。

Put in a slightly different way: Is there any way to allow a client/user to include a (internal!)library(which has external dependencies) by just: 1.) adding a .aar file to the build. 换一种略有不同的方式:有没有办法允许客户端/用户通过以下方式包含(内部!)库(具有外部依赖性):1。)将.aar文件添加到构建中。 2.) including it in the build.gradle. 2.)将它包含在build.gradle中。

If there are other solutions that keep the amount of work that the user/includer of the library has to do to a minimum, then that'd be interesting as well :) 如果有其他解决方案可以将库的用户/包含者必须做的工作量保持在最低限度,那么这也很有趣:)

Many thanks! 非常感谢!

But I don't want to burden the user of the library with including the dependencies of my library in the build.gradle file of the app. 但我不想让我的库的用户在应用程序的build.gradle文件中包含我的库的依赖项给图书馆用户带来负担。

Then publish the AAR and a POM in an artifact repository, where the POM spells out the dependencies. 然后在工件库中发布AAR和POM,其中POM说明了依赖关系。

I feel like a library should ideally define all it's dependencies, and gradle should just resolve them in the background. 我觉得图书馆应该理想地定义它的所有依赖项,gradle应该只在后台解决它们。

That is not how JARs and AARs work. 这不是JAR和AAR的工作方式。 Neither have dependency information in the JAR or AAR. 在JAR或AAR中都没有依赖信息。 Both rely on POMs or other external declarations of dependency information, versioning information, etc. 两者都依赖于POM或依赖信息的其他外部声明,版本信息等。

Is there any way to allow a client/user to include a (internal!)library(which has external dependencies) by just: 1.) adding a .aar file to the build. 有没有办法允许客户端/用户通过以下方式包含(内部!)库(具有外部依赖性):1。)将.aar文件添加到构建中。 2.) including it in the build.gradle. 2.)将它包含在build.gradle中。

Publish the AAR and a POM in an artifact repository, where the POM spells out the dependencies. 在工件存储库中发布AAR和POM,POM会在其中列出依赖关系。

Note that "an artifact repository" does not necessarily imply Maven Central or JCenter. 请注意,“工件库”并不一定意味着Maven Central或JCenter。 An artifact repository can be just a directory tree, particularly when accessed via a local filesystem. 工件存储库可以只是一个目录树,特别是通过本地文件系统访问时。 For example, you already have two of these local repositories on your development machine, most likely: the Android Repository and the Google Repository. 例如,您的开发计算机上已经有两个本地存储库,很可能是:Android存储库和Google存储库。 If you want to have the repository hosted on a Web site (eg, internal Web server), that's possible, though you need to add directory-style index.html files at each level of the directory tree. 如果您希望在网站(例如,内部Web服务器)上托管存储库,那么这是可能的,尽管您需要在目录树的每个级别添加目录样式的index.html文件。 Or, Sonatype and Bintray sell dedicated artifact repository servers, as I understand it. 或者,据我所知,Sonatype和Bintray出售专用的工件库服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM