1414import android .os .Build ;
1515import android .util .Log ;
1616
17+ import androidx .annotation .NonNull ;
18+ import androidx .annotation .Nullable ;
19+
1720import java .io .File ;
1821import java .util .ArrayList ;
1922import java .util .List ;
2023
21- import androidx .annotation .NonNull ;
22- import androidx .annotation .Nullable ;
23-
2424/**
2525 * <pre>
2626 * author: Blankj
@@ -541,6 +541,65 @@ public static int getAppVersionCode(final String packageName) {
541541 }
542542 }
543543
544+ /**
545+ * Return the application's minimum sdk version code.
546+ *
547+ * @return the application's minimum sdk version code
548+ */
549+ public static int getAppMinSdkVersion () {
550+ return getAppMinSdkVersion (Utils .getApp ().getPackageName ());
551+ }
552+
553+ /**
554+ * Return the application's minimum sdk version code.
555+ *
556+ * @param packageName The name of the package.
557+ * @return the application's minimum sdk version code
558+ */
559+ public static int getAppMinSdkVersion (final String packageName ) {
560+ if (UtilsBridge .isSpace (packageName )) return -1 ;
561+ if (android .os .Build .VERSION .SDK_INT < android .os .Build .VERSION_CODES .N ) return -1 ;
562+ try {
563+ PackageManager pm = Utils .getApp ().getPackageManager ();
564+ PackageInfo pi = pm .getPackageInfo (packageName , 0 );
565+ if (null == pi ) return -1 ;
566+ ApplicationInfo ai = pi .applicationInfo ;
567+ return null == ai ? -1 : ai .minSdkVersion ;
568+ } catch (PackageManager .NameNotFoundException e ) {
569+ e .printStackTrace ();
570+ return -1 ;
571+ }
572+ }
573+
574+ /**
575+ * Return the application's target sdk version code.
576+ *
577+ * @return the application's target sdk version code
578+ */
579+ public static int getAppTargetSdkVersion () {
580+ return getAppTargetSdkVersion (Utils .getApp ().getPackageName ());
581+ }
582+
583+ /**
584+ * Return the application's target sdk version code.
585+ *
586+ * @param packageName The name of the package.
587+ * @return the application's target sdk version code
588+ */
589+ public static int getAppTargetSdkVersion (final String packageName ) {
590+ if (UtilsBridge .isSpace (packageName )) return -1 ;
591+ try {
592+ PackageManager pm = Utils .getApp ().getPackageManager ();
593+ PackageInfo pi = pm .getPackageInfo (packageName , 0 );
594+ if (null == pi ) return -1 ;
595+ ApplicationInfo ai = pi .applicationInfo ;
596+ return null == ai ? -1 : ai .targetSdkVersion ;
597+ } catch (PackageManager .NameNotFoundException e ) {
598+ e .printStackTrace ();
599+ return -1 ;
600+ }
601+ }
602+
544603 /**
545604 * Return the application's signature.
546605 *
@@ -721,6 +780,8 @@ private static List<String> getAppSignaturesHash(final String packageName, final
721780 * <li>path of package</li>
722781 * <li>version name</li>
723782 * <li>version code</li>
783+ * <li>minimum sdk version code</li>
784+ * <li>target sdk version code</li>
724785 * <li>is system</li>
725786 * </ul>
726787 *
@@ -740,6 +801,8 @@ public static AppInfo getAppInfo() {
740801 * <li>path of package</li>
741802 * <li>version name</li>
742803 * <li>version code</li>
804+ * <li>minimum sdk version code</li>
805+ * <li>target sdk version code</li>
743806 * <li>is system</li>
744807 * </ul>
745808 *
@@ -829,27 +892,34 @@ private static AppInfo getBean(final PackageManager pm, final PackageInfo pi) {
829892 String packageName = pi .packageName ;
830893 ApplicationInfo ai = pi .applicationInfo ;
831894 if (ai == null ) {
832- return new AppInfo (packageName , "" , null , "" , versionName , versionCode , false );
895+ return new AppInfo (packageName , "" , null , "" , versionName , versionCode , - 1 , - 1 , false );
833896 }
834897 String name = ai .loadLabel (pm ).toString ();
835898 Drawable icon = ai .loadIcon (pm );
836899 String packagePath = ai .sourceDir ;
900+ int minSdkVersion = -1 ;
901+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .N ) {
902+ minSdkVersion = ai .minSdkVersion ;
903+ }
904+ int targetSdkVersion = ai .targetSdkVersion ;
837905 boolean isSystem = (ApplicationInfo .FLAG_SYSTEM & ai .flags ) != 0 ;
838- return new AppInfo (packageName , name , icon , packagePath , versionName , versionCode , isSystem );
906+ return new AppInfo (packageName , name , icon , packagePath , versionName , versionCode , minSdkVersion , targetSdkVersion , isSystem );
839907 }
840908
841909 /**
842910 * The application's information.
843911 */
844912 public static class AppInfo {
845913
846- private String packageName ;
847- private String name ;
914+ private String packageName ;
915+ private String name ;
848916 private Drawable icon ;
849- private String packagePath ;
850- private String versionName ;
851- private int versionCode ;
852- private boolean isSystem ;
917+ private String packagePath ;
918+ private String versionName ;
919+ private int versionCode ;
920+ private int minSdkVersion ;
921+ private int targetSdkVersion ;
922+ private boolean isSystem ;
853923
854924 public Drawable getIcon () {
855925 return icon ;
@@ -907,14 +977,31 @@ public void setVersionName(final String versionName) {
907977 this .versionName = versionName ;
908978 }
909979
910- public AppInfo (String packageName , String name , Drawable icon , String packagePath ,
911- String versionName , int versionCode , boolean isSystem ) {
980+ public int getMinSdkVersion () {
981+ return minSdkVersion ;
982+ }
983+
984+ public void setMinSdkVersion (int minSdkVersion ) {
985+ this .minSdkVersion = minSdkVersion ;
986+ }
987+
988+ public int getTargetSdkVersion () {
989+ return targetSdkVersion ;
990+ }
991+
992+ public void setTargetSdkVersion (int targetSdkVersion ) {
993+ this .targetSdkVersion = targetSdkVersion ;
994+ }
995+
996+ public AppInfo (String packageName , String name , Drawable icon , String packagePath , String versionName , int versionCode , int minSdkVersion , int targetSdkVersion , boolean isSystem ) {
912997 this .setName (name );
913998 this .setIcon (icon );
914999 this .setPackageName (packageName );
9151000 this .setPackagePath (packagePath );
9161001 this .setVersionName (versionName );
9171002 this .setVersionCode (versionCode );
1003+ this .setMinSdkVersion (minSdkVersion );
1004+ this .setTargetSdkVersion (targetSdkVersion );
9181005 this .setSystem (isSystem );
9191006 }
9201007
@@ -928,6 +1015,8 @@ public String toString() {
9281015 "\n app path: " + getPackagePath () +
9291016 "\n app v name: " + getVersionName () +
9301017 "\n app v code: " + getVersionCode () +
1018+ "\n app v min: " + getMinSdkVersion () +
1019+ "\n app v target: " + getTargetSdkVersion () +
9311020 "\n is system: " + isSystem () +
9321021 "\n }" ;
9331022 }
0 commit comments