如何用Gradle管理第三方代码的不同支持库版本? [英] How to manage different support library versions for 3rd party deps with gradle?
问题描述
我有很多第三方库,包含在我的基本应用程序中。我无法控制那些第三方模块包含的支持库。这使得很难在我的应用程序中拥有相同版本的Android支持库。
我可以从每个依赖中排除某些库,我知道使用gradle:
compile('com.test:lib1:1.0.0'){
exclude group:'com.android.support'
}
然而,有大量的支持库并将它们全部排除在外我的第三方库似乎有点过分。
有更好的方法吗?
阅读这篇博文: a href =https://www.devsbedevin.com/android-understanding-gradle-dependencies-and-resolving-conflicts/ =noreferrer> https://www.devsbedevin.com/android-understanding-gradle -dependencies-and-resolving-conflicts /它表明:
configurations.all {
resolutionStrategy {
preferProjectModules()
}
}
然而,当使用我仍g如果我的依赖依赖于不同版本的支持,我该怎么办?如果我的依赖依赖于不同版本的支持,我该怎么办?图书馆?在上面的任何一种情况下,我都会强制第三方库使用它们未使用的特定版本的支持库。我应该使用哪个版本?最新的支持库?所有第三方依赖关系的最小支持库?
这是一个最小的gradle文件示例,显示了第三方依赖关系,每个依赖关系取决于它们自己的支持库版本。 / p>
android {
compileSdkVersion 26
buildToolsVersion '26 .0.2'
defaultConfig {
applicationIdcom.example.app
minSdkVersion 17
targetSdkVersion 25
}
}
configurations.all {
resolutionStrategy {
preferProjectModules()
}
}
依赖关系{
compile'com.android.support:support-v13:26.0.0'
编译'com.test:lib1:1.0'//取决于support-v13:25.0.0
编译'com.test:lib2:1.0'//取决于support-v13:25.2.0
编译'com.test:lib3:1.0'//取决于support-v13:25.4.0
编译'com.test:lib4:1.0'//取决于support-v13:26.0.0
}
Android Studio提供以下警告:
这当然是可以的。在您的项目build.gradle文件(顶级build.gradle文件)中添加以下代码块:
ext {
supportlib_version = '26 .1.0'
gps_version = '11 .2.0'
}
//确保所有依赖关系使用相同版本的Android支持库
子项目{
project.configurations.all {
resolutionStrategy.eachDependency {details - >
if(details.requested.group =='com.android.support'
&&!details.requested.name.contains('multidex')){
details.useVersion $ supportlib_version
}
if(details.requested.group =='com.google.android.gms'
&&!details.requested.name.contains('multidex ')){
details.useVersion$ gps_version
}
}
}
}
以下代码确保'com.android.support'依赖项版本将等于所有依赖项的$ supportlib_version。 'com.google.android.gms'框架也一样。
确保在你的模块的build.gradle文件中,你也使用这些版本来处理你的依赖关系。例如:
compilecom.android.support:support-v4:$supportlib_version
阅读官方Gradle文档中关于强制某个依赖版本的更多信息: https://docs.gradle.org/current/javadoc/org/gradle /api/artifacts/ResolutionStrategy.html#force-java.lang.Object...
I have a bunch of third party libs that I include in my base application. I cannot control the support library those 3rd party modules include. This makes it hard to have the same version of the android support library in my application.
I know using gradle when I can exclude certain libraries from each dependency:
compile('com.test:lib1:1.0.0') {
exclude group: 'com.android.support'
}
However there are a ton of support libraries and excluding them all for each one of my 3rd party libraries seems like overkill.
Is there a better way?
Reading this blog post: https://www.devsbedevin.com/android-understanding-gradle-dependencies-and-resolving-conflicts/
It suggests:
configurations.all {
resolutionStrategy {
preferProjectModules()
}
}
However when using that I still get a warning in Android Studio in my gradle file that there are multiple versions of the support library detected.
What do I do if my dependencies depend on different versions of the support library? In either case above I would be forcing the 3rd party libraries to use a specific version of the support library that they were not build with. Which version am I supposed to use? Latest support library? Min support library of all 3rd party dependencies?
Here is a minimal gradle file example showing pulling in 3rd party dependencies that each depend on their own version of the support library.
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.example.app"
minSdkVersion 17
targetSdkVersion 25
}
}
configurations.all {
resolutionStrategy {
preferProjectModules()
}
}
dependencies {
compile 'com.android.support:support-v13:26.0.0'
compile 'com.test:lib1:1.0' // depends on support-v13:25.0.0
compile 'com.test:lib2:1.0' // depends on support-v13:25.2.0
compile 'com.test:lib3:1.0' // depends on support-v13:25.4.0
compile 'com.test:lib4:1.0' // depends on support-v13:26.0.0
}
Android studio gives the following warning:
This is certainly possible. In your projects build.gradle file (the top level build.gradle file) add the following code block:
ext {
supportlib_version = '26.1.0'
gps_version = '11.2.0'
}
//Ensure that all dependencies use the same version of the Android Support library
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex')) {
details.useVersion "$supportlib_version"
}
if (details.requested.group == 'com.google.android.gms'
&& !details.requested.name.contains('multidex')) {
details.useVersion "$gps_version"
}
}
}
}
The following code ensures that the 'com.android.support' dependency version will be equal to $supportlib_version for all dependencies. The same for the 'com.google.android.gms' framework.
Make sure that in your module's build.gradle file you also use these versions for your dependencies. E.g.:
compile "com.android.support:support-v4:$supportlib_version"
Read more about forcing a certain dependency version in the Official Gradle documentation: https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ResolutionStrategy.html#force-java.lang.Object...-
这篇关于如何用Gradle管理第三方代码的不同支持库版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!