咨询:Android平台生成64位唯一ID的有效方法
Hey there! Let's work through your need for a 64-bit unique ID on Android. Since you've already ruled out ANDROID_ID (thanks to those pesky non-compliant manufacturers) and full UUIDs (way too long), here are some straightforward, lightweight solutions that fit your exact requirements—no persistence needed, no security hoops to jump through:
Option 1: Timestamp + Random Number Combo
This is a simple, fast approach that leverages time and randomness to minimize collision risk. Since you don't need the ID to survive app uninstalls, you can generate it on-demand whenever you need a unique value.
Kotlin Code Example:
fun generate64BitId(): Long { val timestamp = System.currentTimeMillis() // Add a 16-bit random number to avoid collisions in the same millisecond val randomSuffix = (0..65535).random().toLong() // Combine timestamp (48 bits) and random suffix (16 bits) into a 64-bit Long return (timestamp shl 16) or randomSuffix } // If you need a 16-character hex string (which maps to 64 bits): fun generate64BitHexId(): String { val id = generate64BitId() return id.toString(16).padStart(16, '0') // Ensure consistent 16-character length }
Why this works:
- No system permissions required
- Collision risk is extremely low: even if you generate hundreds of IDs per millisecond, the 16-bit random suffix gives 65,536 unique combinations to avoid duplicates.
Option 2: PID + Thread ID + Timestamp
For even lower collision odds, combine process ID, thread ID, and timestamp. This is ideal if you're generating IDs across multiple threads or processes in your app.
Kotlin Code Example:
fun generate64BitId(): Long { val pid = android.os.Process.myPid().toLong() val tid = Thread.currentThread().id val timestamp = System.currentTimeMillis() // Split 64 bits: 40 for timestamp, 12 for PID, 12 for Thread ID return (timestamp shl 24) or ((pid and 0xFFFL) shl 12) or (tid and 0xFFFL) }
Why this works:
- Combines three unique context values, so duplicates would only happen if the same process/thread generates an ID in the exact same millisecond—an edge case you're unlikely to hit.
Option 3: Truncated UUID
Since UUID.randomUUID() generates a cryptographically random 128-bit value, you can simply take its most significant 64 bits to get a high-quality random 64-bit ID. This is the shortest code solution.
Kotlin Code Example:
fun generate64BitId(): Long { return UUID.randomUUID().mostSignificantBits } // For a hex string: fun generate64BitHexId(): String { val highBits = UUID.randomUUID().mostSignificantBits return java.lang.Long.toHexString(highBits).padStart(16, '0') }
Why this works:
- UUIDs are designed for uniqueness, so taking half the bits still gives you more than enough randomness for local, non-persistent IDs.
- No extra logic needed—just leverage the existing UUID utility.
All these options avoid the pitfalls you mentioned: no reliance on manufacturer-compliant ANDROID_ID, and they all produce valid 64-bit values (either as a Long or a 16-character hex string). Since you don't need persistence or tamper resistance, any of these will work perfectly for your use case.
内容的提问来源于stack exchange,提问作者Nidhoegger

