Skip to content

Instantly share code, notes, and snippets.

@Den-Rimus
Created January 27, 2015 15:52
Show Gist options
  • Save Den-Rimus/2aaecb5e8ab24d7dc0b6 to your computer and use it in GitHub Desktop.
Save Den-Rimus/2aaecb5e8ab24d7dc0b6 to your computer and use it in GitHub Desktop.
private boolean checkBuildType() {
boolean isDebugBuild = false;
try {
Signature[] signatures = getPackageManager()
.getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES).signatures;
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X500Principal debugSertificateSubject = new X500Principal(DEBUG_CERTIFICATE_SUBJECT_NAME);
for (Signature signature : signatures) {
ByteArrayInputStream stream = new ByteArrayInputStream(signature.toByteArray());
X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
isDebugBuild = cert.getSubjectX500Principal().equals(debugSertificateSubject);
if (isDebugBuild)
break;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Application -> checkBuildType()", "Error while retrieving package data." +
"Run will be treated as of production build");
} catch (CertificateException e) {
Log.e("Application -> checkBuildType()", "Error while retrieving package data. " +
"\"X.509\" certificate is not available." +
"Run will be treated as of production build");
}
return isDebugBuild;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment