1. 程式人生 > >解決支援庫版本相容問題:引入包時候support包有紅色下劃線

解決支援庫版本相容問題:引入包時候support包有紅色下劃線

如果引用的第三方庫的支援庫版本低於(或者不一致)app build.gradle中的支援庫版本,可能會出現如下問題:

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

如下圖所示:

螢幕快照 2017-09-10 12.58.38.png

去改第三方庫所用的支援庫版本比較麻煩,如果用的庫很多的話工作量很大。這個時候我們可以考慮強制讓所有模組都用相同的支援庫版本。

在app build.gradle中新增:

configurations.all{
    resolutionStrategy.eachDependency{ DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

其中,27.1.1就是你要使用的支援庫版本號,你可以根據需要改成其它的。附上 build.gradle 檔案

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'

android {
    compileSdkVersion 27


    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    //強制讓所有模組都用相同的支援庫版本
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == 'com.android.support') {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion '27.1.1'
                }
            }
        }
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    testImplementation 'junit:junit:4.12'
    api 'com.android.support.test:runner:1.0.2'
    api 'com.android.support.test.espresso:espresso-core:3.0.2'
    api 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation project(':mavo-annotations')

    //Android Support包
    api 'com.android.support:design:27.1.1'
    api 'com.android.support:appcompat-v7:27.1.1'
    api 'com.android.support:support-v4:27.1.1'

    //字型圖示
    api 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2'
    api 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2'

    //fragmentation
    api 'me.yokeyword:fragmentation:1.3.6'
    api 'me.yokeyword:fragmentation-swipeback:1.3.6'

    //Butter Knife
    api 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    //網路請求依賴
    api 'com.squareup.okio:okio:1.13.0'
    api 'com.squareup.okhttp3:okhttp:3.8.1'
    api 'com.squareup.retrofit2:retrofit:2.3.0'
    api 'com.squareup.retrofit2:converter-scalars:2.3.0'

    //AVLoadingIndicatorView
    api 'com.wang.avi:library:2.1.3'

    //JSON依賴Android版
    api 'com.alibaba:fastjson:1.1.57.android'

    //banner依賴
    api 'com.bigkoo:convenientbanner:2.0.5'
    api 'com.ToxicBakery.viewpager.transforms:view-pager-transforms:
[email protected]
' //Log api 'com.orhanobut:logger:2.1.1' //資料庫依賴 api 'org.greenrobot:greendao-generator:3.2.2' api 'org.greenrobot:greendao:3.2.2' }