简体   繁体   English

在Android上分叉gradle依赖项

[英]Forking gradle dependencies on Android

I have not developed Android for a while and I am trying to do things right with the new features as the Gradle dependencies. 我有一段时间没有开发Android,我正在尝试使用Gradle依赖项的新功能做正确的事情。

I am wondering what are the best practices when you need to modify one particular dependency for suiting you needs. 我想知道当您需要修改一个特定的依赖项以满足您的需求时,最佳实践是什么。

For example, let's say we have two libraries that extend the RecyclerView functionality by subclassing the Adapter: 例如,假设我们有两个库通过子类化适配器来扩展RecyclerView功能:

Adapter --> AdapterA
Adapter --> AdapterB

Since Java does not support multiple inheritances, I guess the only way to make both libraries work together would consist on modifying one of them so the subclassing hierarchy looks like this: 由于Java不支持多重继承,我想让两个库协同工作的唯一方法就是修改其中一个,这样子类层次结构如下所示:

Adapter --> AdapterA --> AdapterB

If I am correct and this is the way to go, what would then be the best way of modifying and integrating the library, here are the options I can think of: 如果我是正确的,这是要走的路,那么什么是修改和集成库的最佳方法,这里是我能想到的选项:

  1. Fork on GitHub and use JitPack to add the forked-modified library 在GitHub上分叉并使用JitPack添加分叉修改的库
  2. Add library as a local module to the app and modify source 将库作为本地模块添加到应用程序并修改源

Please justify or suggest what is better and if there are any other alternatives to achieve this. 请证明或建议什么是更好的,如果有任何其他替代方案来实现这一点。

I say just make a local module which depends on the other library and modify that. 我说只需要创建一个依赖于其他库的本地模块并修改它。 Don't forget to record which changes you've made. 不要忘记记录您所做的更改。 Ideally, you import the module as a separate commit in the VCS, and then you modify it in another commit. 理想情况下,您将模块作为VCS中的单独提交导入,然后在另一个提交中对其进行修改。

This has the advantage that if you want to update the library, it will be fairly easy to do since all you have to do is copy over the source for the new version and then make the same modifications. 这样做的好处是,如果您想要更新库,那么这将非常容易,因为您只需要复制新版本的源代码,然后进行相同的修改。 However, if you take the other approach, then you'd also have to push all the changes to GitHub, which adds complexity, but little benefit unless you want to share your modifications with someone else. 但是,如果采用其他方法,那么您还必须将所有更改推送到GitHub,这会增加复杂性,但除非您想与其他人共享修改,否则几乎没有任何好处。

This seems like an unusual circumstance - Maybe an example would help 这似乎是一个不寻常的情况 - 也许一个例子会有所帮助
Also consider composition before resorting to forking libraries. 在使用分叉库之前还要考虑构图。 https://en.wikipedia.org/wiki/Composition_over_inheritance https://en.wikipedia.org/wiki/Composition_over_inheritance

class YourAdapter {
    private AdapterA adapterA
    private AdapterB adapterB
}

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

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