build.gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 28 //版本较高,用androidx开头的合适,之前在别的项目拷贝了一个android开头的依赖,哪里的sdk设置的版本都较低
buildToolsVersion "29.0.2"//版本较高
defaultConfig {
applicationId "com.example"
minSdkVersion 19 // 应用程序支持api的下限
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'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// RecyclerView 依赖库,(我出现这个问题是因为这个版本与上面sdk设置不兼容,现在的这个为兼容依赖)
implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
对应的xml文件xxx.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffececec">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
此次情景
版本与上面sdk设置不兼容,一下几个参数对版本都做了限制
minSdkVersion 19 //最小版本限制
targetSdkVersion 28 //targetSdkVersion保证的是api的一致性。一般minSdkVersion <targetSdkVersion<= compileSdkVersion