如何判断Android个App的SDK级别?

How To Determine SDK Level of Android App?

我正在尝试从我的应用程序中确定 Build SDK 级别。我试过了

Log.i( "MYAPP", "SDK="+Build.VERSION.SDK_INT );

我之前一直在建造 28 级,但最近升级到 30 级。
我模块的当前 build.gradle 文件包括:

android {
    compileSdkVersion      30 // was 28
    defaultConfig {
        applicationId "com.ramrod.MyApp"
        minSdkVersion 23
        targetSdkVersion 30 // was 28  
        more ...  

奇怪,logcat还是显示28级
我已经重新同步、清理并重建了应用程序,但没有任何变化。

怎么回事 ?

正如您在 documentation here、SDK_INT returns 中看到的 OS 版本的设备中应用程序 运行 而不是您要查找的 targetSdkVersion。

public static final int SDK_INT

The SDK version of the software currently running on this hardware device.
This value never changes while a device is booted, but it may increase when the hardware manufacturer provides an OTA update.

要获取 targetSdkVersion 你需要使用 ApplicationInfo.targetSdkVersion class:

  packageManager.getApplicationInfo(youPackage, 0).targetSdkVersion;