Improve version detection.

master uses MASTER as its id, so the previous one would fail.

Change-Id: I14948ec005d0e5e427f8e6b9dd0173d1cac1713b
diff --git a/tools/heap_profile b/tools/heap_profile
index 66d7ba0..f2aaf0f 100755
--- a/tools/heap_profile
+++ b/tools/heap_profile
@@ -135,6 +135,21 @@
     "https://docs.perfetto.dev/#/heapprofd?id=troubleshooting.",
     file=sys.stderr)
 
+SDK = {
+    'R': 30,
+}
+
+def release_or_newer(release):
+  sdk = int(subprocess.check_output(
+    ['adb', 'shell', 'getprop', 'ro.system.build.version.sdk']
+  ).decode('utf-8').strip())
+  if sdk >= SDK[release]:
+    return True
+  codename = subprocess.check_output(
+    ['adb', 'shell', 'getprop', 'ro.build.version.codename']
+  ).decode('utf-8').strip()
+  return codename == release
+
 def main(argv):
   parser = argparse.ArgumentParser()
   parser.add_argument(
@@ -250,10 +265,9 @@
 
   args = parser.parse_args()
 
-  codename = subprocess.check_output(
-    ['adb', 'shell', 'getprop', 'ro.build.id']).decode('utf-8')[0]
-
-  uuid_trace = codename >= 'R'
+  # TODO(fmayer): Maybe feature detect whether we can remove traces instead of
+  # this.
+  uuid_trace = release_or_newer('R')
   if uuid_trace:
     profile_device_path = '/data/misc/perfetto-traces/profile-' + UUID
   else: