14
14
import android .os .Build ;
15
15
import android .util .Log ;
16
16
17
+ import androidx .annotation .NonNull ;
18
+ import androidx .annotation .Nullable ;
19
+
17
20
import java .io .File ;
18
21
import java .util .ArrayList ;
19
22
import java .util .List ;
20
23
21
- import androidx .annotation .NonNull ;
22
- import androidx .annotation .Nullable ;
23
-
24
24
/**
25
25
* <pre>
26
26
* author: Blankj
@@ -541,6 +541,65 @@ public static int getAppVersionCode(final String packageName) {
541
541
}
542
542
}
543
543
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
+
544
603
/**
545
604
* Return the application's signature.
546
605
*
@@ -721,6 +780,8 @@ private static List<String> getAppSignaturesHash(final String packageName, final
721
780
* <li>path of package</li>
722
781
* <li>version name</li>
723
782
* <li>version code</li>
783
+ * <li>minimum sdk version code</li>
784
+ * <li>target sdk version code</li>
724
785
* <li>is system</li>
725
786
* </ul>
726
787
*
@@ -740,6 +801,8 @@ public static AppInfo getAppInfo() {
740
801
* <li>path of package</li>
741
802
* <li>version name</li>
742
803
* <li>version code</li>
804
+ * <li>minimum sdk version code</li>
805
+ * <li>target sdk version code</li>
743
806
* <li>is system</li>
744
807
* </ul>
745
808
*
@@ -829,27 +892,34 @@ private static AppInfo getBean(final PackageManager pm, final PackageInfo pi) {
829
892
String packageName = pi .packageName ;
830
893
ApplicationInfo ai = pi .applicationInfo ;
831
894
if (ai == null ) {
832
- return new AppInfo (packageName , "" , null , "" , versionName , versionCode , false );
895
+ return new AppInfo (packageName , "" , null , "" , versionName , versionCode , - 1 , - 1 , false );
833
896
}
834
897
String name = ai .loadLabel (pm ).toString ();
835
898
Drawable icon = ai .loadIcon (pm );
836
899
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 ;
837
905
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 );
839
907
}
840
908
841
909
/**
842
910
* The application's information.
843
911
*/
844
912
public static class AppInfo {
845
913
846
- private String packageName ;
847
- private String name ;
914
+ private String packageName ;
915
+ private String name ;
848
916
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 ;
853
923
854
924
public Drawable getIcon () {
855
925
return icon ;
@@ -907,14 +977,31 @@ public void setVersionName(final String versionName) {
907
977
this .versionName = versionName ;
908
978
}
909
979
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 ) {
912
997
this .setName (name );
913
998
this .setIcon (icon );
914
999
this .setPackageName (packageName );
915
1000
this .setPackagePath (packagePath );
916
1001
this .setVersionName (versionName );
917
1002
this .setVersionCode (versionCode );
1003
+ this .setMinSdkVersion (minSdkVersion );
1004
+ this .setTargetSdkVersion (targetSdkVersion );
918
1005
this .setSystem (isSystem );
919
1006
}
920
1007
@@ -928,6 +1015,8 @@ public String toString() {
928
1015
"\n app path: " + getPackagePath () +
929
1016
"\n app v name: " + getVersionName () +
930
1017
"\n app v code: " + getVersionCode () +
1018
+ "\n app v min: " + getMinSdkVersion () +
1019
+ "\n app v target: " + getTargetSdkVersion () +
931
1020
"\n is system: " + isSystem () +
932
1021
"\n }" ;
933
1022
}
0 commit comments