RecyclerView 和 CardView 不支持 compileSdkVersion

RecyclerView and CardView don't support compileSdkVersion

最近我在 gradle 文件中实现了 RecyclerViewCardView 但它们的版本不匹配 compileSdkVersion 这是我的 build.gradle 文件:

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.mostafa.tostrategies"
        minSdkVersion 24
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:cardview-v7:28.0.0-alpha1'
    implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1'

}

支持库的主要版本以及recyclerviewcardview必须等于compileSdkVersion

例如,如果您使用 recyclerview-v7:xx.yy.zz,您的 compileSdkVersion 必须是 xx

替换这些

implementation 'com.android.support:cardview-v7:28.0.0-alpha1'
implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1'

implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'

您可以在 official doc 中查看:

Note: 28.0.0-alpha1 is a pre-release version to support the Android P developer preview.

要全面测试您的应用与 AndroidP 的兼容性并开始使用您必须使用的新 API:

android {
    compileSdkVersion 'android-P'

    defaultConfig {
        targetSdkVersion 'P'
    }
    ...
}

还要注意使用相同版本的支持库。

相反,如果您想使用 v26 版本,只需使用:

implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:appcompat-v7:26.1.0'