Android 获取应用签名
本文主要讲下在android中如何获取应用签名. 也方便平时用来区分一个应用是不是原包应用.
1: 通过PackageManager获取签名信息
首先,通过packageManager获取到指定应用的PackageInfo. 这里需要传入的flag是PackageManager.GET_SIGNATURES
/**
* {@link PackageInfo} flag: return information about the
* signatures included in the package.
*
* @deprecated use {@code GET_SIGNING_CERTIFICATES} instead
*/
@Deprecated
public static final int GET_SIGNATURES = 0x00000040;
PackageManager pm = getPackageManager();
List<ApplicationInfo> applications = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : applications) {
try {
PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_SIGNATURES);
Signature[] signs = packageInfo.signatures;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
2: 获取SHA1
public class ShaUtils {
public static String getSHA1(byte[] sig) {
final char[] hexArray = "0123456789ABCDEF".toCharArray();
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] digest = md.digest(sig);
char[] hexChars = new char[digest.length * 2];
for (int j = 0; j < digest.length; j++) {
int v = digest[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
} catch (Throwable e) {
e.printStackTrace();
}
return "";
}
}
下面是整理的一个小demo,展示当前设备的应用列表以及应用签名,包名,icon信息.
创建mainacitity
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private ApkAdapter apkAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.grid_sign);
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
apkAdapter = new ApkAdapter(installedApplications, this);
gridView.setAdapter(apkAdapter);
}
}
acitivity布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/grid_sign"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
ApkAdapter代码如下:
public class ApkAdapter extends BaseAdapter {
private List<ApplicationInfo> list;
private Context context;
private PackageManager packageManager;
public ApkAdapter(List<ApplicationInfo> list, Context context) {
this.list = list;
this.context = context;
packageManager = context.getPackageManager();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_apk, parent, false);
}
ApplicationInfo applicationInfo = list.get(position);
ImageView icon = (ImageView) view.findViewById(R.id.item_icon);
icon.setImageDrawable(applicationInfo.loadIcon(packageManager));
TextView label = (TextView) view.findViewById(R.id.item_name);
label.setText(applicationInfo.packageName);
TextView sign = (TextView) view.findViewById(R.id.item_sign);
StringBuilder stringBuilder = new StringBuilder();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_SIGNATURES);
Signature[] signs = packageInfo.signatures;
for (Signature sig : signs) {
String sha1 = ShaUtils.getSHA1(sig.toByteArray());
stringBuilder.append("Signature: "+sha1+"\n");
Log.d("Signature", "packageName = " + packageInfo.packageName + ", signature = " + sha1);
}
sign.setText(stringBuilder.toString());
} catch (Throwable e) {
Log.e("Signature", "getView: ", e);
}
return view;
}
}
以及item的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/item_icon"
android:layout_margin="10dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/item_icon"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="xxx"
android:layout_alignParentTop="true"
android:textColor="@color/black"
android:id="@+id/item_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="14sp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:text="xxxxxxxxx"
android:id="@+id/item_sign"
/>
</RelativeLayout>
</RelativeLayout>
最后展示下效果图: