Addev
Addev

Reputation: 32233

Retrieve the apk signature at runtime for Android

Is there some way to retrieve information about the apk signature at runtime?

For example I have the same App signed with 2 different signatures: app-1.apk and app-2.apk and I'd like to be able of differentiate both apk in run time. Is that possible?

My goal is to implement a licensing system using a external server and based on the version code of the application and the signature.

Upvotes: 12

Views: 17027

Answers (2)

codemaster
codemaster

Reputation: 572

You can access apk signature using PackageManager. PackageInfo flag defined in Package Manager return information about the signatures included in the package.

Signature[] sigs = context.getPackageManager().getPackageInfo(context.getPackageName(),PackageManager.GET_SIGNATURES).signatures;    
for (Signature sig : sigs)
{
    Log.i("App", "Signature : " + sig.hashCode());
}

http://developer.android.com/reference/android/content/pm/PackageManager.html

Upvotes: 14

Force
Force

Reputation: 6382

This might be a different approach, but why don't you just implement a custom permission, that is signature based? Then you could use the package manager (or a broadcast) to find out if the permission was granted.

If it is, then both signatures are the same, if not they are different.

Upvotes: 4

Related Questions