Android 中支持旧版 API 的方法(API 30)

发布于:2025-04-14 ⋅ 阅读:(77) ⋅ 点赞:(0)

Android 中最新依赖库的版本支持 API 31 及以上版本,若要支持 API30,则对应的依赖库的版本就需要使用旧版本。
可通过修改模块级 build.gradle 文件来进行适配。

1、android 标签的 targetSdk 和 compileSdk 版本号

根据实际目标设备的 android 版本来确定版本号,这里设备是 Android 11 版本的,故将targetSdk 和 compileSdk 设置为 30。

android {
    namespace 'com.example.helloworld'
    compileSdk 30

    defaultConfig {
        applicationId "com.example.helloworld"
        minSdk 23
        targetSdk 30
        versionCode 1
        versionName "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'
	
	// 协程 lifecycleScope
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
}

3、查询依赖库旧版本号

可在官网上查询某个依赖库的旧版本号信息。
网站:https://developer.android.google.cn/jetpack/androidx/explorer

在这里插入图片描述