com.android.support库需与compileSdkVersion匹配吗?版本冲突如何解决?
关于Android Support库版本与compileSdkVersion的匹配问题
首先明确核心规则:所有com.android.support库的版本必须和你的compileSdkVersion完全一致,既不能更高也不能更低。Support库是和对应版本的Android SDK绑定开发的,版本不匹配会触发Lint检测报错(就是你看到的红线提示),甚至可能引发运行时兼容性问题。
针对你的项目现状,这里有两种可行的解决方案:
方案一:升级compileSdkVersion到25,统一Support库版本
将compileSdkVersion和targetSdkVersion都升级到25,同时把appcompat-v7的版本也调整为25.3.1,让所有Support库版本与SDK版本保持一致。修改后的完整配置代码如下:
android { compileSdkVersion 25 buildToolsVersion "27.0.3" lintOptions{ abortOnError false } defaultConfig { applicationId "com.sangabriel.myrealapp" minSdkVersion 23 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { // Firebase compile 'com.google.firebase:firebase-core:16.0.0' compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' testCompile 'junit:junit:4.12' // Design library for Coordinator Layout and Toolbars compile 'com.android.support:design:25.3.1' }
方案二:降低Support库版本到24.x.x,匹配当前SDK版本
如果你暂时不想升级SDK版本,可以把design库的版本改成和appcompat-v7一致的24.2.1,同样能解决版本不匹配的问题。修改后的依赖部分代码如下:
dependencies { // Firebase compile 'com.google.firebase:firebase-core:16.0.0' compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' // Design library for Coordinator Layout and Toolbars compile 'com.android.support:design:24.2.1' }
额外建议
优先推荐方案一,因为升级到更高的SDK版本能获得更多API支持和系统兼容性优化,同时25版本的Support库也修复了24版本存在的部分bug。另外需要注意,buildToolsVersion可以保持27.0.3,它和compileSdkVersion不需要完全匹配,只要版本不低于compileSdkVersion即可。
内容的提问来源于stack exchange,提问作者Raemarc San Gabriel
相关产品推荐
相关产品推荐

