Make non-profileable an error for macrobenchmarks

Test: ./gradlew benchmark:integ:macrobench:cC -P android.testInstrumentationRunnerArguments.class=androidx.benchmark.integration.macrobenchmark.SmallListStartupBenchmark # failed without manifest changes
Fixes: 179930326

Enforce profileable, as traces are less useful without it, and can
miss potentially important info necessary for metrics (like
reportFullyDrawn compat behavior).

Change-Id: Ie918468e5d83ca57a2f76c45d31e4ba9d71e1d61
diff --git a/benchmark/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml b/benchmark/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
index 58d1f37..854c056 100644
--- a/benchmark/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
+++ b/benchmark/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
@@ -25,6 +25,10 @@
         android:theme="@style/Theme.AppCompat"
         tools:ignore="MissingApplicationIcon">
 
+        <!-- Profileable to enable macrobenchmark profiling -->
+        <!--suppress AndroidElementNotAllowed -->
+        <profileable android:shell="true"/>
+
         <!--
         Activities need to be exported so the macrobenchmark can discover them
         under the new package visibility changes for Android 11.
diff --git a/benchmark/macro/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt b/benchmark/macro/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt
index 7936a67..2069797 100644
--- a/benchmark/macro/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt
+++ b/benchmark/macro/src/main/java/androidx/benchmark/macro/Macrobenchmark.kt
@@ -14,24 +14,12 @@
  * limitations under the License.
  */
 
-package androidx.benchmark.macro/*
- * Copyright 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+package androidx.benchmark.macro
 
+import android.annotation.SuppressLint
 import android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE
 import android.content.pm.PackageManager
+import android.os.Build
 import android.util.Log
 import androidx.benchmark.BenchmarkResult
 import androidx.benchmark.InstrumentationResults
@@ -52,6 +40,13 @@
         )
     }
 
+    @SuppressLint("UnsafeNewApiCall")
+    val errorNotProfileable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+        !applicationInfo.isProfileableByShell
+    } else {
+        false
+    }
+
     val errors = DeviceInfo.errors +
         // TODO: Merge this debuggable check / definition with Errors.kt in benchmark-common
         listOfNotNull(
@@ -67,6 +62,23 @@
                     in ways that mean benchmark improvements might not carry over to a
                     real user's experience (or even regress release performance).
                 """.trimIndent()
+            ),
+            conditionalError(
+                hasError = errorNotProfileable,
+                id = "NOT-PROFILEABLE",
+                summary = "Benchmark Target is NOT profileable",
+                message = """
+                    Target package $packageName
+                    is running without profileable. Profileable is required to enable
+                    macrobenchmark to capture detailed trace information from the target process,
+                    such as System tracing sections definied in the app, or libraries.
+
+                    To make the target profileable, add the following in your target app's
+                    main AndroidManifest.xml, within the application tag:
+
+                    <!--suppress AndroidElementNotAllowed -->
+                    <profileable android:shell="true"/>
+                """.trimIndent()
             )
         ).sortedBy { it.id }
 
diff --git a/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml b/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
index baca019..532a2a4 100644
--- a/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
+++ b/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
@@ -25,6 +25,10 @@
         android:icon="@mipmap/ic_launcher"
         tools:ignore="GoogleAppIndexingWarning">
 
+        <!-- Profileable to enable macrobenchmark profiling -->
+        <!--suppress AndroidElementNotAllowed -->
+        <profileable android:shell="true"/>
+
         <!--
         Activities need to be exported so the macrobenchmark can discover them
         under the new package visibility changes for Android 11.