Skip to content
DeveloperMemos

How to Get Your App's Target SDK Version in Kotlin

Kotlin, Android Development, SDK Version1 min read

To get the target SDK version of your app in Kotlin, you can use the targetSdkVersion attribute in the ApplicationInfo class. This attribute represents the target SDK version that the application is targeting.

To get the ApplicationInfo instance for your app, you can use the applicationInfo property of the Context class, which provides access to information about the package of an Android app.

Here is an example of how to get the target SDK version of your app in Kotlin using Context:

1import android.content.Context
2
3fun getTargetSdkVersion(context: Context): Int {
4 return context.applicationInfo.targetSdkVersion
5}

In the example above, we use the applicationInfo property of the Context class to get the ApplicationInfo instance for the app. Then, we return the targetSdkVersion attribute of the ApplicationInfo instance, which represents the target SDK version that the app is targeting.

You can then use this function to get the target SDK version of your app in any part of your code. For example, you can use it to check if the app is targeting a specific SDK version or to display the target SDK version to the user.