如何用Java获取Cordova app同款Android设备UUID?
device.uuid Works on Android & How to Get the Same ID in Native Java Let's break this down step by step. The this.device.uuid you're seeing in the Cordova app isn't the IMEI (which requires the restricted permissions you ran into) — it's almost certainly the Android ID from Settings.Secure.ANDROID_ID, and here's the full breakdown:
1. What Cordova's device.uuid Actually Returns
The Cordova cordova-plugin-device (the plugin that powers device.uuid) uses this logic for Android devices:
- First, it fetches
Settings.Secure.ANDROID_ID— a 64-bit hexadecimal string (16 characters long, exactly matching your example29904bc142b60dce). - This ID stays fixed for the same app (same signing key) on the same device/user profile. That aligns with your observation that the ID never changes.
- Crucially, accessing this ID requires no special permissions — no
READ_PRIVILEGED_PHONE_STATEor evenREAD_PHONE_STATEis needed, which is why the Cordova app can get it without issues.
2. Native Java Code to Fetch the Same ID
You can retrieve this identical ID with a simple, permission-free snippet:
import android.content.Context; import android.provider.Settings; public String getCordovaStyleDeviceUuid(Context context) { return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); }
Key Notes:
- On Android 8.0 (API level 26)+:
ANDROID_IDis unique per app signature and user. If you change your app's signing key, the ID will update. For a single signed app on the same device/user, it remains stable. - On pre-8.0 Android versions:
ANDROID_IDwas a device-wide value, but some older devices had bugs where it reset or returned a generic fixed string — this is rare in modern devices.
3. Why Your Original TelephonyManager Approach Failed
The tManager.getDeviceId() method returns the device's IMEI (or MEID for CDMA devices), a hardware-specific identifier. Starting in Android 10 (API level 29), third-party apps can no longer access IMEIs directly — the READ_PRIVILEGED_PHONE_STATE permission is locked to system apps only. Cordova avoids this restriction entirely by using the Android ID instead.
内容的提问来源于stack exchange,提问作者Vishal Tiwari

