How to manage different support library versions for 3rd party deps with gradle?

前端 未结 4 578
误落风尘
误落风尘 2020-12-12 19:33

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 t

相关标签:
4条回答
  • 2020-12-12 19:54

    Make the targetsdkversion 26 and also in your gradle make changes to 26 or u can change your compilesdk version to 25 and change your relevant dependecies to 25

    0 讨论(0)
  • 2020-12-12 19:56

    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.

    Update Google has decoupled library versions. Therefore, forcing a specific version above 15.0.0 may not work. Instead, you can allow a limited range of versions. The example below allows any version higher than 15.0.0 but lower than 16.

    gps_version = '[15.0.0, 16.0.0)'
    
    0 讨论(0)
  • 2020-12-12 20:03

    First case: You have compatible libs wich already updated their own internal libs, No problem here.

    Second case: You have libs in your project which have higher version than other libs contained internal to other libs, and these libs can be updated to new version with no such problem, Also No problem here.

    Worst case: You have libs in your project which have higher version than other libs contained internal to other libs, and these libs doesn't have a new version that has already updated there internal libs, suggested solutions for that:

    1. Download that library and include it to your app as project implementation project(':library') and update their internal libs.
    2. OR Use other library instead.

    Don't forget to use ./gradlew app:dependencies to check your dependencies.

    Also I believe should be there someway to do that automatically.

    0 讨论(0)
  • 2020-12-12 20:11

    You need to specify dependency, with your desired version, that is causing the conflict before the libs that need it. If you do that these lib will use your specified dependency version.

    Example with libs (from warning message) from your screenshot

    dependencies {
        compile 'com.android.support:support-v13:26.0.0'
        compile 'com.android.support:support-compat:26.0.0'
        compile 'com.android.support:animated-vector-drawable: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
    }
    

    Continue adding dependencies (that show up in the warning from your screenshot) until there is no longer any warning message about different library versions.

    0 讨论(0)
提交回复
热议问题