Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Get TargetSDKVersion in runtime

Tags:

android

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.

like image 981
zed Avatar asked Feb 02 '15 14:02

zed


People also ask

What is the difference between compileSdkVersion and targetSdkVersion?

Even if the compileSdkVersion and targetSdkVersion have completely different meanings they are obviously not independent. targetSdkVersion cannot be higher than the compileSdkVersion simply because we cannot target things that we know nothing about during compilation.

What is minSdkVersion and targetSdkVersion in Android?

android:minSdkVersion — Specifies the minimum API Level on which the application is able to run. The default value is "1". android:targetSdkVersion — Specifies the API Level on which the application is designed to run.

How do I check minSdkVersion?

check it: Android Studio->file->project structure->app->flavors->min sdk version and if you want to run your application on your mobile you have to set min sdk version less than your device sdk(API) you can install any API levels.

How do I find my Android API level?

If you are on at least API version 4 (Android 1.6 Donut), the current suggested way of getting the API level would be to check the value of android. os. Build. VERSION.


3 Answers

In my case, I've done this

int targetSdkVersion = getApplicationContext().getApplicationInfo().targetSdkVersion;
like image 50
Kasim Rangwala Avatar answered Oct 12 '22 13:10

Kasim Rangwala


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;
    }
}

OR

If we talk about device OS version

Build class contain information about version

android.os.Build.VERSION.SDK_INT
like image 10
Kirill Shalnov Avatar answered Oct 12 '22 12:10

Kirill Shalnov


This is another way to get the targetSdkVersionof my application in runtime, using Android Studio:

try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            int targetSdkVersion = packageInfo.applicationInfo.targetSdkVersion;

        }
        catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }

the value of targetSDKVersion is defined into my build.gradle file

   defaultConfig {
        applicationId "com.tuna.hello.androidstudioapplication"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 12
        versionName "1.0"
    }
like image 7
Jorgesys Avatar answered Oct 12 '22 12:10

Jorgesys