摇篮为Android,亚洲遥感协会和条件的依赖遥感、亚洲、摇篮、条件

2023-09-04 10:05:53 作者:孤独劣人

简称:?什么是组织code / POM为AAR的一些方法,以使应用程序使用AAR只能依赖他们实际需要

Short Form: What are some ways of organizing the code/POM for an AAR, such that apps using that AAR only have dependencies they actually need?

长篇

假设我们有一个应用程序依赖于一个Android库项目打包成一个AAR(L)。

Suppose we have an app that depends upon an Android library project packaged as an AAR (L).

L含有类混合以及任何给定的应用程序(如A)将仅使用这些​​类的子集。例如:

L contains a mix of classes, and any given app (like A) will only use a subset of those classes. For example:

L可包含片段实现本机API级别11的片段,该片段向后移植,并ActionBarSherlock味片段

L may contain Fragment implementations for native API Level 11 fragments, the backported fragments, and ActionBarSherlock-flavored fragments

L可包含活动的实施进行定期的活动, FragmentActivity ActionBarActivity 和ActionBarSherlock味的活动

L may contain Activity implementations for regular activities, FragmentActivity, ActionBarActivity, and ActionBarSherlock-flavored activities

L可提高通过 LocalBroadcastManager 的活动,广场的奥托和greenrobot的EventBus

L may raise events via LocalBroadcastManager, Square's Otto, and greenrobot's EventBus

等等

这些案件主要有两个共同点,因为我看到它:

These cases have two main commonalities, as I see it:

应用程序通常只关心一些类。例如,使用奥多的应用程序并不会在意code引用greenrobot的EventBus,或使用 ActionBarActivity 并不会在意ActionBarSherlock的应用程序。 中国遥感应用协会

Apps will usually only care about some of the classes. For example, an app that uses Otto will not care about code that references greenrobot's EventBus, or an app that uses ActionBarActivity will not care about ActionBarSherlock.

如果该AAR是回购的神器,应用程序不会在意构建AAR是需要对所有可能的上游的依赖。例如,使用原生API级别11片段的应用程序将不再需要支持-V4 actionbarsherlock ,即使AAR的本身需要他们建立了AAR

If the AAR is an artifact in a repo, apps will not care about all possible upstream dependencies that is needed to build the AAR. For example, an app that is using native API Level 11 fragments will not need support-v4 or actionbarsherlock, even though the AAR itself needs them to build the AAR

如果我们一起去的JAR文件,而不是亚洲遥感协会和转储依赖管理,这是非常简单的。构建JAR会有编译时依赖(例如,支持-V4 )。然而,应用程式使用该JAR可以跳过这些依赖关系,所以只要这些应用程序不使用类真正需要这些依赖,生活是美好的。

If we were to go with JARs instead of AARs and dump dependency management, this is fairly straightforward. Building the JAR would have compile-time dependencies (e.g., support-v4). However, apps using that JAR could skip those dependencies, and so long as those apps do not use classes that truly need those dependencies, life is good.

不过,我在看到如何完成同样的事情在 build.gradle 文件中指定的亚洲遥感协会和Maven的文物有困难。如果L有一个依赖块引用上游的依赖,应用程序将依次下载这些依赖传递性,当应用程序依赖于大号

However, I am having difficulty in seeing how to accomplish the same thing with AARs and Maven artifacts specified in a build.gradle file. If L has a dependencies block referencing the upstream dependencies, apps will in turn download those dependencies transitively when the apps depend upon L.

这是我相当肯定会工作的一个解决方案是拆分L放入几个库。例如,使用的片段的情况下,我们可以有:

One solution that I am fairly sure will work is to split L into several libraries. For example, using the fragment scenario, we could have:

L1,它包含了实现本机API级别11版本的片段,以及所需要的其他情况,任何普通code。这个库就没有上游的依赖关系。

L1, which contains the implementation for the native API Level 11 version of fragments, plus any common code needed for other scenarios. This library would have no upstream dependencies.

L2,其中包含使用片段的支持Android包的反向移植的实施。 L2将有依赖于L1和支持-V4

L2, which contains the implementation that uses the Android Support package's backport of fragments. L2 would have dependencies on L1 and on support-v4.

L3,其中包含使用夏洛克味片段实施。 L3将不得不依赖于L1和 actionbarsherlock

L3, which contains the implementation that uses Sherlock-flavored fragments. L3 would have dependencies on L1 and actionbarsherlock.

