Created
December 18, 2012 13:05
-
-
Save lucasr/4327842 to your computer and use it in GitHub Desktop.
Simple and fairly reliable way of checking if you're running a debug build of your Android app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.lucasr; | |
import java.io.ByteArrayInputStream; | |
import java.security.cert.CertificateFactory; | |
import java.security.cert.X509Certificate; | |
import javax.security.auth.x500.X500Principal; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.content.pm.Signature; | |
import android.util.Log; | |
public class DeveloperUtils { | |
private static final String LOGTAG = "DeveloperUtils"; | |
private static final X500Principal DEBUG_DN = | |
new X500Principal("CN=Android Debug,O=Android,C=US"); | |
private static Boolean sIsDeveloperBuild = null; | |
public static void init(Context context) { | |
if (sIsDeveloperBuild != null) { | |
return; | |
} | |
try { | |
Signature s = context.getPackageManager().getPackageInfo(context.getPackageName(), | |
PackageManager.GET_SIGNATURES).signatures[0]; | |
CertificateFactory cf = CertificateFactory.getInstance("X.509"); | |
X509Certificate cert = | |
(X509Certificate) cf.generateCertificate(new ByteArrayInputStream(s.toByteArray())); | |
sIsDeveloperBuild = cert.getSubjectX500Principal().equals(DEBUG_DN); | |
if (sIsDeveloperBuild) { | |
Log.d(LOGTAG, "Is this a developer build? " + sIsDeveloperBuild); | |
} | |
} catch (Exception e) { | |
sIsDeveloperBuild = false; | |
} | |
} | |
public static boolean isDeveloperBuild() { | |
return sIsDeveloperBuild; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment