Java tutorial
//package com.java2s; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.Signature; import java.io.ByteArrayInputStream; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import javax.security.auth.x500.X500Principal; public class Main { private static boolean isDebugCertificateCheck(final Context context) { final X500Principal DEBUG_CERTIFICATE_DN = new X500Principal("CN=Android Debug,O=Android,C=US"); boolean debuggable = false; try { Signature[] signatures = getSignatures(context); for (int i = 0; i < signatures.length; i++) { X509Certificate certificate = generateX509CertificateFromSignature(signatures[i]); debuggable = certificate.getSubjectX500Principal().equals(DEBUG_CERTIFICATE_DN); if (debuggable) { break; } } } catch (PackageManager.NameNotFoundException e) { // package not found - debuggable = false } catch (CertificateException e) { // certificate factory non-instantiable - debuggable = false } return debuggable; } private static Signature[] getSignatures(final Context context) throws PackageManager.NameNotFoundException { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(getPackageName(context), PackageManager.GET_SIGNATURES); Signature[] signatures = packageInfo.signatures; return signatures; } private static X509Certificate generateX509CertificateFromSignature(final Signature signature) throws CertificateException { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); ByteArrayInputStream inputStream = new ByteArrayInputStream(signature.toByteArray()); X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream); return certificate; } /** * Get the application package name. * * @param context Context * @return package name */ public static String getPackageName(final Context context) { return context.getPackageName(); } }