Android: get TargetSDKVersion at runtime

Can I use the used TargetSDKVersion at run time?

This is because the WebView in the Android 19> API processes pixels differently than before 19.

This is for the library, so I would not want the developer to enter it manually.

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

+5
source share
4 answers

About the target SDK version, look at 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 the OS version

The assembly class contains version information.

 android.os.Build.VERSION.SDK_INT 
+6
source

This is another way to get the targetSdkVersion my application at 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()); } 

targetSdkVersion value targetSdkVersion defined in build.gradle file

  defaultConfig { applicationId "com.tuna.hello.androidstudioapplication" minSdkVersion 9 targetSdkVersion 22 versionCode 12 versionName "1.0" } 
+5
source

In my case, I did it

 int targetSdkVersion = getApplicationContext().getApplicationInfo().targetSdkVersion; 
+3
source

Having the following configuration on an Android Marshmallow phone (API 23):

 defaultConfig { applicationId "com.mytestapp" minSdkVersion 9 targetSdkVersion 22 versionCode 1 versionName "1.0.0" } 

Another direct approach to targetSdkVersion's actual launch application :

 Context mContext = getApplicationContext(); int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; // Outputs 22 int mobileSdkVersion = Build.VERSION.SDK_INT; // Outputs 23 
+1
source

Source: https://habr.com/ru/post/1212442/


All Articles