美文网首页Android技术知识
Android添加fragment后版本不兼容问题

Android添加fragment后版本不兼容问题

作者: Michaelbest1 | 来源:发表于2017-12-21 11:00 被阅读1096次

昨天学习了下Android的Fragment。按照官网的教程,新建了一个类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的。看日志明显是和版本有关啊!后来终于找到个和版本兼容有关的解决方案,要在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++的风格,函数声明是Python的风格,Lambda表达式又有点像C#的。有空看看。

相关文章

网友评论

    本文标题:Android添加fragment后版本不兼容问题

    本文链接:https://www.haomeiwen.com/subject/lpbowxtx.html