Android SDK29中导入RecyclerView依赖:记录一次痛苦的排错历程

我在学习Android开发的过程中,使用RecyclerView控件时,遇到了这样一个问题:无法在build.gradle中正确地导入RecyclerView依赖

正确方法

依次点击File-Project Structure-Dependencies-app-加号-Library Dependency,搜索recyclerview,再点击ok即可

Android SDK29中导入RecyclerView依赖:记录一次痛苦的排错历程_第1张图片

使用时用以下XML语句就可以了

  

 

排错历程

一开始我使用如下语句导入依赖

    implementation 'com.android.support:recyclerview-v7:29.1.1'

这时点击Sync进行同步,会产生如下报错

Version 28 (intended for Android Pie and below) is the last version of the legacy support library, so we recommend that you migrate to AndroidX libraries when using Android Q and moving forward. The IDE can help with this: Refactor > Migrate to AndroidX… less… (Ctrl+F1)
Inspection info:There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).  Issue id: GradleCompatible。

查找资料,原来com.android.support包已经逐渐被谷歌抛弃需要使用更新的AndroidX包

于是我按照报错提示,点击:Refactor->Migrate to AndroidX,将包更换为AndroidX

可是更换之后,编译器仍然在报错,并且一直同步失败

后来我尝试了各种办法,使用官方文档的依赖添加语句

    dependencies {
        def recyclerview_version = "1.1.0"

        implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
        // For control over item selection of both touch and mouse driven selection
        implementation "androidx.recyclerview:recyclerview-selection:$recyclerview_version"
    }
    

仍然同步失败,提示如下

ERROR: Failed to resolve: androidx.recyclerview:recyclerview-selection:1.1.0
Show in Project Structure dialog
Affected Modules: app

我吐了,怎么连官方文档都能失败?

于是我又选择换个思路,既然SDK29中导包失败,所以我选择更低版本的SDK

下载SDK28,然后将gradle.build中的配置更改如下

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "com.example.demo3"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

仍然在报错。。。

 

嘤嘤嘤

在这次排错的过程中我还是学到了不少有关安卓版本的知识的,SDK29与以往真的有不少的变化呀。以后查资料时要多注意

 

你可能感兴趣的:(XML,AndroidStudio)