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

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

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

去改第三方庫所用的支持庫版本比較麻煩,若是用的庫不少的話工做量很大。這個時候咱們能夠考慮強制讓全部模塊都用相同的支持庫版本。json

在app build.gradle中添加:api

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:1.2.32@aar'

    //Log
    api 'com.orhanobut:logger:2.1.1'
    //數據庫依賴
    api 'org.greenrobot:greendao-generator:3.2.2'
    api 'org.greenrobot:greendao:3.2.2'
}
相關文章
相關標籤/搜索