【问题标题】:Do not duplicate dependencies不要重复依赖
【发布时间】:2019-12-07 22:20:38
【问题描述】:

我有一个包含下一个依赖项的库:

implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.2'

现在我需要使用这个库将相同的依赖项添加到我的主项目中。

如何实现该库,以便所有这些依赖项不会在主项目中重复?

现在我在我的项目中写:

implementation 'com.mandarine.sdk:mandarine-library:2.0.1@aar'
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'  
implementation 'com.squareup.retrofit2:retrofit:2.5.0'  
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'    
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.2'//Don't update while support Android SDK < 20

在图书馆:

implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.2'

更新: 还尝试在我的项目中写下一个:

api 'com.mandarine.sdk:mandarine-library:2.0.1@aar'

并像这样修改了我的库:

api 'com.google.android.gms:play-services-safetynet:17.0.0'
api 'com.squareup.retrofit2:retrofit:2.5.0'
api 'com.squareup.retrofit2:converter-gson:2.5.0'
api 'com.squareup.okhttp3:logging-interceptor:3.12.2'

不幸的是也不起作用。

【问题讨论】:

  • 把工作留给 gradle 来处理。只要确保您引用的版本与您的库也引用的版本相同。 Gradle 将进行合并,不会有重复。只有版本不匹配或类冲突时才需要排除。
  • @GiddyNaya 版本是正确的,但如果我删除这些依赖项,那么应用程序将崩溃
  • 使用项目的依赖项会出现什么错误?
  • 尝试在项目上启用 Multidex,然后清理并重建项目

标签: android gradle android-gradle-plugin dependencies android-library


【解决方案1】:

从每个库中排除您不想包含的依赖项

implementation('com.your.library') {
    exclude module: 'library1'
    exclude module: 'library2'
}

【讨论】:

  • 排除模块:'library1' ?例如,这是我的改装?
  • 排除模块:'retrofit2'
  • 这个想法不是在项目本身中声明依赖项,而是使用库中的那些。排除他们有什么意义?
  • 那么删除你项目中声明的那些。
  • 例如,如果我删除改造 2,我会收到下一个错误 gist.github.com/mnewlive/e0269a1f1b2a5062c6c16beb8329d268
【解决方案2】:

用下一个解决方案解决的问题:

implementation ('com.saltedge.sdk:saltedge-library:2.0.2@aar') {
        transitive = true
    }

这有助于我在库中使用依赖项,并且不要在主项目中复制它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2016-09-30
    • 2014-10-24
    • 1970-01-01
    • 2021-08-05
    • 2019-07-05
    相关资源
    最近更新 更多