使用不同版本的Android支持库的解决方法 [英] Workaround for Using Different Versions of Android Support Libraries

查看:429
本文介绍了使用不同版本的Android支持库的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参考在build.gradle文件中出现的警告消息:

This is in reference to a warning message appearing in build.gradle file:

所有com.android.support库必须使用完全相同的版本 规范(混合版本可能会导致运行时崩溃)

All com.android.support libraries must use the exact same version specification (mixing versions may lead to runtime crashes)

我很清楚这一点,并在自己的代码/构建中使用相同的版本.但是,当我使用某些第三方库时,情况并非如此.

I'm well aware of this and use identical versions in my own code / build. However this isn't the case when it comes to some 3rd party libraries that I use.

有时第三方库使用较旧的版本,而其他一些库则使用较新的版本-因此更新您的支持库版本将无法解决问题.

Sometimes 3rd party libraries use older versions, and some other ones use newer versions - so updating your version of support libraries will not resolve the issue.

在某些情况下,您可能不想升级正在使用的支持库的升级版本.

There are also scenarios where you may not want to up upgrade versions of support libraries you are using.

由于我的代码不可用,因此也不可以使用不同版本的支持库重新编译第三方库.

Recompiling 3rd party libraries with different versions of support libraries isn't an option either since code isn't not available in my case.

当其他引用的第三方库使用不同版本的支持库时,解决此问题的解决方法或建议的方法是什么?

What is the workaround or recommended approach in dealing with this when other referenced 3rd party libraries use different versions of support libraries?

推荐答案

您可以排除所有传递性依存关系,也可以仅一个接一个地排除,然后在build.gradle文件中包含所需的版本.

You can exclude all of the transitive dependencies or just one by one, then include the desired version in your build.gradle file.

使用transitive字段告诉gradle您不希望解决 transitive 依赖项. transitive的文档说:

Use the transitive field to tell gradle you don't want the transitive dependencies to be resolved. The documentation of transitive says:

设置是否应解决此依赖性,包括或排除其传递性依赖性.属于此依存关系的工件本身可能与其他工件具有依存关系.后者称为传递依赖.

Sets whether this dependency should be resolved including or excluding its transitive dependencies. The artifacts belonging to this dependency might themselves have dependencies on other artifacts. The latter are called transitive dependencies.

示例:

compile('com.super.lib:awesome@aar') {
    transitive = false
}


方法2-选择您不想包含的依赖项

使用exclude方法,


Method 2 - Pick which dependencies you don't want to include

Use the exclude method which:

添加排除规则以排除此依赖项的传递依赖项.

Adds an exclude rule to exclude transitive dependencies of this dependency.

示例:

compile('com.super.lib:awesome@aar') {
    exclude group: 'com.dont.want.this', module: 'old-artifact'
}

但是,请注意,这不能保证排除的依赖关系不会被另一个依赖关系引入.如果要从任何地方排除依赖项,请使用配置范围的排除策略:

Note, however, that this does not guarantee that the excluded dependency won't be pulled in by another dependency. If you want to exclude a dependency from everywhere, use a configuration-wide exclusion strategy:

configurations.all {
    exclude group: 'com.dont.want.this', module: 'old-artifact'
}

而且,您不需要同时指定groupmodule名称.此示例来自gradle的JavaDoc:

Also, you don't need to specify both group and module names. This example is from the JavaDoc of gradle:

dependencies {
     compile('org.hibernate:hibernate:3.1') {
         //excluding a particular transitive dependency:
         exclude module: 'cglib' //by artifact name
         exclude group: 'org.jmock' //by group
         exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group
     }
}


警告

只是提醒您更改依赖版本号的危险.

使用此技术更改依赖项版本时,必须彻底测试应用程序,因为新/旧版本的依赖项中可能会发生重大更改或意外行为.很明显,主要版本号的跳跃可能会导致崩溃/意外行为,但是自从lib创建者起,您甚至必须注意版本号的 patch 部分可能曾希望所使用的依赖项版本存在一些错误,并且包含一些修复程序,这些缺陷可能会在错误修复后破坏lib.

When you change dependency versions using this technique, you must test the app thoroughly because there can be breaking changes or unexpected behavior in the newer / older versions of the dependencies. It's pretty obvious that a major version number jump will likely result in crashes / unexpected behavior, but you must pay attention even to the patch part of the version number since the lib creator might have expected that the used version of the dependency had some bugs and included some fixes that might break the lib once the bug is fixed.

这篇关于使用不同版本的Android支持库的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