然后,一个应用程序会选择是否要依赖于L1,L2或L3,因此只能获得必要的上游的依赖(如果有的话)。

Then, an app would choose whether to depend upon L1, L2, or L3, and therefore would only get the necessary upstream dependencies (if any).

我的问题是:是最好的解决办法?还是有别的东西在摇篮世界为Android,亚洲遥感协会,和Maven风格的器物,让应用程序依赖于一个单一的L&我所关注的库来处理上游的依赖关系的多变的搭配可能combinatoric爆炸。我也很关心古怪的应用程序,实际上的执行的需要多个实现,以及是否可以可靠地指定依赖那些(例如,根据L1和L2,因为这是一个应用程序,这是什么程序的作者认为该应用程序的需求)。

My question is: is that the best solution? Or is there something else in the world of Gradle for Android, AARs, and Maven-style artifacts that would allow apps to depend upon a single L? I am concerned about possible combinatoric explosions of libraries to handle a varied mix of upstream dependencies. I am also concerned about oddball apps that actually do need multiple implementations and whether or not we can reliably specify the dependencies for those (e.g., an app depending on both L1 and L2, because that's what that app's author thinks that app needs).

我知道有办法让一个应用程序来块 排除相关性(见语法约瑟夫·厄尔的答案),因此一个应用程序可以依靠大号但随后块上在 actionbarsherlock 上游的依赖,如果它是不需要的。虽然这可以工作,因为在那里我的L作者的情况下,我宁愿去的L1 / L2 / L3的做法,因为这似乎更清洁。

I know that there are ways for an app to block exclude dependencies (see Joseph Earl's answer for the syntax), so an app could depend upon L but then block the actionbarsherlock upstream dependency if it is not needed. While that could work, for cases where I'm the author of L, I'd rather go the L1/L2/L3 approach, as that seems cleaner.

任何其他建议?

推荐答案

我不知道有任何依赖管理功能,将有助于这一点。

I'm not aware of any dependency management feature that would help with this.

有一个单一的库有许多依赖,并删除不需要的依赖关系可以工作的能力,但也有一些问题,将带着这样的:

Having a single libraries with many dependencies and the ability to remove unwanted dependencies could work but there are some issues that will come with this:

您必须依靠对L-改掉正确的依赖关系的用户。 您将有类,不会很容易去掉使用ProGuard库。 Proguard的不会删除任何扩展片段/活动/ etc和L时,应提供ProGuard的规则文件不删除类,扩展支持片段/活动的/ etc ...这将使它难以去除不需要的类。 在一些实现可能有更多的资源,现在我们不能去掉不需要的资源。

我想分裂的图书馆是正确的事情,但是这将是复杂做,直到我们在库项目的味道。一旦我们有了这个,我认为这将是更容易处理。你不会有碱L1和L2 / L3延伸它。相反,你就会有一个单一的库,产生不同的变体,每个都有自己的依赖。

I think splitting the library is the right thing to do, however this is going to be complicated to do until we have flavors in library projects. Once we have this I think it'll be much easier to handle. You wouldn't have a base L1 and L2/L3 extending it. Instead you'd have a single library that generate different variants, each with their own dependencies.

随着香味的尺寸,你可以处理支持库VS事件库的组合,虽然你会明确地得到一些爆炸,如果你添加更多的维度。

With flavor dimensions, you could handle support libs vs event libs combinations though you'd definitively get some explosions if you add more dimensions.

的多变量库的优点是,它更容易跨越的变体的一个子集共享code没有引入更多的子库。你也可以拥有的主要code和味道,你不能用L2 / L3取决于L1具有双向引用。

The advantages of a multi-variant library is that it's much easier to share code across a subset of variants without introducing even more sub libraries. You can also have bi-directional references between the main code and the flavors, which you couldn't have with L2/L3 depending on L1.

至于支承蚂蚁/ Eclipse中,这是明确将被复杂化。我会忽略蚂蚁明确。如果有人只关心命令行构建他们应该已经转移到摇篮。 对于Eclipse的,以及我们将有摇篮的支持在某些时候,但我明白,如果你等不及了。

As for supporting Ant/Eclipse, this is definitively going to be complicated. I would ignore Ant definitively. If someone only cares about command line build they should already move to Gradle. As for Eclipse, well we'll have Gradle support at some point, but I understand if you can't wait.