Android 中最新依賴庫的版本支持 API 31 及以上版本,若要支持 API30,則對應的依賴庫的版本就需要使用舊版本。
可通過修改模塊級 build.gradle 文件來進行適配。
1、android 標簽的 targetSdk 和 compileSdk 版本號
根據實際目標設備的 android 版本來確定版本號,這里設備是 Android 11 版本的,故將targetSdk 和 compileSdk 設置為 30。
android {namespace 'com.example.helloworld'compileSdk 30defaultConfig {applicationId "com.example.helloworld"minSdk 23targetSdk 30versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}
}
2、dependencies 標簽中修改依賴庫版本
dependencies {implementation 'androidx.core:core-ktx:1.6.0'implementation 'androidx.appcompat:appcompat:1.3.0'implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.1.3'implementation 'androidx.annotation:annotation:1.3.0'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'// 協程 lifecycleScopeimplementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
}
3、查詢依賴庫舊版本號
可在官網上查詢某個依賴庫的舊版本號信息。
網站:https://developer.android.google.cn/jetpack/androidx/explorer