抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

前言

之前的很久时间,我们都是使用以下的方式获取应用的版本号,但在Android SDK 28中,谷歌弃用了该方式,推荐使用getLongVersionCode()

1
2
3
4
5
6
7
8
9
10
public int getSelfVersionCode(Context context){
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo info = packageManager.getPackageInfo(context.getPackageName(),0);
return info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}

但 getLongVersionCode() 仅限API28及以上使用,所以我们需要判断版本号并做出对应处理。

那么,我们可以一步到位吗?

答案是可以的!

PackageInfoCompat

引入依赖

1
2
3
4
5
// Androidx
implementation 'androidx.appcompat:appcompat:1.3.0-rc01'

// Android Support
implementation 'com.android.support:appcompat-v7:28.0.0'

What is PackageInfoCompat?

PackageInfoCompat顾名思义,是用来代替PackageInfo部分功能的,里面仅包含三个静态方法

修饰符 方法参数 方法作用
static
Long getLongVersionCode(
info: PackageInfo) 返回版本号
static MutableList<Signature!>
getSignatures(
packageManager: PackageManager, packageName: String) 获取签名数组
static
Boolean hasSignatures(
packageManager: PackageManager, packageName: String,certificatesAndType: MutableMap<ByteArray!, Int!>, matchExact:
Boolean) 检查设备上的包是否包含一组证书。

使用

getLongVersionCode - 获取版本号,内部实现其实与上文提到的方法无异

1
2
3
4
5
6
7
8
9
10
public long getSelfVersionCode(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo info = packageManager.getPackageInfo(context.getPackageName(), 0);
return PackageInfoCompat.getLongVersionCode(info);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}
1
2
3
4
5
fun getSelfVersionCode(context: Context): Long {
val packageManager = context.packageManager
val info = packageManager.getPackageInfo(context.packageName, 0)
return PackageInfoCompat.getLongVersionCode(info)
}
1
2
3
4
5
6
7
@SuppressWarnings("deprecation")
public static long getLongVersionCode(@NonNull PackageInfo info) {
if (Build.VERSION.SDK_INT >= 28) {
return info.getLongVersionCode();
}
return info.versionCode;
}

getSignatures - 获取应用签名, signatures 在SDK28中被 signingInfo 替代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public String getSelfSignToSHA1(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
List<Signature> signatureList = PackageInfoCompat.getSignatures(packageManager,context.getPackageName());
byte[] cert = signatureList.get(0).toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] publicKey = md.digest(cert);
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < publicKey.length; i++) {
String appendString = Integer.toHexString(0xFF & publicKey[i]).toUpperCase(Locale.US);
if (appendString.length() == 1)hexString.append("0");
hexString.append(appendString);
if (i != publicKey.length - 1) hexString.append(":");
}
return hexString.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fun getSelfSign(context: Context): String {
val packageManager = context.packageManager
val signatures = PackageInfoCompat.getSignatures(packageManager, context.packageName)
val bytes = signatures[0].toByteArray()
val messageDigest = MessageDigest.getInstance("SHA1")
val publicKey: ByteArray = messageDigest.digest(bytes)
val hexString = StringBuilder()
for (i in publicKey.indices) {
val appendString = Integer.toHexString(0xFF and publicKey[i].toInt())
.toUpperCase(Locale.US)
if (appendString.length == 1) hexString.append("0")
hexString.append(appendString)
if (i != publicKey.size - 1) hexString.append(":")
}
return hexString.toString()
}

评论