简体   繁体   English

Gradle 依赖管理:如何在 android 项目中同时维护新旧版本的依赖?

[英]Gradle dependency management: how to maintain both old and new version of a dependency in android project?

I am faced with a situation currently in one of my android projects.我目前在我的一个 android 项目中面临着一种情况。

Situation is:情况是:

Project uses libraries A and B(new version).项目使用库 A 和 B(新版本)。 Also library A depends on library B(old version) as well.库 A 也依赖于库 B(旧版本)。 Both A and B are maintained independently and their release cannot be synced. A 和 B 都是独立维护的,它们的发布不能同步。

The versions of library B old and new are incompatible in the sense that some interface definitions have changed in the new version.新版本库 B 的旧版本和新版本是不兼容的,因为某些接口定义在新版本中发生了变化。 This I think will create some problem while building the project.我认为这会在构建项目时产生一些问题。

What is the best way to maintain both A and B in my android project?在我的 android 项目中同时维护 A 和 B 的最佳方法是什么?

As already stated in the comments there is no out-of-the-box solution for your problem and your best option probably is to make your project use the same old version of library B that is used by library A.正如评论中所述,您的问题没有现成的解决方案,您最好的选择可能是让您的项目使用库 A 使用的相同旧版本的库 B。

Only if you really depend on features or fixes that are only available in the new version of B you could try to use the shadow plugin to build your own version of library B with relocated packages.只有当您真正依赖仅在新版本 B 中可用的功能或修复程序时,您才可以尝试使用shadow 插件来构建您自己的带有重定位包的库 B 版本。 That is:那是:

  • Create a new project eg custom-b with the new version of B as dependency使用新版本的 B 作为依赖项创建一个新项目,例如custom-b
  • Apply the shadow-plugin and relocate the classes of B to another package.应用 shadow-plugin 并将 B 的类重新定位到另一个包。 For example package com.library.b could become custom-b.com.library.b例如包com.library.b可以变成custom-b.com.library.b
  • Publish the shadow-jar of the custom-b project locally and make your main project depend on this jar在本地发布custom-b项目的shadow-jar,让你的主项目依赖这个jar

This way the classes of the new version of B will be available under a different package and could be used by your code.这样,新版本 B 的类将在不同的包下可用,并且可以由您的代码使用。

As stated above you should only try this if you really depend on the new version of B as this introduces some considerable amount of additional complexity which should be avoided if not absolutely necessary.如上所述,如果您真的依赖 B 的新版本,您应该只尝试这样做,因为这会引入一些相当大的额外复杂性,如果不是绝对必要的,应该避免。 Alternatively you could of course also ask the developers of A if they are planning to use the new version of B in one of their coming releases.或者,您当然也可以询问 A 的开发人员,他们是否计划在即将发布的版本之一中使用新版本的 B。 This might solve your problem as well.这也可能解决您的问题。

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

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