Getting a unique ID signature for every android device

Getting a unique ID signature for every android device

A problem often encountered while trying to keep track on your users actions is how to get a device identification for every user that installed your application. There is no method that will give you a definite and unique ID, but there are a few of them that are stated as 99% sure. We will give you a simple Class that uses all those methods in order to give you confidence.

Before presenting the class itself, you should know that this class doesn’t handle the caching of the ID in database. You have to make those implementations yourself. We are using the Settings.get() and Settings.set() methods of our own settings class that handles the simple database based key-value settings implementation. I will write about that exact system in one of my next posts, so make sure you follow our blog or social profiles (links are to the right) on a regular basis :).

Finally, the class itself

// ---------------------------------------
// Class: DeviceID
// Usage: Generate a unique ID for your
//        app installation
// ---------------------------------------

public class DeviceID {
  private static String ID = null;
	static Context context;
	// return a cached unique ID for each device
	public static String getID() {
		// if the ID isn't cached inside the class itself
		if (ID == null) {
			//get it from database / settings table (implement your own method here)
			ID = Settings.get("DeviceID");
		// if the saved value was incorrect
		if (ID.equals("0")) {
			// generate a new ID
			ID = generateID();
			if (ID != null) {
				// save it to database / setting (implement your own method here)
				Settings.set("DeviceID", ID);
		return ID;

	// generate a unique ID for each device
	// use available schemes if possible / generate a random signature instead 
	private static String generateID() {
		// use the ANDROID_ID constant, generated at the first device boot
		String deviceId = Secure.getString(context.getContentResolver(),

		// in case known problems are occured
		if ("9774d56d682e549c".equals(deviceId) || deviceId == null) {

			// get a unique deviceID like IMEI for GSM or ESN for CDMA phones
			// don't forget:
			deviceId = ((TelephonyManager) context
                                                       .getSystemService( Context.TELEPHONY_SERVICE ))

			// if nothing else works, generate a random number
			if (deviceId == null) {

				Random tmpRand = new Random();
				deviceId = String.valueOf(tmpRand.nextLong());
		// any value is hashed to have consistent format
		return getHash(deviceId);
	// generates a SHA-1 hash for any string
    public static String getHash(String stringToHash) {

    	MessageDigest digest = null;
		try {
			digest = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
    	byte[] result = null;
		try {
			result = digest.digest(stringToHash.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {

    	StringBuilder sb = new StringBuilder();

    	for (byte b : result)
    	    sb.append(String.format("%02X", b));

    	String messageDigest = sb.toString();
    	return messageDigest;


The usage is as simple as it gets:

String myID;
myID = DeviceID.getID();

Other than the comments in code above, let me just walk you through the methods of getting the ID:

  • First, try to get the ID from database
  • Second, try to generate the ID based on Androids random ID generated on first boot
  • Third, try to generate the ID based on IMEI for GSM or ESN for CDMA
  • Last, simply generate your own random ID

Generating AndroidID in this order gives you a more than enough safe way to get a unique signature. Generation occurs only the first time, and later on uses cache to fetch the ID.

If you have any questions feel free to leave them on the form underneath, and also add Profico on social pages (links are in the right column) to keep track of other cool articles 🙂

Ivan Lovrić

Ivan Lovrić

Explore more

We shape our core capabilities around lean product teams capable of delivering immense value to organisations worldwide

Got a project?
Let's have a chat!

Zagreb Office

Radnička cesta 39

Split Office

Put Orišca 11, 2nd floor

Contact info

Split+Zagreb, Croatia
+385 91 395

This website uses cookies in order to provide a better user experience and functionality. By continuing to browse the site you agree to our  Cookie and Privacy Policy .