I had this problem too. I think this is because my Java version is low. I found that in my Android application, uuid.split("-")
and uuid.replace("-")
are both useless. I guess it may be caused by that Java regards "-" as a regular expression. However when I try uuid.split("\\-")
and uuid.replace("\\-")
, they didn't work either. I don't know why this happened. I think it's a bug of Java.
According to Fildor's comment, in Android's implementation, uuid.split("-")
is used to split the uuid string into 5 parts. Then because of the mentioned bug, the uuid string couldn't be spliced into 5 parts. So "Invalid UUID" exception is thrown.
However, we can modify android's source code to avoid this problem. Using substring()
method, we can split the uuid string into 5 parts. And then we can make the uuid.
The following codes worked for me:
public static UUID makeUuid(String uuidString) {
String[] parts = {
uuidString.substring(0, 7),
uuidString.substring(9, 12),
uuidString.substring(14, 17),
uuidString.substring(19, 22),
uuidString.substring(24, 35)
};
long m1 = Long.parseLong(parts[0], 16);
long m2 = Long.parseLong(parts[1], 16);
long m3 = Long.parseLong(parts[2], 16);
long lsb1 = Long.parseLong(parts[3], 16);
long lsb2 = Long.parseLong(parts[4], 16);
long msb = (m1 << 32) | (m2 << 16) | m3;
long lsb = (lsb1 << 48) | lsb2;
return new UUID(msb, lsb);
}