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

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

正确方法

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

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

 <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> 

 

排错历程

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

    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与以往真的有不少的变化呀。以后查资料时要多注意

 

  • 14
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值