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.
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.
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.
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.
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.
In my case, I've done this
int targetSdkVersion = getApplicationContext().getApplicationInfo().targetSdkVersion;
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
This is another way to get the targetSdkVersion
of 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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With