Android Studio导入v7的RecyclerView时找不到以及support包与androidx包冲突的问题
问题描述 由于平时用的都是ListView来进行实现,但RecyclerView都没有用过,于是准备写个小Demo练练手,由于RecyclerView为v7下的包,于是我在build.gradle中加入语句,build时也没有报错.implementation 'com.android.support:recyclerview-v7:28.0.0' sync时没有错误,显示com...
问题描述
由于平时用的都是ListView来进行实现,但RecyclerView都没有用过,于是准备写个小Demo练练手,由于RecyclerView为v7下的包,于是我在build.gradle中加入语句,build时也没有报错.
implementation 'com.android.support:recyclerview-v7:28.0.0'
sync时没有错误,显示completed successfully
但在xml文件中进行选择RecyclerView的时候发现找不到,只有这两个,上面那个还是RecycleListView也不是我需要的RecyclerView.
问题解决一
RecyclerView组件并没有找到,所以我去External Libraries中去找,也并没有发现v7包,而且几乎所有的开头都是androidx,而我需要的是andorid.support,于是怀疑是这个的问题,在gradle.properties中发现两句话导致我的包都是androix.就是下面那两句话,看字面意思也可以知道,于是乎把这两句去掉在进行编译.
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
成功!!!!,当然我一开始并没有发现这个问题,毕竟都是系统生成的,其他人都用的好好的,我也没有多想,不过为什么我的生生成这两句话我也并不是很清楚.
新的问题
当我将RecyclerView在代码中使用起来时,一切准备就绪,运行时居然报了错,而且从来没见过,也不知道是什么意思,在网上寻找了一番后,发现和报错的红字一样,在AndroidManifest.xml中配置个属性.
android:appComponentFactory=""
tools:replace="android:appComponentFactory"
在配置中加了这个属性后,这个错误是没有了,但是新的错误就出现了,哭了,大片大片的红色,错误如下.
一波未平,一波又起,当然了,经过我的思考以及网上的错误,我得出了一个结论,由于我在新建项目时默认勾选了"Use androidx.* artifacts",导致我项目中的库都为anroidx的
最终解决方法
但是刚刚我在解决v7包找不到时将build.properties中的那两句话删掉了,导致我的support包都导入成功,这也就是为什么我可以用v7的RecyclerView了,但是原本存在的androidx和现在的support包会发生冲突,要么都是androidx,要么都是support,而且新建项目也不可行了,一旦勾选了那个选项后,后面就变成灰色默认了,我太难了,于是寻找解决之道,发现v7的RecyclerView和anroidx里有相应的版本对应,于是乎,哈哈,将所有的都换成androidx里的,并且将build.properties的那两句话还原.
android.useAndroidX=true
android.enableJetifier=true
原来导入v7的RecyclerView语句是
implementation 'com.android.support:recyclerview-v7:28.0.0'
在androidx中对应的是
implementation 'androidx.recyclerview:recyclerview:1.0.0'
所以我将build.gradle中的进行修改,改为下面这句话.运行成功!!!,终于解决了这个问题,以下为support库和anroidx库的依赖库的对应列表,当然自己在网上也可以查到,以后可以只使用androidx的包来进行操作了,这样就不会有那么多问题了.
支持库的旧路径 | 支持库的新路径 |
---|---|
com.android.support.constraint:constraint-layout | androidx.constraintlayout:constraintlayout:1.1.2 |
com.android.support.test.espresso:espresso-core | androidx.test.espresso:espresso-core:3.1.0 |
com.android.support.test:runner | androidx.test:runner:1.1.0 |
com.android.support:appcompat-v7 | androidx.appcompat:appcompat:1.0.0 |
com.android.support:cardview-v7 | androidx.cardview:cardview:1.0.0 |
com.android.support:design | com.google.android.material:material:1.0.0-rc01 |
com.android.support:multidex | androidx.multidex:multidex:2.0.0 |
com.android.support:palette-v7 | androidx.palette:palette:1.0.0 |
com.android.support:recyclerview-v7 | androidx.recyclerview:recyclerview:1.0.0 |
com.android.support:support-v4 | androidx.legacy:legacy-support-v4:1.0.0 |
support库和androidx库的控件的对应关系如下:
支持控件的旧包名 | 支持控件的旧包名 |
---|---|
android.support.v4.app.Fragment | androidx.fragment.app.Fragment |
android.support.v4.app.FragmentActivity | androidx.fragment.app.FragmentActivity |
android.support.v4.app.FragmentManager | androidx.fragment.app.FragmentManager |
android.support.v4.app.FragmentPagerAdapter | androidx.fragment.app.FragmentPagerAdapter |
android.support.v4.view.ViewPager | androidx.viewpager.widget.ViewPager |
android.support.v4.view.PagerAdapter | androidx.viewpager.widget.PagerAdapter |
android.support.v4.view.PagerTabStrip | androidx.viewpager.widget.PagerTabStrip |
android.support.v4.view.PagerTitleStrip | androidx.viewpager.widget.PagerTitleStrip |
android.support.v7.app.AppCompatActivity | androidx.appcompat.app.AppCompatActivity |
android.support.v7.widget.Toolbar | androidx.appcompat.widget.Toolbar |
android.support.v7.widget.RecyclerView | androidx.recyclerview.widget.RecyclerView |
android.support.v7.widget.GridLayoutManager | androidx.recyclerview.widget.GridLayoutManager |
android.support.v7.widget.LinearLayoutManager | androidx.recyclerview.widget.LinearLayoutManager |
android.support.v7.widget.StaggeredGridLayoutManager | androidx.recyclerview.widget.StaggeredGridLayoutManager |
android.support.v7.widget.CardView | androidx.cardview.widget.CardView |
android.support.v7.graphics.Palette | androidx.palette.graphics.Palette |
更多推荐
所有评论(0)