zed zed - 2 years ago 316
Android Question

Android: Get TargetSDKVersion in runtime

Can I get the used TargetSDKVersion in runtime?

This is due to the fact that the WebView in Android API 19> handles pixels differently than pre 19.

This is for a library and so I would not like to have the developer enter it manually.

My comment:
I am using my Nexus 5 API 21 Lollipop. Changing TargetSDKVersion changes the way javascript of the html reads the widths by a multiple of the screen density. I have just changed it to 14 then to 19, and I confirm this.

Answer Source

About target SDK version, look to the ApplicationInfo class (get it from here)

int version = 0;
IPackageManager pm = AppGlobals.getPackageManager();
try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo(yourAppName, 0);
    if (applicationInfo != null) {
      version = applicationInfo.targetSdkVersion;


If we talk about device OS version

Build class contain information about version

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download