Android添加fragment后版本不兼容問題

昨天學習了下Android的Fragment。按照官網(wǎng)的教程,新建了一個類BlankFragment,繼承自Fragment。然后編譯的時候就出錯了:

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0-beta1) from [com.android.support:design:26.0.0-beta1] AndroidManifest.xml:28:13-41
    is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
    Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:44 to override.

搜了一下,七七八八說什么的都有。最不靠譜的一個就是說什么把manifest文件里的標簽從android:name改成class的??慈罩久黠@是和版本有關(guān)啊!后來終于找到個和版本兼容有關(guān)的解決方案,要在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.0.0-beta1'
            }
        }
    }
}

試了一下確實好了。但不能滿足于此。
注意到,就在添加的這段代碼上面,描述的是工程依賴的庫:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.0.0-beta1'
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:0.5'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}

而這一行下面有紅線:

implementation 'com.android.support:appcompat-v7:26.0.0-beta1'

鼠標指上去提示:

All com.android.support libraries must use the exact same version specification 
(mixing versions can lead to runtime crashes). 
Found versions 26.1.0, 26.0.0-beta1. Examples include com.android.support:support-compat:26.1.0 
and com.android.support:animated-vector-drawable:26.0.0-beta1

這不是說的很明白:所有依賴com.android.support的庫必須使用一樣的版本!看看Dependency里,com.android.support:appcompat-v7:26.0.0-beta1和com.android.support:support-v4:26.1.0兩個版本就是矛盾的啊!看一下git的記錄,26.1.0這一行就是新加的,肯定就是新建BlankFragment類的時候AndroidStudio自動添加的。再回頭看看出錯日志,說的不也是同一個意思?
不得不吐槽一下AndroidStudio,你既然要自動加版本依賴,就把版本兼容性也一起檢查了唄。不然還不如不要加,讓我們自己來。最討厭這種半自動化的東西,最坑人。
另外,gradle文件的語法也挺有意思。注釋是C/C++的風格,函數(shù)聲明是Python的風格,Lambda表達式又有點像C#的。有空看看。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容