I have an About box in my App that displays information about the App, the phone and the data it uses. It's very useful when a user has a problem. I can get the phone's SDK version using "android.os.Build.VERSION.SDK_INT". However, I haven't found a way to get the value of "CompileSdkversion" which indicates the SDK version the App was compiled with. This is the value that is set in the build.gradle file.
CodePudding user response:
While the Android OS version varies by user, the compileSdkVersion
does not. For version X.Y.Z
of your app, the compileSdkVersion
is whatever you said it was when you compiled that app version. So long as your about box contains the app version, you know what compileSdkVersion
that you used, if you keep track of that (e.g., check what it was in your version control system).
But, if you really want to have it be available to you at runtime, you have two options.
If your minSdkVersion
is 31
or higher, you can use compileSdkVersion
on ApplicationInfo
. However, most likely, if you are reading this before the year 2026, your minSdkVersion
is lower than that.
For older devices than Android 12, you could add a BuildConfig
field for it, at least with newer versions of the Android Gradle Plugin:
android {
compileSdk 31
defaultConfig {
applicationId "com.commonsware.android.myapplication"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "int", "COMPILE_SDK_VERSION", "$compileSdk"
}
// other stuff goes here
}
This takes your defined value for compileSdk
and hoists it into BuildConfig.COMPILE_SDK_VERSION
, so you can reference it at runtime. This was tested using a scrap Arctic Fox project, using Gradle 7.0.2 and 7.0.3 of the Android Gradle Plugin.
CodePudding user response:
Here is the relationship between the three values:
minSdkVersion (lowest possible) <= targetSdkVersion == compileSdkVersion (latest SDK)
CompileSdkVersion has nothing to do with what devices can and cannot run your app. Usually, you set this to be the latest version of the Android SDK.
And the targetSdkVersion should be fully tested and less or equal to compileSdkVersion.(It depends on your app)
If you are using the features of API level of 26 then you need to use compileSdkVersion 26, the lower version will give you an error. Android supports backward compatibility (i.e. an app compiled on 26 can also run on a phone having API level 26 or lower).
CodePudding user response:
Considering your use-case, wouldn't a better approach be just to show the current app version? If you know the version, you could look up how/when it was created (via git tags, for example) and then find out the SDK version it was compiled with.